8686import org .hibernate .query .spi .QueryImplementor ;
8787import org .hibernate .query .spi .ScrollableResultsImplementor ;
8888import org .hibernate .resource .jdbc .spi .JdbcSessionContext ;
89+ import org .hibernate .resource .jdbc .spi .PhysicalConnectionHandlingMode ;
8990import org .hibernate .resource .jdbc .spi .StatementInspector ;
9091import org .hibernate .resource .transaction .backend .jta .internal .JtaTransactionCoordinatorImpl ;
9192import 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 );
0 commit comments