diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/JpaVendorAdapter.java b/spring-orm/src/main/java/org/springframework/orm/jpa/JpaVendorAdapter.java index f4e94fd9f79..e82d52d1915 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/JpaVendorAdapter.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/JpaVendorAdapter.java @@ -63,6 +63,12 @@ public interface JpaVendorAdapter { * non-unit-dependent properties. Effectively, this PersistenceUnitInfo-based * variant only needs to be implemented if there is an actual need to react * to unit-specific characteristics such as the transaction type. + *

NOTE: This variant will only be invoked in case of Java EE style + * container bootstrapping where a {@link PersistenceUnitInfo} is present + * (i.e. {@link LocalContainerEntityManagerFactoryBean}. In case of simple + * Java SE style bootstrapping via {@link javax.persistence.Persistence} + * (i.e. {@link LocalEntityManagerFactoryBean}), the parameter-less + * {@link #getJpaPropertyMap()} variant will be called directly. * @param pui the PersistenceUnitInfo for the current persistence unit * @return a Map of JPA properties, as accepted by the standard JPA bootstrap * facilities, or an empty Map if there are no properties to expose 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 316bdc1c26c..aa048208ae3 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 @@ -115,32 +115,16 @@ public class HibernateJpaVendorAdapter extends AbstractJpaVendorAdapter { @Override public Map getJpaPropertyMap(PersistenceUnitInfo pui) { - Map jpaProperties = getJpaPropertyMap(); - - if (this.jpaDialect.prepareConnection && pui.getTransactionType() != PersistenceUnitTransactionType.JTA) { - // 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"); - jpaProperties.put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_HOLD"); - } - catch (NoSuchFieldException ex) { - // Try Hibernate 5.1 - try { - AvailableSettings.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; + return buildJpaPropertyMap(this.jpaDialect.prepareConnection && + pui.getTransactionType() != PersistenceUnitTransactionType.JTA); } @Override public Map getJpaPropertyMap() { + return buildJpaPropertyMap(this.jpaDialect.prepareConnection); + } + + private Map buildJpaPropertyMap(boolean connectionReleaseOnClose) { Map jpaProperties = new HashMap<>(); if (getDatabasePlatform() != null) { @@ -160,6 +144,25 @@ public class HibernateJpaVendorAdapter extends AbstractJpaVendorAdapter { jpaProperties.put(AvailableSettings.SHOW_SQL, "true"); } + if (connectionReleaseOnClose) { + // 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"); + jpaProperties.put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_HOLD"); + } + catch (NoSuchFieldException ex) { + // Try Hibernate 5.1 + try { + AvailableSettings.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; }