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

@ -3380,7 +3380,7 @@ public class SessionImpl
if ( getLoadQueryInfluencers().getEffectiveEntityGraph().getSemantic() == GraphSemantic.FETCH ) {
setEnforcingFetchGraph( true );
}
return loadAccess.load( (Serializable) primaryKey );
}
catch ( EntityNotFoundException ignored ) {

View File

@ -163,22 +163,29 @@ public abstract class AbstractLazyInitializer implements LazyInitializer {
@Override
public final void initialize() throws HibernateException {
if ( !initialized ) {
if ( allowLoadOutsideTransaction ) {
permissiveInitialization();
try {
if ( allowLoadOutsideTransaction ) {
permissiveInitialization();
}
else if ( session == null ) {
throw new LazyInitializationException( "could not initialize proxy [" + entityName + "#" + id + "] - no Session" );
}
else if ( !session.isOpenOrWaitingForAutoClose() ) {
throw new LazyInitializationException( "could not initialize proxy [" + entityName + "#" + id + "] - the owning Session was closed" );
}
else if ( !session.isConnected() ) {
throw new LazyInitializationException( "could not initialize proxy [" + entityName + "#" + id + "] - the owning Session is disconnected" );
}
else {
target = session.immediateLoad( entityName, id );
initialized = true;
checkTargetState( session );
}
}
else if ( session == null ) {
throw new LazyInitializationException( "could not initialize proxy [" + entityName + "#" + id + "] - no Session" );
}
else if ( !session.isOpenOrWaitingForAutoClose() ) {
throw new LazyInitializationException( "could not initialize proxy [" + entityName + "#" + id + "] - the owning Session was closed" );
}
else if ( !session.isConnected() ) {
throw new LazyInitializationException( "could not initialize proxy [" + entityName + "#" + id + "] - the owning Session is disconnected" );
}
else {
target = session.immediateLoad( entityName, id );
initialized = true;
checkTargetState(session);
finally {
if ( session != null && !session.isTransactionInProgress() ) {
session.getJdbcCoordinator().afterTransaction();
}
}
}
else {