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 // be created even if a current session and transaction are
// open (ex: session.clear() was used). We must prevent // open (ex: session.clear() was used). We must prevent
// multiple transactions. // multiple transactions.
( (Session) session ).beginTransaction(); session.beginTransaction();
} }
session.getPersistenceContextInternal().addUninitializedDetachedCollection( session.getPersistenceContextInternal().addUninitializedDetachedCollection(
@ -265,20 +265,26 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
finally { finally {
if ( tempSession != null ) { if ( tempSession != null ) {
// make sure the just opened temp session gets closed! // make sure the just opened temp session gets closed!
isTempSession = false; isTempSession = false;
session = originalSession; session = originalSession;
try { try {
if ( !isJTA ) { if ( !isJTA ) {
( (Session) tempSession ).getTransaction().commit(); tempSession.getTransaction().commit();
} }
( (Session) tempSession ).close(); tempSession.close();
} }
catch (Exception e) { catch (Exception e) {
LOG.warn( "Unable to close temporary session used to load lazy collection associated to no session" ); 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 @Override
public final void initialize() throws HibernateException { public final void initialize() throws HibernateException {
if ( !initialized ) { if ( !initialized ) {
try {
if ( allowLoadOutsideTransaction ) { if ( allowLoadOutsideTransaction ) {
permissiveInitialization(); permissiveInitialization();
} }
@ -178,7 +179,13 @@ public abstract class AbstractLazyInitializer implements LazyInitializer {
else { else {
target = session.immediateLoad( entityName, id ); target = session.immediateLoad( entityName, id );
initialized = true; initialized = true;
checkTargetState(session); checkTargetState( session );
}
}
finally {
if ( session != null && !session.isTransactionInProgress() ) {
session.getJdbcCoordinator().afterTransaction();
}
} }
} }
else { else {