Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import jakarta.transaction.Synchronization;
import jakarta.transaction.Transaction;

import jakarta.transaction.TransactionManager;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Session;
Expand Down Expand Up @@ -62,53 +64,57 @@ public Session currentSession() throws HibernateException {
throw new HibernateException( "No TransactionManagerLookup specified" );
}

Transaction txn;
final var txn = getTransaction( transactionManager );
final Object txnIdentifier = jtaPlatform.getTransactionIdentifier( txn );

Session currentSession = currentSessionMap.get( txnIdentifier );
if ( currentSession == null ) {
currentSession = buildOrObtainSession();
registerSynchronization( txn, txnIdentifier, currentSession );
currentSessionMap.put( txnIdentifier, currentSession );
}
else {
validateExistingSession( currentSession );
}

return currentSession;
}

private void registerSynchronization(Transaction txn, Object txnIdentifier, Session currentSession) {
try {
txn.registerSynchronization( buildCleanupSynch( txnIdentifier ) );
}
catch ( Throwable t ) {
try {
currentSession.close();
}
catch ( Throwable e ) {
CURRENT_SESSION_LOGGER.unableToReleaseGeneratedCurrentSessionOnFailedSynchronizationRegistration(e);
}
throw new HibernateException( "Unable to register cleanup Synchronization with TransactionManager" );
}
}

private static @NonNull Transaction getTransaction(TransactionManager transactionManager) {
try {
txn = transactionManager.getTransaction();
if ( txn == null ) {
final var transaction = transactionManager.getTransaction();
if ( transaction == null ) {
throw new HibernateException( "Unable to locate current JTA transaction" );
}
if ( !isActive( txn.getStatus() ) ) {
if ( !isActive( transaction.getStatus() ) ) {
// We could register the session against the transaction even though it is
// not started, but we'd have no guarantee of ever getting the map
// entries cleaned up (aside from spawning threads).
throw new HibernateException( "Current transaction is not in progress" );
}
return transaction;
}
catch ( HibernateException e ) {
throw e;
}
catch ( Throwable t ) {
throw new HibernateException( "Problem locating/validating JTA transaction", t );
}

final Object txnIdentifier = jtaPlatform.getTransactionIdentifier( txn );

Session currentSession = currentSessionMap.get( txnIdentifier );

if ( currentSession == null ) {
currentSession = buildOrObtainSession();

try {
txn.registerSynchronization( buildCleanupSynch( txnIdentifier ) );
}
catch ( Throwable t ) {
try {
currentSession.close();
}
catch ( Throwable e ) {
CURRENT_SESSION_LOGGER.unableToReleaseGeneratedCurrentSessionOnFailedSynchronizationRegistration(e);
}
throw new HibernateException( "Unable to register cleanup Synchronization with TransactionManager" );
}

currentSessionMap.put( txnIdentifier, currentSession );
}
else {
validateExistingSession( currentSession );
}

return currentSession;
}

/**
Expand Down
Loading