Skip to content

Commit e2018e6

Browse files
committed
HibernateJpaVendorAdapter sets connection release mode ON_CLOSE in non-PersistenceUnitInfo bootstrap scenario
Issue: SPR-16162
1 parent 72f20e8 commit e2018e6

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ default String getPersistenceProviderRootPackage() {
6363
* non-unit-dependent properties. Effectively, this PersistenceUnitInfo-based
6464
* variant only needs to be implemented if there is an actual need to react
6565
* to unit-specific characteristics such as the transaction type.
66+
* <p><b>NOTE:</b> This variant will only be invoked in case of Java EE style
67+
* container bootstrapping where a {@link PersistenceUnitInfo} is present
68+
* (i.e. {@link LocalContainerEntityManagerFactoryBean}. In case of simple
69+
* Java SE style bootstrapping via {@link javax.persistence.Persistence}
70+
* (i.e. {@link LocalEntityManagerFactoryBean}), the parameter-less
71+
* {@link #getJpaPropertyMap()} variant will be called directly.
6672
* @param pui the PersistenceUnitInfo for the current persistence unit
6773
* @return a Map of JPA properties, as accepted by the standard JPA bootstrap
6874
* facilities, or an empty Map if there are no properties to expose

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

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -115,32 +115,16 @@ public String getPersistenceProviderRootPackage() {
115115

116116
@Override
117117
public Map<String, Object> getJpaPropertyMap(PersistenceUnitInfo pui) {
118-
Map<String, Object> jpaProperties = getJpaPropertyMap();
119-
120-
if (this.jpaDialect.prepareConnection && pui.getTransactionType() != PersistenceUnitTransactionType.JTA) {
121-
// Hibernate 5.1/5.2: manually enforce connection release mode ON_CLOSE (the former default)
122-
try {
123-
// Try Hibernate 5.2
124-
AvailableSettings.class.getField("CONNECTION_HANDLING");
125-
jpaProperties.put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_HOLD");
126-
}
127-
catch (NoSuchFieldException ex) {
128-
// Try Hibernate 5.1
129-
try {
130-
AvailableSettings.class.getField("ACQUIRE_CONNECTIONS");
131-
jpaProperties.put("hibernate.connection.release_mode", "ON_CLOSE");
132-
}
133-
catch (NoSuchFieldException ex2) {
134-
// on Hibernate 5.0.x or lower - no need to change the default there
135-
}
136-
}
137-
}
138-
139-
return jpaProperties;
118+
return buildJpaPropertyMap(this.jpaDialect.prepareConnection &&
119+
pui.getTransactionType() != PersistenceUnitTransactionType.JTA);
140120
}
141121

142122
@Override
143123
public Map<String, Object> getJpaPropertyMap() {
124+
return buildJpaPropertyMap(this.jpaDialect.prepareConnection);
125+
}
126+
127+
private Map<String, Object> buildJpaPropertyMap(boolean connectionReleaseOnClose) {
144128
Map<String, Object> jpaProperties = new HashMap<>();
145129

146130
if (getDatabasePlatform() != null) {
@@ -160,6 +144,25 @@ public Map<String, Object> getJpaPropertyMap() {
160144
jpaProperties.put(AvailableSettings.SHOW_SQL, "true");
161145
}
162146

147+
if (connectionReleaseOnClose) {
148+
// Hibernate 5.1/5.2: manually enforce connection release mode ON_CLOSE (the former default)
149+
try {
150+
// Try Hibernate 5.2
151+
AvailableSettings.class.getField("CONNECTION_HANDLING");
152+
jpaProperties.put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_HOLD");
153+
}
154+
catch (NoSuchFieldException ex) {
155+
// Try Hibernate 5.1
156+
try {
157+
AvailableSettings.class.getField("ACQUIRE_CONNECTIONS");
158+
jpaProperties.put("hibernate.connection.release_mode", "ON_CLOSE");
159+
}
160+
catch (NoSuchFieldException ex2) {
161+
// on Hibernate 5.0.x or lower - no need to change the default there
162+
}
163+
}
164+
}
165+
163166
return jpaProperties;
164167
}
165168

0 commit comments

Comments
 (0)