From 9ecffeda110a4ad105a93a60d6cee7efe30e2769 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Mon, 2 Jun 2025 17:55:05 +0200 Subject: [PATCH 1/7] Improve dialect handling --- .../util/JpaHibernatePropertiesProvider.java | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main/java/ca/uhn/fhir/jpa/starter/util/JpaHibernatePropertiesProvider.java b/src/main/java/ca/uhn/fhir/jpa/starter/util/JpaHibernatePropertiesProvider.java index 9a4bc0bea75..d6a2944cc0f 100644 --- a/src/main/java/ca/uhn/fhir/jpa/starter/util/JpaHibernatePropertiesProvider.java +++ b/src/main/java/ca/uhn/fhir/jpa/starter/util/JpaHibernatePropertiesProvider.java @@ -2,31 +2,44 @@ import ca.uhn.fhir.context.ConfigurationException; import ca.uhn.fhir.jpa.config.HibernatePropertiesProvider; +import ca.uhn.fhir.util.ReflectionUtil; import org.hibernate.dialect.Dialect; import org.hibernate.engine.jdbc.dialect.internal.StandardDialectResolver; import org.hibernate.engine.jdbc.dialect.spi.DatabaseMetaDataDialectResolutionInfoAdapter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import java.sql.Connection; import java.sql.SQLException; import javax.sql.DataSource; +import static org.apache.commons.lang3.StringUtils.isNotBlank; + public class JpaHibernatePropertiesProvider extends HibernatePropertiesProvider { + private static final Logger ourLog = LoggerFactory.getLogger(JpaHibernatePropertiesProvider.class); - private final Dialect dialect; + private final Dialect myDialect; - public JpaHibernatePropertiesProvider(LocalContainerEntityManagerFactoryBean myEntityManagerFactory) { - DataSource connection = myEntityManagerFactory.getDataSource(); - try (Connection dbConnection = connection.getConnection()) { - dialect = new StandardDialectResolver() + public JpaHibernatePropertiesProvider(LocalContainerEntityManagerFactoryBean theEntityManagerFactory) { + String dialectClass = + (String) theEntityManagerFactory.getJpaPropertyMap().get("hibernate.dialect"); + if (isNotBlank(dialectClass)) { + myDialect = ReflectionUtil.newInstanceOrReturnNull(dialectClass, Dialect.class); + } else { + ourLog.warn("'hibernate.dialect' not set in application configuration! Please explicitly specify a valid HAPI FHIR hibernate dialect."); + DataSource connection = theEntityManagerFactory.getDataSource(); + try (Connection dbConnection = connection.getConnection()) { + myDialect = new StandardDialectResolver() .resolveDialect(new DatabaseMetaDataDialectResolutionInfoAdapter(dbConnection.getMetaData())); - } catch (SQLException sqlException) { - throw new ConfigurationException(sqlException.getMessage(), sqlException); + } catch (SQLException sqlException) { + throw new ConfigurationException(sqlException.getMessage(), sqlException); + } } } @Override public Dialect getDialect() { - return dialect; + return myDialect; } } From b69561017831ea203005365c6f1596d21c24af01 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Mon, 2 Jun 2025 18:03:26 +0200 Subject: [PATCH 2/7] Add test --- .../starter/util/JpaHibernatePropertiesProvider.java | 7 ++++--- .../ca/uhn/fhir/jpa/starter/ExampleServerR4IT.java | 10 ++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/ca/uhn/fhir/jpa/starter/util/JpaHibernatePropertiesProvider.java b/src/main/java/ca/uhn/fhir/jpa/starter/util/JpaHibernatePropertiesProvider.java index d6a2944cc0f..da0d496dbcc 100644 --- a/src/main/java/ca/uhn/fhir/jpa/starter/util/JpaHibernatePropertiesProvider.java +++ b/src/main/java/ca/uhn/fhir/jpa/starter/util/JpaHibernatePropertiesProvider.java @@ -23,15 +23,16 @@ public class JpaHibernatePropertiesProvider extends HibernatePropertiesProvider public JpaHibernatePropertiesProvider(LocalContainerEntityManagerFactoryBean theEntityManagerFactory) { String dialectClass = - (String) theEntityManagerFactory.getJpaPropertyMap().get("hibernate.dialect"); + (String) theEntityManagerFactory.getJpaPropertyMap().get("hibernate.dialect"); if (isNotBlank(dialectClass)) { myDialect = ReflectionUtil.newInstanceOrReturnNull(dialectClass, Dialect.class); } else { - ourLog.warn("'hibernate.dialect' not set in application configuration! Please explicitly specify a valid HAPI FHIR hibernate dialect."); + ourLog.warn( + "'hibernate.dialect' not set in application configuration! Please explicitly specify a valid HAPI FHIR hibernate dialect."); DataSource connection = theEntityManagerFactory.getDataSource(); try (Connection dbConnection = connection.getConnection()) { myDialect = new StandardDialectResolver() - .resolveDialect(new DatabaseMetaDataDialectResolutionInfoAdapter(dbConnection.getMetaData())); + .resolveDialect(new DatabaseMetaDataDialectResolutionInfoAdapter(dbConnection.getMetaData())); } catch (SQLException sqlException) { throw new ConfigurationException(sqlException.getMessage(), sqlException); } diff --git a/src/test/java/ca/uhn/fhir/jpa/starter/ExampleServerR4IT.java b/src/test/java/ca/uhn/fhir/jpa/starter/ExampleServerR4IT.java index 1aef83a21fe..a63a672cfa4 100644 --- a/src/test/java/ca/uhn/fhir/jpa/starter/ExampleServerR4IT.java +++ b/src/test/java/ca/uhn/fhir/jpa/starter/ExampleServerR4IT.java @@ -1,6 +1,8 @@ package ca.uhn.fhir.jpa.starter; import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.jpa.config.HibernatePropertiesProvider; +import ca.uhn.fhir.jpa.model.dialect.HapiFhirH2Dialect; import ca.uhn.fhir.jpa.searchparam.config.NicknameServiceConfig; import ca.uhn.fhir.jpa.starter.cr.CrProperties; import ca.uhn.fhir.model.primitive.IdDt; @@ -80,6 +82,9 @@ class ExampleServerR4IT implements IServerSupport { @Autowired private CrProperties crProperties; + @Autowired + private HibernatePropertiesProvider myHibernatePropertiesProvider; + @LocalServerPort private int port; @@ -367,6 +372,11 @@ void testValidateRemoteTerminology() { Parameters localResult = ourClient.operation().onType(CodeSystem.class).named("$validate-code").withParameter(Parameters.class, "url", new UrlType(testCodeSystem)).andParameter("coding", new Coding(testCodeSystem, "yes", null)).execute(); } + @Test + public void testHibernatePropertiesProvider_GetDialect() { + assertEquals(HapiFhirH2Dialect.class, myHibernatePropertiesProvider.getDialect().getClass()); + } + @BeforeEach void beforeEach() { From 299ab285991deb393b12c4d280c4b109765989c0 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Mon, 2 Jun 2025 18:05:15 +0200 Subject: [PATCH 3/7] Add partition settings --- src/main/resources/application.yaml | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index d4df357603c..f9d6366d58c 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -236,11 +236,26 @@ hapi: - https://unitsofmeasure.org/* - http://loinc.org/* - https://loinc.org/* + + # ### Uncomment the following section, and any sub-properties you need in order to enable + # ### partitioning support on this server. # partitioning: # allow_references_across_partitions: false # partitioning_include_in_search_hashes: false - # conditional_create_duplicate_identifiers_enabled: false - # request_tenant_partitioning_mode: true + # default_partition_id: 0 + # ### Enable the following setting to enable Database Partitioning Mode + # ### See: https://hapifhir.io/hapi-fhir/docs/server_jpa_partitioning/db_partition_mode.html + # database_partition_mode_enabled: false + # ### Partition Style: Partitioning requires a partition interceptor which helps the server + # ### select which partition(s) should be accessed for a given request. You can supply your + # ### own interceptor (see https://hapifhir.io/hapi-fhir/docs/server_jpa_partitioning/partitioning.html#partition-interceptors ) + # ### but the following setting can also be used to use a built-in form. + # ### Patient ID Partitioning Mode uses the patient/subject ID to determine the partition + # patient_id_partitioning_mode: false + # ### Request tenant mode can be used for a multi-tenancy setup where the request path is + # ### expected to have an additional path element, e.g. GET http://example.com/fhir/TENANT-ID/Patient/A + # request_tenant_partitioning_mode: false + cors: allow_Credentials: true # These are allowed_origin patterns, see: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/cors/CorsConfiguration.html#setAllowedOriginPatterns-java.util.List- From d0aa399c7567ccb4fc7381e5e30c9e4af87aa90c Mon Sep 17 00:00:00 2001 From: James Agnew Date: Fri, 6 Jun 2025 08:49:47 +0200 Subject: [PATCH 4/7] Add additional options --- .../uhn/fhir/jpa/starter/AppProperties.java | 20 ++++++++++ .../common/FhirServerConfigCommon.java | 18 ++++++++- src/main/resources/application.yaml | 38 ++++++++++--------- 3 files changed, 57 insertions(+), 19 deletions(-) diff --git a/src/main/java/ca/uhn/fhir/jpa/starter/AppProperties.java b/src/main/java/ca/uhn/fhir/jpa/starter/AppProperties.java index 0095b538909..7bb9a11d779 100644 --- a/src/main/java/ca/uhn/fhir/jpa/starter/AppProperties.java +++ b/src/main/java/ca/uhn/fhir/jpa/starter/AppProperties.java @@ -18,6 +18,8 @@ import java.util.Objects; import java.util.Set; +import static org.apache.commons.lang3.ObjectUtils.defaultIfNull; + @ConfigurationProperties(prefix = "hapi.fhir") @Configuration @EnableConfigurationProperties @@ -44,6 +46,7 @@ public class AppProperties { private Boolean mass_ingestion_mode_enabled = false; private Boolean language_search_parameter_enabled = false; private Boolean dao_scheduling_enabled = true; + private Boolean delete_enabled = true; private Boolean delete_expunge_enabled = false; private Boolean enable_index_missing_fields = false; private Boolean enable_index_contained_resource = false; @@ -107,6 +110,7 @@ public class AppProperties { private Integer maximum_expansion_size = 1000; private Map remote_terminology_service = null; + private Boolean match_url_cache_enabled = false; public List getCustomInterceptorClasses() { return custom_interceptor_classes; @@ -364,6 +368,14 @@ public Boolean getDelete_expunge_enabled() { return delete_expunge_enabled; } + public boolean getDelete_enabled() { + return defaultIfNull(delete_enabled, true); + } + + public void setDelete_enabled(boolean theDelete_enabled) { + delete_enabled = theDelete_enabled; + } + public void setDelete_expunge_enabled(Boolean delete_expunge_enabled) { this.delete_expunge_enabled = delete_expunge_enabled; } @@ -733,6 +745,14 @@ public void setRemote_terminology_service(Map remote_termi this.remote_terminology_service = remote_terminology_service; } + public boolean getMatch_url_cache_enabled() { + return defaultIfNull(match_url_cache_enabled, false); + } + + public void setMatch_url_cache_enabled(boolean theMatchUrlCacheEnabled) { + match_url_cache_enabled = theMatchUrlCacheEnabled; + } + public static class Cors { private Boolean allow_Credentials = true; private List allowed_origin = List.of("*"); diff --git a/src/main/java/ca/uhn/fhir/jpa/starter/common/FhirServerConfigCommon.java b/src/main/java/ca/uhn/fhir/jpa/starter/common/FhirServerConfigCommon.java index e9f3549c518..b96d4c975cd 100644 --- a/src/main/java/ca/uhn/fhir/jpa/starter/common/FhirServerConfigCommon.java +++ b/src/main/java/ca/uhn/fhir/jpa/starter/common/FhirServerConfigCommon.java @@ -16,6 +16,8 @@ import ca.uhn.fhir.rest.server.mail.MailSvc; import com.google.common.base.Strings; import org.hl7.fhir.r4.model.Bundle.BundleType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.boot.env.YamlPropertySourceLoader; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -36,7 +38,7 @@ @EnableTransactionManagement public class FhirServerConfigCommon { - private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirServerConfigCommon.class); + private static final Logger ourLog = LoggerFactory.getLogger(FhirServerConfigCommon.class); public FhirServerConfigCommon(AppProperties appProperties) { ourLog.info("Server configured to " + (appProperties.getAllow_contains_searches() ? "allow" : "deny") @@ -176,6 +178,8 @@ public JpaStorageSettings jpaStorageSettings(AppProperties appProperties) { jpaStorageSettings.setAllowMultipleDelete(appProperties.getAllow_multiple_delete()); jpaStorageSettings.setAllowExternalReferences(appProperties.getAllow_external_references()); jpaStorageSettings.setSchedulingDisabled(!appProperties.getDao_scheduling_enabled()); + jpaStorageSettings.setMatchUrlCacheEnabled(appProperties.getMatch_url_cache_enabled()); + jpaStorageSettings.setDeleteEnabled(appProperties.getDelete_enabled()); jpaStorageSettings.setDeleteExpungeEnabled(appProperties.getDelete_expunge_enabled()); jpaStorageSettings.setExpungeEnabled(appProperties.getExpunge_enabled()); jpaStorageSettings.setLanguageSearchParameterEnabled(appProperties.getLanguage_search_parameter_enabled()); @@ -286,6 +290,18 @@ public PartitionSettings partitionSettings(AppProperties appProperties) { } retVal.setConditionalCreateDuplicateIdentifiersEnabled( appProperties.getPartitioning().getConditional_create_duplicate_identifiers_enabled()); + + ourLog.info(""" + Partitioning is enabled on this server. Settings: + * Database Partition Mode Enabled: {} + * Default Partition ID : {} + * Cross-Partition References : {}""", + databasePartitionModeEnabled, + defaultPartitionId, + retVal.getAllowReferencesAcrossPartitions()); + + } else { + ourLog.info("Partitioning is not enabled on this server"); } return retVal; diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index f9d6366d58c..990721b8fd0 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -190,7 +190,9 @@ hapi: # default_encoding: JSON # default_pretty_print: true # default_page_size: 20 + # delete_enabled: true # delete_expunge_enabled: true + # match_url_cache_enabled: false # enable_repository_validating_interceptor: true # enable_index_missing_fields: false # enable_index_of_type: true @@ -237,24 +239,24 @@ hapi: - http://loinc.org/* - https://loinc.org/* - # ### Uncomment the following section, and any sub-properties you need in order to enable - # ### partitioning support on this server. - # partitioning: - # allow_references_across_partitions: false - # partitioning_include_in_search_hashes: false - # default_partition_id: 0 - # ### Enable the following setting to enable Database Partitioning Mode - # ### See: https://hapifhir.io/hapi-fhir/docs/server_jpa_partitioning/db_partition_mode.html - # database_partition_mode_enabled: false - # ### Partition Style: Partitioning requires a partition interceptor which helps the server - # ### select which partition(s) should be accessed for a given request. You can supply your - # ### own interceptor (see https://hapifhir.io/hapi-fhir/docs/server_jpa_partitioning/partitioning.html#partition-interceptors ) - # ### but the following setting can also be used to use a built-in form. - # ### Patient ID Partitioning Mode uses the patient/subject ID to determine the partition - # patient_id_partitioning_mode: false - # ### Request tenant mode can be used for a multi-tenancy setup where the request path is - # ### expected to have an additional path element, e.g. GET http://example.com/fhir/TENANT-ID/Patient/A - # request_tenant_partitioning_mode: false + ### Uncomment the following section, and any sub-properties you need in order to enable + ### partitioning support on this server. + partitioning: + allow_references_across_partitions: false + partitioning_include_in_search_hashes: false + default_partition_id: 0 + ### Enable the following setting to enable Database Partitioning Mode + ### See: https://hapifhir.io/hapi-fhir/docs/server_jpa_partitioning/db_partition_mode.html + database_partition_mode_enabled: true + ### Partition Style: Partitioning requires a partition interceptor which helps the server + ### select which partition(s) should be accessed for a given request. You can supply your + ### own interceptor (see https://hapifhir.io/hapi-fhir/docs/server_jpa_partitioning/partitioning.html#partition-interceptors ) + ### but the following setting can also be used to use a built-in form. + ### Patient ID Partitioning Mode uses the patient/subject ID to determine the partition + patient_id_partitioning_mode: true + ### Request tenant mode can be used for a multi-tenancy setup where the request path is + ### expected to have an additional path element, e.g. GET http://example.com/fhir/TENANT-ID/Patient/A + request_tenant_partitioning_mode: false cors: allow_Credentials: true From f31a7743c34920acc464e09005234c60c9e2f7e6 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Fri, 6 Jun 2025 08:50:50 +0200 Subject: [PATCH 5/7] Spotless --- .../fhir/jpa/starter/common/FhirServerConfigCommon.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/ca/uhn/fhir/jpa/starter/common/FhirServerConfigCommon.java b/src/main/java/ca/uhn/fhir/jpa/starter/common/FhirServerConfigCommon.java index b96d4c975cd..7e5a97e7f7e 100644 --- a/src/main/java/ca/uhn/fhir/jpa/starter/common/FhirServerConfigCommon.java +++ b/src/main/java/ca/uhn/fhir/jpa/starter/common/FhirServerConfigCommon.java @@ -291,14 +291,15 @@ public PartitionSettings partitionSettings(AppProperties appProperties) { retVal.setConditionalCreateDuplicateIdentifiersEnabled( appProperties.getPartitioning().getConditional_create_duplicate_identifiers_enabled()); - ourLog.info(""" + ourLog.info( + """ Partitioning is enabled on this server. Settings: * Database Partition Mode Enabled: {} * Default Partition ID : {} * Cross-Partition References : {}""", - databasePartitionModeEnabled, - defaultPartitionId, - retVal.getAllowReferencesAcrossPartitions()); + databasePartitionModeEnabled, + defaultPartitionId, + retVal.getAllowReferencesAcrossPartitions()); } else { ourLog.info("Partitioning is not enabled on this server"); From d972a2a2f33498c1c40229c9d1ad47a953472062 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Fri, 6 Jun 2025 09:23:02 +0200 Subject: [PATCH 6/7] Add index storage optimized --- src/main/java/ca/uhn/fhir/jpa/starter/AppProperties.java | 9 +++++++++ .../fhir/jpa/starter/common/FhirServerConfigCommon.java | 1 + src/main/resources/application.yaml | 2 ++ 3 files changed, 12 insertions(+) diff --git a/src/main/java/ca/uhn/fhir/jpa/starter/AppProperties.java b/src/main/java/ca/uhn/fhir/jpa/starter/AppProperties.java index 7bb9a11d779..80c7b21f21b 100644 --- a/src/main/java/ca/uhn/fhir/jpa/starter/AppProperties.java +++ b/src/main/java/ca/uhn/fhir/jpa/starter/AppProperties.java @@ -111,6 +111,7 @@ public class AppProperties { private Map remote_terminology_service = null; private Boolean match_url_cache_enabled = false; + private Boolean index_storage_optimized = false; public List getCustomInterceptorClasses() { return custom_interceptor_classes; @@ -753,6 +754,14 @@ public void setMatch_url_cache_enabled(boolean theMatchUrlCacheEnabled) { match_url_cache_enabled = theMatchUrlCacheEnabled; } + public boolean getIndex_storage_optimized() { + return defaultIfNull(index_storage_optimized, false); + } + + public void setIndex_storage_optimized(boolean theIndex_storage_optimized) { + index_storage_optimized = theIndex_storage_optimized; + } + public static class Cors { private Boolean allow_Credentials = true; private List allowed_origin = List.of("*"); diff --git a/src/main/java/ca/uhn/fhir/jpa/starter/common/FhirServerConfigCommon.java b/src/main/java/ca/uhn/fhir/jpa/starter/common/FhirServerConfigCommon.java index 7e5a97e7f7e..4b79117e28e 100644 --- a/src/main/java/ca/uhn/fhir/jpa/starter/common/FhirServerConfigCommon.java +++ b/src/main/java/ca/uhn/fhir/jpa/starter/common/FhirServerConfigCommon.java @@ -178,6 +178,7 @@ public JpaStorageSettings jpaStorageSettings(AppProperties appProperties) { jpaStorageSettings.setAllowMultipleDelete(appProperties.getAllow_multiple_delete()); jpaStorageSettings.setAllowExternalReferences(appProperties.getAllow_external_references()); jpaStorageSettings.setSchedulingDisabled(!appProperties.getDao_scheduling_enabled()); + jpaStorageSettings.setIndexStorageOptimized(appProperties.getIndex_storage_optimized()); jpaStorageSettings.setMatchUrlCacheEnabled(appProperties.getMatch_url_cache_enabled()); jpaStorageSettings.setDeleteEnabled(appProperties.getDelete_enabled()); jpaStorageSettings.setDeleteExpungeEnabled(appProperties.getDelete_expunge_enabled()); diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 990721b8fd0..240ed299a55 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -194,6 +194,8 @@ hapi: # delete_expunge_enabled: true # match_url_cache_enabled: false # enable_repository_validating_interceptor: true + ### Reduce the size used by search indexes by not tagging every row with the resource type and parameter name (this setting makes manual inspection of the database more difficult, but does not impact HAPI FHIR functionality in any way) + # index_storage_optimized: false # enable_index_missing_fields: false # enable_index_of_type: true # enable_index_contained_resource: false From 0d8f2aac1463444e2a29e83f60d070e696f93bf1 Mon Sep 17 00:00:00 2001 From: Patrick Werner Date: Tue, 8 Jul 2025 17:50:56 +0200 Subject: [PATCH 7/7] fix: correct formatting and spacing in AppProperties.java --- src/main/java/ca/uhn/fhir/jpa/starter/AppProperties.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/ca/uhn/fhir/jpa/starter/AppProperties.java b/src/main/java/ca/uhn/fhir/jpa/starter/AppProperties.java index 9c9aeaa1caf..c053d62c38f 100644 --- a/src/main/java/ca/uhn/fhir/jpa/starter/AppProperties.java +++ b/src/main/java/ca/uhn/fhir/jpa/starter/AppProperties.java @@ -772,9 +772,9 @@ public boolean getIndex_storage_optimized() { public void setIndex_storage_optimized(boolean theIndex_storage_optimized) { index_storage_optimized = theIndex_storage_optimized; - } - - public JpaStorageSettings.StoreMetaSourceInformationEnum getStore_meta_source_information() { + } + + public JpaStorageSettings.StoreMetaSourceInformationEnum getStore_meta_source_information() { return store_meta_source_information; }