HibernateJpaVendorAdapter sets connection release mode ON_CLOSE in non-PersistenceUnitInfo bootstrap scenario
Issue: SPR-16162
This commit is contained in:
@@ -63,6 +63,12 @@ public interface JpaVendorAdapter {
|
|||||||
* non-unit-dependent properties. Effectively, this PersistenceUnitInfo-based
|
* non-unit-dependent properties. Effectively, this PersistenceUnitInfo-based
|
||||||
* variant only needs to be implemented if there is an actual need to react
|
* variant only needs to be implemented if there is an actual need to react
|
||||||
* to unit-specific characteristics such as the transaction type.
|
* 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
|
* @param pui the PersistenceUnitInfo for the current persistence unit
|
||||||
* @return a Map of JPA properties, as accepted by the standard JPA bootstrap
|
* @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
|
* facilities, or an empty Map if there are no properties to expose
|
||||||
|
|||||||
@@ -115,32 +115,16 @@ public class HibernateJpaVendorAdapter extends AbstractJpaVendorAdapter {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Object> getJpaPropertyMap(PersistenceUnitInfo pui) {
|
public Map<String, Object> getJpaPropertyMap(PersistenceUnitInfo pui) {
|
||||||
Map<String, Object> jpaProperties = getJpaPropertyMap();
|
return buildJpaPropertyMap(this.jpaDialect.prepareConnection &&
|
||||||
|
pui.getTransactionType() != PersistenceUnitTransactionType.JTA);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Object> getJpaPropertyMap() {
|
public Map<String, Object> getJpaPropertyMap() {
|
||||||
|
return buildJpaPropertyMap(this.jpaDialect.prepareConnection);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> buildJpaPropertyMap(boolean connectionReleaseOnClose) {
|
||||||
Map<String, Object> jpaProperties = new HashMap<>();
|
Map<String, Object> jpaProperties = new HashMap<>();
|
||||||
|
|
||||||
if (getDatabasePlatform() != null) {
|
if (getDatabasePlatform() != null) {
|
||||||
@@ -160,6 +144,25 @@ public class HibernateJpaVendorAdapter extends AbstractJpaVendorAdapter {
|
|||||||
jpaProperties.put(AvailableSettings.SHOW_SQL, "true");
|
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;
|
return jpaProperties;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user