JBPAPP-1998 Added additional try/catch in AbstractEntityManagerImpl.wrapStaleStateException

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@16594 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Hardy Ferentschik 2009-05-19 09:55:46 +00:00
parent ea7c157ae7
commit 97a0919521
2 changed files with 102 additions and 84 deletions

View File

@ -11,7 +11,7 @@ import java.io.ObjectOutputStream;
import java.io.Serializable; import java.io.Serializable;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException; import javax.persistence.EntityNotFoundException;
import javax.persistence.EntityTransaction; import javax.persistence.EntityTransaction;
import javax.persistence.FlushModeType; import javax.persistence.FlushModeType;
@ -23,16 +23,18 @@ import javax.persistence.PersistenceContextType;
import javax.persistence.PersistenceException; import javax.persistence.PersistenceException;
import javax.persistence.Query; import javax.persistence.Query;
import javax.persistence.TransactionRequiredException; import javax.persistence.TransactionRequiredException;
import javax.persistence.EntityManagerFactory;
import javax.persistence.metamodel.Metamodel;
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.QueryBuilder; import javax.persistence.criteria.QueryBuilder;
import javax.persistence.metamodel.Metamodel;
import javax.persistence.spi.PersistenceUnitTransactionType; import javax.persistence.spi.PersistenceUnitTransactionType;
import javax.transaction.Status; import javax.transaction.Status;
import javax.transaction.Synchronization; import javax.transaction.Synchronization;
import javax.transaction.SystemException; import javax.transaction.SystemException;
import javax.transaction.TransactionManager; import javax.transaction.TransactionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.hibernate.AssertionFailure; import org.hibernate.AssertionFailure;
import org.hibernate.FlushMode; import org.hibernate.FlushMode;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
@ -58,8 +60,6 @@ import org.hibernate.proxy.HibernateProxy;
import org.hibernate.transaction.TransactionFactory; import org.hibernate.transaction.TransactionFactory;
import org.hibernate.util.CollectionHelper; import org.hibernate.util.CollectionHelper;
import org.hibernate.util.JTAHelper; import org.hibernate.util.JTAHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* @author <a href="mailto:gavin@hibernate.org">Gavin King</a> * @author <a href="mailto:gavin@hibernate.org">Gavin King</a>
@ -84,7 +84,9 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
protected void postInit() { protected void postInit() {
//register in Sync if needed //register in Sync if needed
if ( PersistenceUnitTransactionType.JTA.equals( transactionType ) ) joinTransaction( true ); if ( PersistenceUnitTransactionType.JTA.equals( transactionType ) ) {
joinTransaction( true );
}
Object flushMode = properties.get( "org.hibernate.flushMode" ); Object flushMode = properties.get( "org.hibernate.flushMode" );
if ( flushMode != null ) { if ( flushMode != null ) {
getSession().setFlushMode( ConfigurationHelper.getFlushMode( flushMode ) ); getSession().setFlushMode( ConfigurationHelper.getFlushMode( flushMode ) );
@ -369,6 +371,7 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
/** /**
* return a Session * return a Session
*
* @throws IllegalStateException if the entity manager is closed * @throws IllegalStateException if the entity manager is closed
*/ */
public abstract Session getSession(); public abstract Session getSession();
@ -457,7 +460,9 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
throw new TransactionRequiredException( "no transaction is in progress" ); throw new TransactionRequiredException( "no transaction is in progress" );
} }
//adjustFlushMode(); //adjustFlushMode();
if ( !contains( entity ) ) throw new IllegalArgumentException( "entity not in the persistence context" ); if ( !contains( entity ) ) {
throw new IllegalArgumentException( "entity not in the persistence context" );
}
getSession().lock( entity, getLockMode( lockMode ) ); getSession().lock( entity, getLockMode( lockMode ) );
} }
catch ( HibernateException he ) { catch ( HibernateException he ) {
@ -515,8 +520,12 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
} }
public <T> T unwrap(Class<T> clazz) { public <T> T unwrap(Class<T> clazz) {
if (clazz.equals( Session.class ) ) return (T) getSession(); if ( clazz.equals( Session.class ) ) {
if (clazz.equals( SessionImplementor.class ) ) return (T) getSession(); return ( T ) getSession();
}
if ( clazz.equals( SessionImplementor.class ) ) {
return ( T ) getSession();
}
//FIXME //FIXME
return null; //To change body of implemented methods use File | Settings | File Templates. return null; //To change body of implemented methods use File | Settings | File Templates.
} }
@ -550,8 +559,7 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
); );
} }
} }
else else if ( joinableCMTTransaction.getStatus() == JoinableCMTTransaction.JoinStatus.MARKED_FOR_JOINED ) {
if ( joinableCMTTransaction.getStatus() == JoinableCMTTransaction.JoinStatus.MARKED_FOR_JOINED ) {
throw new AssertionFailure( "Transaction MARKED_FOR_JOINED after isOpen() call" ); throw new AssertionFailure( "Transaction MARKED_FOR_JOINED after isOpen() call" );
} }
//flush before completion and //flush before completion and
@ -566,8 +574,11 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
ctx = ( TransactionFactory.Context ) session; ctx = ( TransactionFactory.Context ) session;
JoinableCMTTransaction joinable = ( JoinableCMTTransaction ) session.getTransaction(); JoinableCMTTransaction joinable = ( JoinableCMTTransaction ) session.getTransaction();
javax.transaction.Transaction transaction = joinable.getTransaction(); javax.transaction.Transaction transaction = joinable.getTransaction();
if ( transaction == null ) if ( transaction == null ) {
log.warn( "Transaction not available on beforeCompletionPhase: assuming valid" ); log.warn(
"Transaction not available on beforeCompletionPhase: assuming valid"
);
}
flush = !ctx.isFlushModeNever() && flush = !ctx.isFlushModeNever() &&
//ctx.isFlushBeforeCompletionEnabled() && //ctx.isFlushBeforeCompletionEnabled() &&
//TODO probably make it ! isFlushBeforecompletion() //TODO probably make it ! isFlushBeforecompletion()
@ -638,7 +649,9 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
} }
} }
else { else {
if ( !ignoreNotJoining ) log.warn( "Calling joinTransaction() on a non JTA EntityManager" ); if ( !ignoreNotJoining ) {
log.warn( "Calling joinTransaction() on a non JTA EntityManager" );
}
} }
} }
@ -711,6 +724,7 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
StaleObjectStateException sose = ( StaleObjectStateException ) e; StaleObjectStateException sose = ( StaleObjectStateException ) e;
Serializable identifier = sose.getIdentifier(); Serializable identifier = sose.getIdentifier();
if ( identifier != null ) { if ( identifier != null ) {
try {
Object entity = getRawSession().load( sose.getEntityName(), identifier ); Object entity = getRawSession().load( sose.getEntityName(), identifier );
if ( entity instanceof Serializable ) { if ( entity instanceof Serializable ) {
//avoid some user errors regarding boundary crossing //avoid some user errors regarding boundary crossing
@ -720,6 +734,10 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
pe = new OptimisticLockException( e ); pe = new OptimisticLockException( e );
} }
} }
catch ( EntityNotFoundException enfe ) {
pe = new OptimisticLockException( e );
}
}
else { else {
pe = new OptimisticLockException( e ); pe = new OptimisticLockException( e );
} }

View File

@ -7,6 +7,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.OptimisticLockException;
import java.util.Map; import java.util.Map;
/** /**
@ -75,8 +76,7 @@ public class RemoveTest extends TestCase {
fail("should have an optimistic lock exception"); fail("should have an optimistic lock exception");
} }
//catch( OptimisticLockException e ) { catch( OptimisticLockException e ) {
catch( Exception e ) {
log.debug("success"); log.debug("success");
} }
finally { finally {