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

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

View File

@ -163,22 +163,29 @@ public abstract class AbstractLazyInitializer implements LazyInitializer {
@Override @Override
public final void initialize() throws HibernateException { public final void initialize() throws HibernateException {
if ( !initialized ) { if ( !initialized ) {
if ( allowLoadOutsideTransaction ) { try {
permissiveInitialization(); 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 ) { finally {
throw new LazyInitializationException( "could not initialize proxy [" + entityName + "#" + id + "] - no Session" ); if ( session != null && !session.isTransactionInProgress() ) {
} session.getJdbcCoordinator().afterTransaction();
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 { else {