HHH-4808 SessionImpl.initializeCollection() does not release JDBC connection (if outside of a transaction)

This commit is contained in:
Andrea Boriero 2018-07-05 11:07:38 +01:00 committed by Sanne Grinovero
parent 179c1d1da0
commit 3ea0484122
3 changed files with 32 additions and 19 deletions

View File

@ -251,7 +251,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
// be created even if a current session and transaction are
// open (ex: session.clear() was used). We must prevent
// multiple transactions.
( (Session) session ).beginTransaction();
session.beginTransaction();
}
session.getPersistenceContextInternal().addUninitializedDetachedCollection(
@ -265,20 +265,26 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
}
finally {
if ( tempSession != null ) {
// make sure the just opened temp session gets closed!
isTempSession = false;
session = originalSession;
try {
if ( !isJTA ) {
( (Session) tempSession ).getTransaction().commit();
tempSession.getTransaction().commit();
}
( (Session) tempSession ).close();
tempSession.close();
}
catch (Exception e) {
LOG.warn( "Unable to close temporary session used to load lazy collection associated to no session" );
}
}
else {
if ( !session.isTransactionInProgress() ) {
session.getJdbcCoordinator().afterTransaction();
}
}
}
}

View File

@ -163,6 +163,7 @@ public abstract class AbstractLazyInitializer implements LazyInitializer {
@Override
public final void initialize() throws HibernateException {
if ( !initialized ) {
try {
if ( allowLoadOutsideTransaction ) {
permissiveInitialization();
}
@ -181,6 +182,12 @@ public abstract class AbstractLazyInitializer implements LazyInitializer {
checkTargetState( session );
}
}
finally {
if ( session != null && !session.isTransactionInProgress() ) {
session.getJdbcCoordinator().afterTransaction();
}
}
}
else {
checkTargetState(session);
}