diff --git a/spring-orm-hibernate5/src/main/java/org/springframework/orm/hibernate5/LocalSessionFactoryBuilder.java b/spring-orm-hibernate5/src/main/java/org/springframework/orm/hibernate5/LocalSessionFactoryBuilder.java index f74fb8ad7e..cdace8ec26 100644 --- a/spring-orm-hibernate5/src/main/java/org/springframework/orm/hibernate5/LocalSessionFactoryBuilder.java +++ b/spring-orm-hibernate5/src/main/java/org/springframework/orm/hibernate5/LocalSessionFactoryBuilder.java @@ -42,7 +42,6 @@ import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.Configuration; -import org.hibernate.cfg.Environment; import org.hibernate.context.spi.CurrentTenantIdentifierResolver; import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider; import org.hibernate.engine.spi.SessionFactoryImplementor; @@ -138,13 +137,27 @@ public class LocalSessionFactoryBuilder extends Configuration { public LocalSessionFactoryBuilder(DataSource dataSource, ResourceLoader resourceLoader, MetadataSources metadataSources) { super(metadataSources); - getProperties().put(Environment.CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName()); + getProperties().put(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName()); if (dataSource != null) { - getProperties().put(Environment.DATASOURCE, dataSource); + getProperties().put(AvailableSettings.DATASOURCE, dataSource); } - // Hibernate 5.2: manually enforce connection release mode ON_CLOSE (the former default) - getProperties().put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_HOLD"); + // Hibernate 5.1/5.2: manually enforce connection release mode ON_CLOSE (the former default) + try { + // Try Hibernate 5.2 + AvailableSettings.class.getField("CONNECTION_HANDLING"); + getProperties().put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_HOLD"); + } + catch (NoSuchFieldException ex) { + // Try Hibernate 5.1 + try { + AvailableSettings.class.getField("ACQUIRE_CONNECTIONS"); + getProperties().put("hibernate.connection.release_mode", "ON_CLOSE"); + } + catch (NoSuchFieldException ex2) { + // on Hibernate 5.0.x or lower - no need to change the default there + } + } getProperties().put(AvailableSettings.CLASSLOADERS, Collections.singleton(resourceLoader.getClassLoader())); this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader); @@ -191,8 +204,22 @@ public class LocalSessionFactoryBuilder extends Configuration { "Unknown transaction manager type: " + jtaTransactionManager.getClass().getName()); } - // Hibernate 5.2: manually enforce connection release mode AFTER_STATEMENT (the JTA default) - getProperties().put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT"); + // Hibernate 5.1/5.2: manually enforce connection release mode AFTER_STATEMENT (the JTA default) + try { + // Try Hibernate 5.2 + AvailableSettings.class.getField("CONNECTION_HANDLING"); + getProperties().put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT"); + } + catch (NoSuchFieldException ex) { + // Try Hibernate 5.1 + try { + AvailableSettings.class.getField("ACQUIRE_CONNECTIONS"); + getProperties().put("hibernate.connection.release_mode", "AFTER_STATEMENT"); + } + catch (NoSuchFieldException ex2) { + // on Hibernate 5.0.x or lower - no need to change the default there + } + } return this; } diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/vendor/HibernateJpaVendorAdapter.java b/spring-orm/src/main/java/org/springframework/orm/jpa/vendor/HibernateJpaVendorAdapter.java index 4451a8bf9a..c30b0930ba 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/vendor/HibernateJpaVendorAdapter.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/vendor/HibernateJpaVendorAdapter.java @@ -154,8 +154,22 @@ public class HibernateJpaVendorAdapter extends AbstractJpaVendorAdapter { } if (this.jpaDialect.prepareConnection) { - // Hibernate 5.2: manually enforce connection release mode ON_CLOSE (the former default) - jpaProperties.put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_HOLD"); + // Hibernate 5.1/5.2: manually enforce connection release mode ON_CLOSE (the former default) + try { + // Try Hibernate 5.2 + Environment.class.getField("CONNECTION_HANDLING"); + jpaProperties.put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_HOLD"); + } + catch (NoSuchFieldException ex) { + // Try Hibernate 5.1 + try { + Environment.class.getField("ACQUIRE_CONNECTIONS"); + jpaProperties.put("hibernate.connection.release_mode", "ON_CLOSE"); + } + catch (NoSuchFieldException ex2) { + // on Hibernate 5.0.x or lower - no need to change the default there + } + } } return jpaProperties;