diff --git a/hibernate-core/src/main/java/org/hibernate/collection/internal/AbstractPersistentCollection.java b/hibernate-core/src/main/java/org/hibernate/collection/internal/AbstractPersistentCollection.java index 43d5c2482c..ed755bb7a2 100644 --- a/hibernate-core/src/main/java/org/hibernate/collection/internal/AbstractPersistentCollection.java +++ b/hibernate-core/src/main/java/org/hibernate/collection/internal/AbstractPersistentCollection.java @@ -201,7 +201,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers throwLazyInitializationException( "could not initialize proxy - no Session" ); } } - else if ( !session.isOpen() ) { + else if ( !session.isOpenOrWaitingForAutoClose() ) { if ( allowLoadOutsideTransaction ) { tempSession = openTemporarySessionForLoading(); } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java index 1ae03910a6..16f7bf9d06 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java @@ -403,6 +403,16 @@ public class SessionDelegatorBaseImpl implements SessionImplementor { return delegate.isClosed(); } + @Override + public void checkOpen() { + delegate.checkOpen(); + } + + @Override + public boolean isOpenOrWaitingForAutoClose() { + return delegate.isOpenOrWaitingForAutoClose(); + } + @Override public boolean shouldAutoClose() { return delegate.shouldAutoClose(); diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/SharedSessionContractImplementor.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/SharedSessionContractImplementor.java index b6d3756bb1..4b06dea3cc 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/SharedSessionContractImplementor.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/SharedSessionContractImplementor.java @@ -116,6 +116,15 @@ public interface SharedSessionContractImplementor */ boolean isClosed(); + /** + * Checks whether the session is open or is waiting for auto-close + * + * @return {@code true} if the session is closed or if it's waiting for auto-close; {@code false} otherwise. + */ + default boolean isOpenOrWaitingForAutoClose() { + return !isClosed(); + } + /** * Performs a check whether the Session is open, and if not: