Skip to content

Commit 7a04206

Browse files
committed
Compatibility with Hibernate 6.0/6.1 (for transitional purposes)
Closes gh-28813
1 parent cabd126 commit 7a04206

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

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

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -497,7 +497,7 @@ protected void doBegin(Object transaction, TransactionDefinition definition) {
497497
if (logger.isDebugEnabled()) {
498498
logger.debug("Preparing JDBC Connection of Hibernate Session [" + session + "]");
499499
}
500-
Connection con = session.connection();
500+
Connection con = session.getJdbcCoordinator().getLogicalConnection().getPhysicalConnection();
501501
Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);
502502
txObject.setPreviousIsolationLevel(previousIsolationLevel);
503503
txObject.setReadOnly(definition.isReadOnly());
@@ -562,7 +562,9 @@ protected void doBegin(Object transaction, TransactionDefinition definition) {
562562

563563
// Register the Hibernate Session's JDBC Connection for the DataSource, if set.
564564
if (getDataSource() != null) {
565-
ConnectionHolder conHolder = new ConnectionHolder(session::connection);
565+
final SessionImplementor sessionToUse = session;
566+
ConnectionHolder conHolder = new ConnectionHolder(
567+
() -> sessionToUse.getJdbcCoordinator().getLogicalConnection().getPhysicalConnection());
566568
if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
567569
conHolder.setTimeoutInSeconds(timeout);
568570
}
@@ -724,7 +726,7 @@ protected void doCleanupAfterCompletion(Object transaction) {
724726
// the isolation level and/or read-only flag of the JDBC Connection here.
725727
// Else, we need to rely on the connection pool to perform proper cleanup.
726728
try {
727-
Connection con = session.connection();
729+
Connection con = session.getJdbcCoordinator().getLogicalConnection().getPhysicalConnection();
728730
Integer previousHoldability = txObject.getPreviousHoldability();
729731
if (previousHoldability != null) {
730732
con.setHoldability(previousHoldability);
@@ -763,13 +765,15 @@ protected void doCleanupAfterCompletion(Object transaction) {
763765
/**
764766
* Disconnect a pre-existing Hibernate Session on transaction completion,
765767
* returning its database connection but preserving its entity state.
766-
* <p>The default implementation simply calls {@link Session#disconnect()}.
768+
* <p>The default implementation calls the equivalent of {@link Session#disconnect()}.
767769
* Subclasses may override this with a no-op or with fine-tuned disconnection logic.
768770
* @param session the Hibernate Session to disconnect
769771
* @see Session#disconnect()
770772
*/
771773
protected void disconnectOnCompletion(Session session) {
772-
session.disconnect();
774+
if (session instanceof SessionImplementor sessionImpl) {
775+
sessionImpl.getJdbcCoordinator().getLogicalConnection().manualDisconnect();
776+
}
773777
}
774778

775779
/**

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import org.hibernate.FlushMode;
2020
import org.hibernate.Session;
2121
import org.hibernate.SessionFactory;
22+
import org.hibernate.engine.spi.SessionImplementor;
2223

2324
import org.springframework.core.Ordered;
2425
import org.springframework.dao.DataAccessException;
@@ -69,7 +70,10 @@ public void suspend() {
6970
if (this.holderActive) {
7071
TransactionSynchronizationManager.unbindResource(this.sessionFactory);
7172
// Eagerly disconnect the Session here, to make release mode "on_close" work on JBoss.
72-
getCurrentSession().disconnect();
73+
Session session = getCurrentSession();
74+
if (session instanceof SessionImplementor sessionImpl) {
75+
sessionImpl.getJdbcCoordinator().getLogicalConnection().manualDisconnect();
76+
}
7377
}
7478
}
7579

@@ -106,7 +110,9 @@ public void beforeCompletion() {
106110
session.setHibernateFlushMode(this.sessionHolder.getPreviousFlushMode());
107111
}
108112
// Eagerly disconnect the Session here, to make release mode "on_close" work nicely.
109-
session.disconnect();
113+
if (session instanceof SessionImplementor sessionImpl) {
114+
sessionImpl.getJdbcCoordinator().getLogicalConnection().manualDisconnect();
115+
}
110116
}
111117
finally {
112118
// Unbind at this point if it's a new Session...

0 commit comments

Comments
 (0)