Browse Source

HibernateJpaVendorAdapter sets connection release mode ON_CLOSE in non-PersistenceUnitInfo bootstrap scenario

Issue: SPR-16162
pull/1578/merge
Juergen Hoeller 8 years ago
parent
commit
e2018e6b72
  1. 6
      spring-orm/src/main/java/org/springframework/orm/jpa/JpaVendorAdapter.java
  2. 47
      spring-orm/src/main/java/org/springframework/orm/jpa/vendor/HibernateJpaVendorAdapter.java

6
spring-orm/src/main/java/org/springframework/orm/jpa/JpaVendorAdapter.java

@ -63,6 +63,12 @@ public interface JpaVendorAdapter { @@ -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.
* <p><b>NOTE:</b> 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

47
spring-orm/src/main/java/org/springframework/orm/jpa/vendor/HibernateJpaVendorAdapter.java vendored

@ -115,32 +115,16 @@ public class HibernateJpaVendorAdapter extends AbstractJpaVendorAdapter { @@ -115,32 +115,16 @@ public class HibernateJpaVendorAdapter extends AbstractJpaVendorAdapter {
@Override
public Map<String, Object> getJpaPropertyMap(PersistenceUnitInfo pui) {
Map<String, Object> 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<String, Object> getJpaPropertyMap() {
return buildJpaPropertyMap(this.jpaDialect.prepareConnection);
}
private Map<String, Object> buildJpaPropertyMap(boolean connectionReleaseOnClose) {
Map<String, Object> jpaProperties = new HashMap<>();
if (getDatabasePlatform() != null) {
@ -160,6 +144,25 @@ public class HibernateJpaVendorAdapter extends AbstractJpaVendorAdapter { @@ -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;
}

Loading…
Cancel
Save