Skip to content

Commit

Permalink
HHH-7674 DB locks not cleared on
Browse files Browse the repository at this point in the history
LazyLoadingTest#testLazyCollectionLoadingWithClearedSession
  • Loading branch information
brmeyer committed Oct 11, 2012
1 parent c9fd71f commit 25a98fa
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Expand Up @@ -23,7 +23,6 @@
*/ */
package org.hibernate.collection.internal; package org.hibernate.collection.internal;


import javax.naming.NamingException;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
Expand All @@ -33,7 +32,7 @@
import java.util.List; import java.util.List;
import java.util.ListIterator; import java.util.ListIterator;


import org.jboss.logging.Logger; import javax.naming.NamingException;


import org.hibernate.AssertionFailure; import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
Expand All @@ -55,6 +54,7 @@
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.pretty.MessageHelper; import org.hibernate.pretty.MessageHelper;
import org.hibernate.type.Type; import org.hibernate.type.Type;
import org.jboss.logging.Logger;


/** /**
* Base class implementing {@link org.hibernate.collection.spi.PersistentCollection} * Base class implementing {@link org.hibernate.collection.spi.PersistentCollection}
Expand Down Expand Up @@ -175,6 +175,7 @@ public static interface LazyInitializationWork<T> {
private <T> T withTemporarySessionIfNeeded(LazyInitializationWork<T> lazyInitializationWork) { private <T> T withTemporarySessionIfNeeded(LazyInitializationWork<T> lazyInitializationWork) {
SessionImplementor originalSession = null; SessionImplementor originalSession = null;
boolean isTempSession = false; boolean isTempSession = false;
boolean isJTA = false;


if ( session == null ) { if ( session == null ) {
if ( specjLazyLoad ) { if ( specjLazyLoad ) {
Expand Down Expand Up @@ -207,6 +208,22 @@ else if ( !session.isConnected() ) {
} }


if ( isTempSession ) { if ( isTempSession ) {
// TODO: On the next major release, add an
// 'isJTA' or 'getTransactionFactory' method to Session.
isJTA = session.getTransactionCoordinator()
.getTransactionContext().getTransactionEnvironment()
.getTransactionFactory()
.compatibleWithJtaSynchronization();

if ( !isJTA ) {
// Explicitly handle the transactions only if we're not in
// a JTA environment. A lazy loading temporary session can
// 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.getPersistenceContext().addUninitializedDetachedCollection( session.getPersistenceContext().addUninitializedDetachedCollection(
session.getFactory().getCollectionPersister( getRole() ), session.getFactory().getCollectionPersister( getRole() ),
this this
Expand All @@ -220,6 +237,9 @@ else if ( !session.isConnected() ) {
if ( isTempSession ) { if ( isTempSession ) {
// make sure the just opened temp session gets closed! // make sure the just opened temp session gets closed!
try { try {
if ( !isJTA ) {
( ( Session) session ).getTransaction().commit();
}
( (Session) session ).close(); ( (Session) session ).close();
} }
catch (Exception e) { catch (Exception e) {
Expand Down
Expand Up @@ -23,10 +23,9 @@
*/ */
package org.hibernate.proxy; package org.hibernate.proxy;


import javax.naming.NamingException;
import java.io.Serializable; import java.io.Serializable;


import org.jboss.logging.Logger; import javax.naming.NamingException;


import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.LazyInitializationException; import org.hibernate.LazyInitializationException;
Expand All @@ -38,6 +37,7 @@
import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.internal.SessionFactoryRegistry; import org.hibernate.internal.SessionFactoryRegistry;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
import org.jboss.logging.Logger;


/** /**
* Convenience base class for lazy initialization handlers. Centralizes the basic plumbing of doing lazy * Convenience base class for lazy initialization handlers. Centralizes the basic plumbing of doing lazy
Expand Down Expand Up @@ -190,13 +190,32 @@ protected void specialSpecjInitialization() {
SessionFactoryImplementor sf = (SessionFactoryImplementor) SessionFactoryImplementor sf = (SessionFactoryImplementor)
SessionFactoryRegistry.INSTANCE.getSessionFactory( sessionFactoryUuid ); SessionFactoryRegistry.INSTANCE.getSessionFactory( sessionFactoryUuid );
SessionImplementor session = (SessionImplementor) sf.openSession(); SessionImplementor session = (SessionImplementor) sf.openSession();

// TODO: On the next major release, add an
// 'isJTA' or 'getTransactionFactory' method to Session.
boolean isJTA = session.getTransactionCoordinator()
.getTransactionContext().getTransactionEnvironment()
.getTransactionFactory()
.compatibleWithJtaSynchronization();

if ( !isJTA ) {
// Explicitly handle the transactions only if we're not in
// a JTA environment. A lazy loading temporary session can
// be created even if a current session and transaction are
// open (ex: session.clear() was used). We must prevent
// multiple transactions.
( ( Session) session ).beginTransaction();
}


try { try {
target = session.immediateLoad( entityName, id ); target = session.immediateLoad( entityName, id );
} }
finally { finally {
// make sure the just opened temp session gets closed! // make sure the just opened temp session gets closed!
try { try {
if ( !isJTA ) {
( ( Session) session ).getTransaction().commit();
}
( (Session) session ).close(); ( (Session) session ).close();
} }
catch (Exception e) { catch (Exception e) {
Expand Down

0 comments on commit 25a98fa

Please sign in to comment.