Skip to content

Commit 7d1c2f1

Browse files
committed
Consistent default connection release mode with Hibernate 5.1
Issue: SPR-14548
1 parent 4ada571 commit 7d1c2f1

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

spring-orm-hibernate5/src/main/java/org/springframework/orm/hibernate5/LocalSessionFactoryBuilder.java

+34-7
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
4343
import org.hibernate.cfg.AvailableSettings;
4444
import org.hibernate.cfg.Configuration;
45-
import org.hibernate.cfg.Environment;
4645
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
4746
import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;
4847
import org.hibernate.engine.spi.SessionFactoryImplementor;
@@ -138,13 +137,27 @@ public LocalSessionFactoryBuilder(DataSource dataSource, ResourceLoader resource
138137
public LocalSessionFactoryBuilder(DataSource dataSource, ResourceLoader resourceLoader, MetadataSources metadataSources) {
139138
super(metadataSources);
140139

141-
getProperties().put(Environment.CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName());
140+
getProperties().put(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName());
142141
if (dataSource != null) {
143-
getProperties().put(Environment.DATASOURCE, dataSource);
142+
getProperties().put(AvailableSettings.DATASOURCE, dataSource);
144143
}
145144

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

149162
getProperties().put(AvailableSettings.CLASSLOADERS, Collections.singleton(resourceLoader.getClassLoader()));
150163
this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
@@ -191,8 +204,22 @@ else if (jtaTransactionManager instanceof TransactionManager) {
191204
"Unknown transaction manager type: " + jtaTransactionManager.getClass().getName());
192205
}
193206

194-
// Hibernate 5.2: manually enforce connection release mode AFTER_STATEMENT (the JTA default)
195-
getProperties().put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT");
207+
// Hibernate 5.1/5.2: manually enforce connection release mode AFTER_STATEMENT (the JTA default)
208+
try {
209+
// Try Hibernate 5.2
210+
AvailableSettings.class.getField("CONNECTION_HANDLING");
211+
getProperties().put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT");
212+
}
213+
catch (NoSuchFieldException ex) {
214+
// Try Hibernate 5.1
215+
try {
216+
AvailableSettings.class.getField("ACQUIRE_CONNECTIONS");
217+
getProperties().put("hibernate.connection.release_mode", "AFTER_STATEMENT");
218+
}
219+
catch (NoSuchFieldException ex2) {
220+
// on Hibernate 5.0.x or lower - no need to change the default there
221+
}
222+
}
196223

197224
return this;
198225
}

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

+16-2
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,22 @@ else if (getDatabase() != null) {
154154
}
155155

156156
if (this.jpaDialect.prepareConnection) {
157-
// Hibernate 5.2: manually enforce connection release mode ON_CLOSE (the former default)
158-
jpaProperties.put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_HOLD");
157+
// Hibernate 5.1/5.2: manually enforce connection release mode ON_CLOSE (the former default)
158+
try {
159+
// Try Hibernate 5.2
160+
Environment.class.getField("CONNECTION_HANDLING");
161+
jpaProperties.put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_HOLD");
162+
}
163+
catch (NoSuchFieldException ex) {
164+
// Try Hibernate 5.1
165+
try {
166+
Environment.class.getField("ACQUIRE_CONNECTIONS");
167+
jpaProperties.put("hibernate.connection.release_mode", "ON_CLOSE");
168+
}
169+
catch (NoSuchFieldException ex2) {
170+
// on Hibernate 5.0.x or lower - no need to change the default there
171+
}
172+
}
159173
}
160174

161175
return jpaProperties;

0 commit comments

Comments
 (0)