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.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 );

View File

@ -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 );

View File

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