Permalink
Browse files

HHH-7674 DB locks not cleared on

LazyLoadingTest#testLazyCollectionLoadingWithClearedSession
  • Loading branch information...
1 parent c9fd71f commit 25a98fada3ad44b352b52eaa740f7279e1e0e2a8 brmeyer committed Oct 11, 2012
@@ -23,7 +23,6 @@
*/
package org.hibernate.collection.internal;
-import javax.naming.NamingException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
@@ -33,7 +32,7 @@
import java.util.List;
import java.util.ListIterator;
-import org.jboss.logging.Logger;
+import javax.naming.NamingException;
import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
@@ -55,6 +54,7 @@
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.pretty.MessageHelper;
import org.hibernate.type.Type;
+import org.jboss.logging.Logger;
/**
* Base class implementing {@link org.hibernate.collection.spi.PersistentCollection}
@@ -175,6 +175,7 @@ public Boolean doWork() {
private <T> T withTemporarySessionIfNeeded(LazyInitializationWork<T> lazyInitializationWork) {
SessionImplementor originalSession = null;
boolean isTempSession = false;
+ boolean isJTA = false;
if ( session == null ) {
if ( specjLazyLoad ) {
@@ -207,6 +208,22 @@ else if ( !session.isConnected() ) {
}
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.getFactory().getCollectionPersister( getRole() ),
this
@@ -220,6 +237,9 @@ else if ( !session.isConnected() ) {
if ( isTempSession ) {
// make sure the just opened temp session gets closed!
try {
+ if ( !isJTA ) {
+ ( ( Session) session ).getTransaction().commit();
+ }
( (Session) session ).close();
}
catch (Exception e) {
@@ -23,10 +23,9 @@
*/
package org.hibernate.proxy;
-import javax.naming.NamingException;
import java.io.Serializable;
-import org.jboss.logging.Logger;
+import javax.naming.NamingException;
import org.hibernate.HibernateException;
import org.hibernate.LazyInitializationException;
@@ -38,6 +37,7 @@
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.internal.SessionFactoryRegistry;
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
@@ -190,13 +190,32 @@ protected void specialSpecjInitialization() {
SessionFactoryImplementor sf = (SessionFactoryImplementor)
SessionFactoryRegistry.INSTANCE.getSessionFactory( sessionFactoryUuid );
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 {
target = session.immediateLoad( entityName, id );
}
finally {
// make sure the just opened temp session gets closed!
try {
+ if ( !isJTA ) {
+ ( ( Session) session ).getTransaction().commit();
+ }
( (Session) session ).close();
}
catch (Exception e) {

0 comments on commit 25a98fa

Please sign in to comment.