From 7780b74c21d064e78f085f392b9b45d88f575742 Mon Sep 17 00:00:00 2001 From: Dmitriy Churilov Date: Fri, 6 Dec 2024 11:05:31 +0100 Subject: [PATCH] HHH-13790 Temporary session not being closed --- .../spi/AbstractPersistentCollection.java | 47 +++++++++---------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java b/hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java index 1b243e20953..ff2dd2b7788 100644 --- a/hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java +++ b/hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java @@ -238,31 +238,30 @@ else if ( !session.isConnected() ) { SharedSessionContractImplementor originalSession = null; boolean isJTA = false; - - if ( tempSession != null ) { - isTempSession = true; - originalSession = session; - session = tempSession; - - isJTA = session.getTransactionCoordinator().getTransactionCoordinatorBuilder().isJta(); - - if ( !isJTA ) { - // Explicitly handle the transactions only if we're not in - // a JTA environment. A lazy loading temporary session can - // be created even if a current session and transaction are - // open (ex: session.clear() was used). We must prevent - // multiple transactions. - session.beginTransaction(); - } - - final CollectionPersister collectionDescriptor = - session.getSessionFactory() - .getMappingMetamodel() - .getCollectionDescriptor( getRole() ); - session.getPersistenceContextInternal().addUninitializedDetachedCollection( collectionDescriptor, this ); - } - try { + if ( tempSession != null ) { + isTempSession = true; + originalSession = session; + session = tempSession; + + isJTA = session.getTransactionCoordinator().getTransactionCoordinatorBuilder().isJta(); + + if ( !isJTA ) { + // Explicitly handle the transactions only if we're not in + // a JTA environment. A lazy loading temporary session can + // be created even if a current session and transaction are + // open (ex: session.clear() was used). We must prevent + // multiple transactions. + session.beginTransaction(); + } + + final CollectionPersister collectionDescriptor = + session.getSessionFactory() + .getMappingMetamodel() + .getCollectionDescriptor( getRole() ); + session.getPersistenceContextInternal() + .addUninitializedDetachedCollection( collectionDescriptor, this ); + } return lazyInitializationWork.doWork(); } finally {