From d0b44c48ef6926f59a265222040fafaedfca42a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Thu, 14 Jan 2021 11:12:42 +0100 Subject: [PATCH] HHH-14404 Take into account the connectionHandlingMode passed through SessionBuilder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Yoann Rodière --- .../AbstractSharedSessionContract.java | 20 ++++++++++++++----- .../internal/JdbcSessionContextImpl.java | 3 ++- .../internal/SessionFactoryImpl.java | 2 +- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java b/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java index 9ad4e9f156..2fd16f8f8f 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java @@ -86,6 +86,7 @@ import org.hibernate.query.spi.NativeQueryImplementor; import org.hibernate.query.spi.QueryImplementor; import org.hibernate.query.spi.ScrollableResultsImplementor; import org.hibernate.resource.jdbc.spi.JdbcSessionContext; +import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode; import org.hibernate.resource.jdbc.spi.StatementInspector; import org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl; import org.hibernate.resource.transaction.spi.TransactionCoordinator; @@ -134,6 +135,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont private FlushMode flushMode; private boolean autoJoinTransactions; + private final PhysicalConnectionHandlingMode connectionHandlingMode; private CacheMode cacheMode; @@ -181,11 +183,9 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont sessionEventsManager = new SessionEventListenerManagerImpl( customSessionEventListener.toArray( new SessionEventListener[0] ) ); } - final StatementInspector statementInspector = interpret( options.getStatementInspector() ); - this.jdbcSessionContext = new JdbcSessionContextImpl( this, statementInspector, fastSessionServices ); - this.entityNameResolver = new CoordinatingEntityNameResolver( factory, interceptor ); + final StatementInspector statementInspector = interpret( options.getStatementInspector() ); if ( options instanceof SharedSessionCreationOptions && ( (SharedSessionCreationOptions) options ).isTransactionCoordinatorShared() ) { if ( options.getConnection() != null ) { throw new SessionException( "Cannot simultaneously share transaction context and specify connection" ); @@ -207,18 +207,27 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont ); autoJoinTransactions = false; } - if ( sharedOptions.getPhysicalConnectionHandlingMode() != this.jdbcCoordinator.getLogicalConnection().getConnectionHandlingMode() ) { + this.connectionHandlingMode = this.jdbcCoordinator.getLogicalConnection().getConnectionHandlingMode(); + if ( sharedOptions.getPhysicalConnectionHandlingMode() != this.connectionHandlingMode ) { log.debug( "Session creation specified 'PhysicalConnectionHandlingMode which is invalid in conjunction " + "with sharing JDBC connection between sessions; ignoring" ); } + this.jdbcSessionContext = new JdbcSessionContextImpl( this, statementInspector, + connectionHandlingMode, fastSessionServices ); + addSharedSessionTransactionObserver( transactionCoordinator ); } else { this.isTransactionCoordinatorShared = false; this.autoJoinTransactions = options.shouldAutoJoinTransactions(); + this.connectionHandlingMode = options.getPhysicalConnectionHandlingMode(); + this.jdbcSessionContext = new JdbcSessionContextImpl( this, statementInspector, + connectionHandlingMode, fastSessionServices ); + // This must happen *after* the JdbcSessionContext was initialized, + // because some of the calls below retrieve this context indirectly through Session getters. this.jdbcCoordinator = new JdbcCoordinatorImpl( options.getConnection(), this, fastSessionServices.jdbcServices ); this.transactionCoordinator = fastSessionServices.transactionCoordinatorBuilder.buildTransactionCoordinator( jdbcCoordinator, this ); } @@ -1236,7 +1245,8 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont factory = SessionFactoryImpl.deserialize( ois ); fastSessionServices = factory.getFastSessionServices(); sessionEventsManager = new SessionEventListenerManagerImpl( fastSessionServices.defaultSessionEventListeners.buildBaseline() ); - jdbcSessionContext = new JdbcSessionContextImpl( this, (StatementInspector) ois.readObject(), fastSessionServices ); + jdbcSessionContext = new JdbcSessionContextImpl( this, (StatementInspector) ois.readObject(), + connectionHandlingMode, fastSessionServices ); jdbcCoordinator = JdbcCoordinatorImpl.deserialize( ois, this ); cacheTransactionSync = factory.getCache().getRegionFactory().createTransactionContext( this ); diff --git a/hibernate-core/src/main/java/org/hibernate/internal/JdbcSessionContextImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/JdbcSessionContextImpl.java index 954593d6d5..36d16b05af 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/JdbcSessionContextImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/JdbcSessionContextImpl.java @@ -31,10 +31,11 @@ public class JdbcSessionContextImpl implements JdbcSessionContext { public JdbcSessionContextImpl( SharedSessionContractImplementor session, StatementInspector statementInspector, + PhysicalConnectionHandlingMode connectionHandlingMode, FastSessionServices fastSessionServices) { this.sessionFactory = session.getFactory(); this.statementInspector = statementInspector; - this.connectionHandlingMode = settings().getPhysicalConnectionHandlingMode(); + this.connectionHandlingMode = connectionHandlingMode; this.serviceRegistry = sessionFactory.getServiceRegistry(); this.jdbcObserver = new JdbcObserverImpl( session, fastSessionServices ); diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java index bbaf51baae..4f22a6a143 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -1473,7 +1473,7 @@ public class SessionFactoryImpl implements SessionFactoryImplementor { @Override public PhysicalConnectionHandlingMode getPhysicalConnectionHandlingMode() { - return null; + return sessionFactory.getSessionFactoryOptions().getPhysicalConnectionHandlingMode(); } @Override