Skip to content

Commit

Permalink
HHH-4808 SessionImpl.initializeCollection() does not release JDBC con…
Browse files Browse the repository at this point in the history
…nection (if outside of a transaction)
  • Loading branch information
dreab8 authored and Sanne committed Jun 22, 2021
1 parent 179c1d1 commit 3ea0484
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ else if ( !session.isConnected() ) {
// 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(
Expand All @@ -265,20 +265,26 @@ else if ( !session.isConnected() ) {
}
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();
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3380,7 +3380,7 @@ public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode
if ( getLoadQueryInfluencers().getEffectiveEntityGraph().getSemantic() == GraphSemantic.FETCH ) {
setEnforcingFetchGraph( true );
}

return loadAccess.load( (Serializable) primaryKey );
}
catch ( EntityNotFoundException ignored ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,22 +163,29 @@ public final void unsetSession() {
@Override
public final void initialize() throws HibernateException {
if ( !initialized ) {
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" );
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 {
target = session.immediateLoad( entityName, id );
initialized = true;
checkTargetState(session);
finally {
if ( session != null && !session.isTransactionInProgress() ) {
session.getJdbcCoordinator().afterTransaction();
}
}
}
else {
Expand Down

0 comments on commit 3ea0484

Please sign in to comment.