Skip to content

Commit d0b44c4

Browse files
yrodiereSanne
authored andcommitted
HHH-14404 Take into account the connectionHandlingMode passed through SessionBuilder
Signed-off-by: Yoann Rodière <[email protected]>
1 parent d726dcb commit d0b44c4

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
import org.hibernate.query.spi.QueryImplementor;
8787
import org.hibernate.query.spi.ScrollableResultsImplementor;
8888
import org.hibernate.resource.jdbc.spi.JdbcSessionContext;
89+
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
8990
import org.hibernate.resource.jdbc.spi.StatementInspector;
9091
import org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl;
9192
import org.hibernate.resource.transaction.spi.TransactionCoordinator;
@@ -134,6 +135,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
134135

135136
private FlushMode flushMode;
136137
private boolean autoJoinTransactions;
138+
private final PhysicalConnectionHandlingMode connectionHandlingMode;
137139

138140
private CacheMode cacheMode;
139141

@@ -181,11 +183,9 @@ public AbstractSharedSessionContract(SessionFactoryImpl factory, SessionCreation
181183
sessionEventsManager = new SessionEventListenerManagerImpl( customSessionEventListener.toArray( new SessionEventListener[0] ) );
182184
}
183185

184-
final StatementInspector statementInspector = interpret( options.getStatementInspector() );
185-
this.jdbcSessionContext = new JdbcSessionContextImpl( this, statementInspector, fastSessionServices );
186-
187186
this.entityNameResolver = new CoordinatingEntityNameResolver( factory, interceptor );
188187

188+
final StatementInspector statementInspector = interpret( options.getStatementInspector() );
189189
if ( options instanceof SharedSessionCreationOptions && ( (SharedSessionCreationOptions) options ).isTransactionCoordinatorShared() ) {
190190
if ( options.getConnection() != null ) {
191191
throw new SessionException( "Cannot simultaneously share transaction context and specify connection" );
@@ -207,18 +207,27 @@ public AbstractSharedSessionContract(SessionFactoryImpl factory, SessionCreation
207207
);
208208
autoJoinTransactions = false;
209209
}
210-
if ( sharedOptions.getPhysicalConnectionHandlingMode() != this.jdbcCoordinator.getLogicalConnection().getConnectionHandlingMode() ) {
210+
this.connectionHandlingMode = this.jdbcCoordinator.getLogicalConnection().getConnectionHandlingMode();
211+
if ( sharedOptions.getPhysicalConnectionHandlingMode() != this.connectionHandlingMode ) {
211212
log.debug(
212213
"Session creation specified 'PhysicalConnectionHandlingMode which is invalid in conjunction " +
213214
"with sharing JDBC connection between sessions; ignoring"
214215
);
215216
}
216217

218+
this.jdbcSessionContext = new JdbcSessionContextImpl( this, statementInspector,
219+
connectionHandlingMode, fastSessionServices );
220+
217221
addSharedSessionTransactionObserver( transactionCoordinator );
218222
}
219223
else {
220224
this.isTransactionCoordinatorShared = false;
221225
this.autoJoinTransactions = options.shouldAutoJoinTransactions();
226+
this.connectionHandlingMode = options.getPhysicalConnectionHandlingMode();
227+
this.jdbcSessionContext = new JdbcSessionContextImpl( this, statementInspector,
228+
connectionHandlingMode, fastSessionServices );
229+
// This must happen *after* the JdbcSessionContext was initialized,
230+
// because some of the calls below retrieve this context indirectly through Session getters.
222231
this.jdbcCoordinator = new JdbcCoordinatorImpl( options.getConnection(), this, fastSessionServices.jdbcServices );
223232
this.transactionCoordinator = fastSessionServices.transactionCoordinatorBuilder.buildTransactionCoordinator( jdbcCoordinator, this );
224233
}
@@ -1236,7 +1245,8 @@ private void readObject(ObjectInputStream ois) throws IOException, ClassNotFound
12361245
factory = SessionFactoryImpl.deserialize( ois );
12371246
fastSessionServices = factory.getFastSessionServices();
12381247
sessionEventsManager = new SessionEventListenerManagerImpl( fastSessionServices.defaultSessionEventListeners.buildBaseline() );
1239-
jdbcSessionContext = new JdbcSessionContextImpl( this, (StatementInspector) ois.readObject(), fastSessionServices );
1248+
jdbcSessionContext = new JdbcSessionContextImpl( this, (StatementInspector) ois.readObject(),
1249+
connectionHandlingMode, fastSessionServices );
12401250
jdbcCoordinator = JdbcCoordinatorImpl.deserialize( ois, this );
12411251

12421252
cacheTransactionSync = factory.getCache().getRegionFactory().createTransactionContext( this );

hibernate-core/src/main/java/org/hibernate/internal/JdbcSessionContextImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ public class JdbcSessionContextImpl implements JdbcSessionContext {
3131
public JdbcSessionContextImpl(
3232
SharedSessionContractImplementor session,
3333
StatementInspector statementInspector,
34+
PhysicalConnectionHandlingMode connectionHandlingMode,
3435
FastSessionServices fastSessionServices) {
3536
this.sessionFactory = session.getFactory();
3637
this.statementInspector = statementInspector;
37-
this.connectionHandlingMode = settings().getPhysicalConnectionHandlingMode();
38+
this.connectionHandlingMode = connectionHandlingMode;
3839
this.serviceRegistry = sessionFactory.getServiceRegistry();
3940
this.jdbcObserver = new JdbcObserverImpl( session, fastSessionServices );
4041

hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1473,7 +1473,7 @@ public StatementInspector getStatementInspector() {
14731473

14741474
@Override
14751475
public PhysicalConnectionHandlingMode getPhysicalConnectionHandlingMode() {
1476-
return null;
1476+
return sessionFactory.getSessionFactoryOptions().getPhysicalConnectionHandlingMode();
14771477
}
14781478

14791479
@Override

0 commit comments

Comments
 (0)