Skip to content

Commit fad931d

Browse files
committed
Consistent default connection release mode with Hibernate 5.1
Issue: SPR-14548
1 parent 8109a8c commit fad931d

File tree

2 files changed

+62
-17
lines changed

2 files changed

+62
-17
lines changed

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

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,22 @@ public LocalSessionFactoryBuilder(DataSource dataSource, ResourceLoader resource
143143
getProperties().put(Environment.DATASOURCE, dataSource);
144144
}
145145

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

149163
getProperties().put(AvailableSettings.CLASSLOADERS, Collections.singleton(resourceLoader.getClassLoader()));
150164
this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
@@ -191,8 +205,22 @@ else if (jtaTransactionManager instanceof TransactionManager) {
191205
"Unknown transaction manager type: " + jtaTransactionManager.getClass().getName());
192206
}
193207

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

197225
return this;
198226
}

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

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,23 @@
2222
import javax.persistence.EntityManagerFactory;
2323
import javax.persistence.spi.PersistenceProvider;
2424

25+
import org.hibernate.cfg.AvailableSettings;
2526
import org.hibernate.cfg.Environment;
2627
import org.hibernate.dialect.DB2Dialect;
28+
import org.hibernate.dialect.DerbyTenSevenDialect;
2729
import org.hibernate.dialect.H2Dialect;
2830
import org.hibernate.dialect.HSQLDialect;
2931
import org.hibernate.dialect.InformixDialect;
30-
import org.hibernate.dialect.MySQLDialect;
31-
import org.hibernate.dialect.Oracle9iDialect;
32-
import org.hibernate.dialect.SQLServerDialect;
32+
import org.hibernate.dialect.MySQL5Dialect;
33+
import org.hibernate.dialect.Oracle12cDialect;
34+
import org.hibernate.dialect.PostgreSQL95Dialect;
35+
import org.hibernate.dialect.SQLServer2012Dialect;
36+
import org.hibernate.dialect.SybaseDialect;
3337

3438
/**
3539
* {@link org.springframework.orm.jpa.JpaVendorAdapter} implementation for Hibernate
3640
* EntityManager. Developed and tested against Hibernate 5.0, 5.1 and 5.2;
37-
* backwards-compatible with Hibernate 4.3 as well.
41+
* backwards-compatible with Hibernate 4.3 at runtime on a best-effort basis.
3842
*
3943
* <p>Exposes Hibernate's persistence provider and EntityManager extension interface,
4044
* and adapts {@link AbstractJpaVendorAdapter}'s common configuration settings.
@@ -121,8 +125,22 @@ else if (getDatabase() != null) {
121125
}
122126

123127
if (this.jpaDialect.prepareConnection) {
124-
// Hibernate 5.2: manually enforce connection release mode ON_CLOSE (the former default)
125-
jpaProperties.put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_HOLD");
128+
// Hibernate 5.1/5.2: manually enforce connection release mode ON_CLOSE (the former default)
129+
try {
130+
// Try Hibernate 5.2
131+
AvailableSettings.class.getField("CONNECTION_HANDLING");
132+
jpaProperties.put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_HOLD");
133+
}
134+
catch (NoSuchFieldException ex) {
135+
// Try Hibernate 5.1
136+
try {
137+
AvailableSettings.class.getField("ACQUIRE_CONNECTIONS");
138+
jpaProperties.put("hibernate.connection.release_mode", "ON_CLOSE");
139+
}
140+
catch (NoSuchFieldException ex2) {
141+
// on Hibernate 5.0.x or lower - no need to change the default there
142+
}
143+
}
126144
}
127145

128146
return jpaProperties;
@@ -133,19 +151,18 @@ else if (getDatabase() != null) {
133151
* @param database the target database
134152
* @return the Hibernate database dialect class, or {@code null} if none found
135153
*/
136-
@SuppressWarnings("deprecation")
137154
protected Class<?> determineDatabaseDialectClass(Database database) {
138155
switch (database) {
139156
case DB2: return DB2Dialect.class;
140-
case DERBY: return org.hibernate.dialect.DerbyDialect.class;
157+
case DERBY: return DerbyTenSevenDialect.class;
141158
case H2: return H2Dialect.class;
142159
case HSQL: return HSQLDialect.class;
143160
case INFORMIX: return InformixDialect.class;
144-
case MYSQL: return MySQLDialect.class;
145-
case ORACLE: return Oracle9iDialect.class;
146-
case POSTGRESQL: return org.hibernate.dialect.PostgreSQLDialect.class;
147-
case SQL_SERVER: return SQLServerDialect.class;
148-
case SYBASE: return org.hibernate.dialect.SybaseDialect.class;
161+
case MYSQL: return MySQL5Dialect.class;
162+
case ORACLE: return Oracle12cDialect.class;
163+
case POSTGRESQL: return PostgreSQL95Dialect.class;
164+
case SQL_SERVER: return SQLServer2012Dialect.class;
165+
case SYBASE: return SybaseDialect.class;
149166
default: return null;
150167
}
151168
}

0 commit comments

Comments
 (0)