2828import org .hibernate .engine .spi .SessionFactoryImplementor ;
2929import org .hibernate .engine .spi .SessionImplementor ;
3030import org .hibernate .event .spi .EventSource ;
31- import org .hibernate .internal .CoreLogging ;
32- import org .hibernate .internal .CoreMessageLogger ;
31+ import static org .hibernate .context .internal .CurrentSessionLogging .CURRENT_SESSION_LOGGER ;
3332import org .hibernate .resource .jdbc .spi .PhysicalConnectionHandlingMode ;
3433import org .hibernate .resource .transaction .spi .TransactionStatus ;
3534
5857 */
5958public class ThreadLocalSessionContext extends AbstractCurrentSessionContext {
6059
61- private static final CoreMessageLogger LOG = CoreLogging .messageLogger ( ThreadLocalSessionContext .class );
62-
6360 private static final Class <?>[] SESSION_PROXY_INTERFACES = new Class <?>[] {
6461 Session .class ,
6562 SessionImplementor .class ,
@@ -167,8 +164,8 @@ protected PhysicalConnectionHandlingMode getConnectionHandlingMode() {
167164 }
168165
169166 protected Session wrap (Session session ) {
170- final TransactionProtectionWrapper wrapper = new TransactionProtectionWrapper ( session );
171- final Session wrapped = (Session ) Proxy .newProxyInstance (
167+ final var wrapper = new TransactionProtectionWrapper ( session );
168+ final var wrapped = (Session ) Proxy .newProxyInstance (
172169 Session .class .getClassLoader (),
173170 SESSION_PROXY_INTERFACES ,
174171 wrapper
@@ -189,25 +186,20 @@ public static void bind(Session session) {
189186
190187 private static void terminateOrphanedSession (Session orphan ) {
191188 if ( orphan != null ) {
192- LOG .alreadySessionBound ();
193- try {
194- final Transaction orphanTransaction = orphan .getTransaction ();
189+ CURRENT_SESSION_LOGGER .alreadySessionBound ();
190+ try ( orphan ) {
191+ final var orphanTransaction = orphan .getTransaction ();
195192 if ( orphanTransaction != null && orphanTransaction .getStatus () == TransactionStatus .ACTIVE ) {
196193 try {
197194 orphanTransaction .rollback ();
198195 }
199- catch ( Throwable t ) {
200- LOG . debug ( "Unable to rollback transaction for orphaned session" , t );
196+ catch ( Throwable t ) {
197+ CURRENT_SESSION_LOGGER . unableToRollbackTransactionForOrphanedSession ( t );
201198 }
202199 }
203200 }
204- finally {
205- try {
206- orphan .close ();
207- }
208- catch ( Throwable t ) {
209- LOG .debug ( "Unable to close orphaned session" , t );
210- }
201+ catch (Throwable t ) {
202+ CURRENT_SESSION_LOGGER .unableToCloseOrphanedSession ( t );
211203 }
212204
213205 }
@@ -232,13 +224,13 @@ protected static Map<SessionFactory,Session> sessionMap() {
232224 }
233225
234226 private static void doBind (Session session , SessionFactory factory ) {
235- Session orphanedPreviousSession = sessionMap ().put ( factory , session );
227+ final var orphanedPreviousSession = sessionMap ().put ( factory , session );
236228 terminateOrphanedSession ( orphanedPreviousSession );
237229 }
238230
239231 private static Session doUnbind (SessionFactory factory ) {
240- final Map < SessionFactory , Session > sessionMap = sessionMap ();
241- final Session session = sessionMap .remove ( factory );
232+ final var sessionMap = sessionMap ();
233+ final var session = sessionMap .remove ( factory );
242234 if ( sessionMap .isEmpty () ) {
243235 //Do not use set(null) as it would prevent the initialValue to be invoked again in case of need.
244236 CONTEXT_TL .remove ();
@@ -288,10 +280,12 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
288280 return this .equals ( Proxy .getInvocationHandler ( args [0 ] ) );
289281 }
290282 else if ( "hashCode" .equals ( methodName ) && method .getParameterCount () == 0 ) {
291- return this . hashCode ();
283+ return hashCode ();
292284 }
293285 else if ( "toString" .equals ( methodName ) && method .getParameterCount () == 0 ) {
294- return String .format ( Locale .ROOT , "ThreadLocalSessionContext.TransactionProtectionWrapper[%s]" , realSession );
286+ return String .format ( Locale .ROOT ,
287+ "ThreadLocalSessionContext.TransactionProtectionWrapper[%s]" ,
288+ realSession );
295289 }
296290
297291
@@ -300,20 +294,21 @@ else if ( "toString".equals( methodName ) && method.getParameterCount() == 0 ) {
300294 // If close() is called, guarantee unbind()
301295 if ( "close" .equals ( methodName ) ) {
302296 unbind ( realSession .getSessionFactory () );
297+ CURRENT_SESSION_LOGGER .allowingInvocationToProceed (methodName );
303298 }
304299 else if ( "getStatistics" .equals ( methodName )
305300 || "isOpen" .equals ( methodName )
306301 || "getListeners" .equals ( methodName ) ) {
307302 // allow these to go through the real session no matter what
308- LOG . tracef ( "Allowing invocation [%s] to proceed to real session" , methodName );
303+ CURRENT_SESSION_LOGGER . allowingInvocationToProceed ( methodName );
309304 }
310305 else if ( !realSession .isOpen () ) {
311- // essentially, if the real session is closed allow any
312- // method call to pass through since the real session
313- // will complain by throwing an appropriate exception;
314- // NOTE that allowing close() above has the same basic effect,
315- // but we capture that there simply to doAfterTransactionCompletion the unbind.. .
316- LOG . tracef ( "Allowing invocation [%s] to proceed to real (closed) session" , methodName );
306+ // essentially, if the real session is closed, allow any method
307+ // call to pass through since the real session will complain by
308+ // throwing an appropriate exception; note that allowing close()
309+ // above has the same basic effect, but we capture that there
310+ // just to unbind() .
311+ CURRENT_SESSION_LOGGER . allowingInvocationToProceedToClosedSession ( methodName );
317312 }
318313 else if ( realSession .getTransaction ().getStatus () != TransactionStatus .ACTIVE ) {
319314 // limit the methods available if no transaction is active
@@ -326,14 +321,14 @@ else if ( realSession.getTransaction().getStatus() != TransactionStatus.ACTIVE )
326321 || "getSessionFactory" .equals ( methodName )
327322 || "getJdbcCoordinator" .equals ( methodName )
328323 || "getTenantIdentifier" .equals ( methodName ) ) {
329- LOG . tracef ( "Allowing invocation [%s] to proceed to real (non-transacted) session" , methodName );
324+ CURRENT_SESSION_LOGGER . allowingInvocationToProceedToNonTransactedSession ( methodName );
330325 }
331326 else {
332- throw new HibernateException ( "Calling method '" + methodName + "' is not valid without an active transaction (Current status: "
327+ throw new HibernateException ( "Calling method '" + methodName
328+ + "' is not valid without an active transaction (Current status: "
333329 + realSession .getTransaction ().getStatus () + ")" );
334330 }
335331 }
336- LOG .tracef ( "Allowing proxy invocation [%s] to proceed to real session" , methodName );
337332 return method .invoke ( realSession , args );
338333 }
339334 catch ( InvocationTargetException e ) {
@@ -370,7 +365,7 @@ private void writeObject(ObjectOutputStream oos) throws IOException {
370365 @ Serial
371366 private void readObject (ObjectInputStream ois ) throws IOException , ClassNotFoundException {
372367 // on the inverse, it makes sense that if a ThreadLocalSessionContext-
373- // bound session then gets deserialized to go ahead and re-bind it to
368+ // bound session then gets deserialized to go ahead and rebind it to
374369 // the ThreadLocalSessionContext session map.
375370 ois .defaultReadObject ();
376371 realSession .getTransaction ().registerSynchronization ( buildCleanupSynch () );
0 commit comments