Skip to content

Commit 921bf53

Browse files
committed
extract two methods in JTASessionContext
1 parent 0ba882b commit 921bf53

File tree

1 file changed

+38
-32
lines changed

1 file changed

+38
-32
lines changed

hibernate-core/src/main/java/org/hibernate/context/internal/JTASessionContext.java

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import jakarta.transaction.Synchronization;
1010
import jakarta.transaction.Transaction;
1111

12+
import jakarta.transaction.TransactionManager;
13+
import org.checkerframework.checker.nullness.qual.NonNull;
1214
import org.hibernate.FlushMode;
1315
import org.hibernate.HibernateException;
1416
import org.hibernate.Session;
@@ -62,53 +64,57 @@ public Session currentSession() throws HibernateException {
6264
throw new HibernateException( "No TransactionManagerLookup specified" );
6365
}
6466

65-
Transaction txn;
67+
final var txn = getTransaction( transactionManager );
68+
final Object txnIdentifier = jtaPlatform.getTransactionIdentifier( txn );
69+
70+
Session currentSession = currentSessionMap.get( txnIdentifier );
71+
if ( currentSession == null ) {
72+
currentSession = buildOrObtainSession();
73+
registerSynchronization( txn, txnIdentifier, currentSession );
74+
currentSessionMap.put( txnIdentifier, currentSession );
75+
}
76+
else {
77+
validateExistingSession( currentSession );
78+
}
79+
80+
return currentSession;
81+
}
82+
83+
private void registerSynchronization(Transaction txn, Object txnIdentifier, Session currentSession) {
84+
try {
85+
txn.registerSynchronization( buildCleanupSynch( txnIdentifier ) );
86+
}
87+
catch ( Throwable t ) {
88+
try {
89+
currentSession.close();
90+
}
91+
catch ( Throwable e ) {
92+
CURRENT_SESSION_LOGGER.unableToReleaseGeneratedCurrentSessionOnFailedSynchronizationRegistration(e);
93+
}
94+
throw new HibernateException( "Unable to register cleanup Synchronization with TransactionManager" );
95+
}
96+
}
97+
98+
private static @NonNull Transaction getTransaction(TransactionManager transactionManager) {
6699
try {
67-
txn = transactionManager.getTransaction();
68-
if ( txn == null ) {
100+
final var transaction = transactionManager.getTransaction();
101+
if ( transaction == null ) {
69102
throw new HibernateException( "Unable to locate current JTA transaction" );
70103
}
71-
if ( !isActive( txn.getStatus() ) ) {
104+
if ( !isActive( transaction.getStatus() ) ) {
72105
// We could register the session against the transaction even though it is
73106
// not started, but we'd have no guarantee of ever getting the map
74107
// entries cleaned up (aside from spawning threads).
75108
throw new HibernateException( "Current transaction is not in progress" );
76109
}
110+
return transaction;
77111
}
78112
catch ( HibernateException e ) {
79113
throw e;
80114
}
81115
catch ( Throwable t ) {
82116
throw new HibernateException( "Problem locating/validating JTA transaction", t );
83117
}
84-
85-
final Object txnIdentifier = jtaPlatform.getTransactionIdentifier( txn );
86-
87-
Session currentSession = currentSessionMap.get( txnIdentifier );
88-
89-
if ( currentSession == null ) {
90-
currentSession = buildOrObtainSession();
91-
92-
try {
93-
txn.registerSynchronization( buildCleanupSynch( txnIdentifier ) );
94-
}
95-
catch ( Throwable t ) {
96-
try {
97-
currentSession.close();
98-
}
99-
catch ( Throwable e ) {
100-
CURRENT_SESSION_LOGGER.unableToReleaseGeneratedCurrentSessionOnFailedSynchronizationRegistration(e);
101-
}
102-
throw new HibernateException( "Unable to register cleanup Synchronization with TransactionManager" );
103-
}
104-
105-
currentSessionMap.put( txnIdentifier, currentSession );
106-
}
107-
else {
108-
validateExistingSession( currentSession );
109-
}
110-
111-
return currentSession;
112118
}
113119

114120
/**

0 commit comments

Comments
 (0)