HHH-14404 Take into account the connectionHandlingMode passed through SessionBuilder

Signed-off-by: Yoann Rodière <yoann@hibernate.org>
This commit is contained in:
Yoann Rodière 2021-01-14 11:12:42 +01:00 committed by Sanne Grinovero
parent d726dcb394
commit d0b44c48ef
3 changed files with 18 additions and 7 deletions

View File

@ -86,6 +86,7 @@ import org.hibernate.query.spi.NativeQueryImplementor;
import org.hibernate.query.spi.QueryImplementor; import org.hibernate.query.spi.QueryImplementor;
import org.hibernate.query.spi.ScrollableResultsImplementor; import org.hibernate.query.spi.ScrollableResultsImplementor;
import org.hibernate.resource.jdbc.spi.JdbcSessionContext; 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.jdbc.spi.StatementInspector;
import org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl; import org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl;
import org.hibernate.resource.transaction.spi.TransactionCoordinator; import org.hibernate.resource.transaction.spi.TransactionCoordinator;
@ -134,6 +135,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
private FlushMode flushMode; private FlushMode flushMode;
private boolean autoJoinTransactions; private boolean autoJoinTransactions;
private final PhysicalConnectionHandlingMode connectionHandlingMode;
private CacheMode cacheMode; private CacheMode cacheMode;
@ -181,11 +183,9 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
sessionEventsManager = new SessionEventListenerManagerImpl( customSessionEventListener.toArray( new SessionEventListener[0] ) ); 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 ); this.entityNameResolver = new CoordinatingEntityNameResolver( factory, interceptor );
final StatementInspector statementInspector = interpret( options.getStatementInspector() );
if ( options instanceof SharedSessionCreationOptions && ( (SharedSessionCreationOptions) options ).isTransactionCoordinatorShared() ) { if ( options instanceof SharedSessionCreationOptions && ( (SharedSessionCreationOptions) options ).isTransactionCoordinatorShared() ) {
if ( options.getConnection() != null ) { if ( options.getConnection() != null ) {
throw new SessionException( "Cannot simultaneously share transaction context and specify connection" ); throw new SessionException( "Cannot simultaneously share transaction context and specify connection" );
@ -207,18 +207,27 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
); );
autoJoinTransactions = false; autoJoinTransactions = false;
} }
if ( sharedOptions.getPhysicalConnectionHandlingMode() != this.jdbcCoordinator.getLogicalConnection().getConnectionHandlingMode() ) { this.connectionHandlingMode = this.jdbcCoordinator.getLogicalConnection().getConnectionHandlingMode();
if ( sharedOptions.getPhysicalConnectionHandlingMode() != this.connectionHandlingMode ) {
log.debug( log.debug(
"Session creation specified 'PhysicalConnectionHandlingMode which is invalid in conjunction " + "Session creation specified 'PhysicalConnectionHandlingMode which is invalid in conjunction " +
"with sharing JDBC connection between sessions; ignoring" "with sharing JDBC connection between sessions; ignoring"
); );
} }
this.jdbcSessionContext = new JdbcSessionContextImpl( this, statementInspector,
connectionHandlingMode, fastSessionServices );
addSharedSessionTransactionObserver( transactionCoordinator ); addSharedSessionTransactionObserver( transactionCoordinator );
} }
else { else {
this.isTransactionCoordinatorShared = false; this.isTransactionCoordinatorShared = false;
this.autoJoinTransactions = options.shouldAutoJoinTransactions(); 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.jdbcCoordinator = new JdbcCoordinatorImpl( options.getConnection(), this, fastSessionServices.jdbcServices );
this.transactionCoordinator = fastSessionServices.transactionCoordinatorBuilder.buildTransactionCoordinator( jdbcCoordinator, this ); this.transactionCoordinator = fastSessionServices.transactionCoordinatorBuilder.buildTransactionCoordinator( jdbcCoordinator, this );
} }
@ -1236,7 +1245,8 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
factory = SessionFactoryImpl.deserialize( ois ); factory = SessionFactoryImpl.deserialize( ois );
fastSessionServices = factory.getFastSessionServices(); fastSessionServices = factory.getFastSessionServices();
sessionEventsManager = new SessionEventListenerManagerImpl( fastSessionServices.defaultSessionEventListeners.buildBaseline() ); 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 ); jdbcCoordinator = JdbcCoordinatorImpl.deserialize( ois, this );
cacheTransactionSync = factory.getCache().getRegionFactory().createTransactionContext( this ); cacheTransactionSync = factory.getCache().getRegionFactory().createTransactionContext( this );

View File

@ -31,10 +31,11 @@ public class JdbcSessionContextImpl implements JdbcSessionContext {
public JdbcSessionContextImpl( public JdbcSessionContextImpl(
SharedSessionContractImplementor session, SharedSessionContractImplementor session,
StatementInspector statementInspector, StatementInspector statementInspector,
PhysicalConnectionHandlingMode connectionHandlingMode,
FastSessionServices fastSessionServices) { FastSessionServices fastSessionServices) {
this.sessionFactory = session.getFactory(); this.sessionFactory = session.getFactory();
this.statementInspector = statementInspector; this.statementInspector = statementInspector;
this.connectionHandlingMode = settings().getPhysicalConnectionHandlingMode(); this.connectionHandlingMode = connectionHandlingMode;
this.serviceRegistry = sessionFactory.getServiceRegistry(); this.serviceRegistry = sessionFactory.getServiceRegistry();
this.jdbcObserver = new JdbcObserverImpl( session, fastSessionServices ); this.jdbcObserver = new JdbcObserverImpl( session, fastSessionServices );

View File

@ -1473,7 +1473,7 @@ public class SessionFactoryImpl implements SessionFactoryImplementor {
@Override @Override
public PhysicalConnectionHandlingMode getPhysicalConnectionHandlingMode() { public PhysicalConnectionHandlingMode getPhysicalConnectionHandlingMode() {
return null; return sessionFactory.getSessionFactoryOptions().getPhysicalConnectionHandlingMode();
} }
@Override @Override