[hibernate-commits] Hibernate SVN: r10758 - in branches/Branch_3_2/HibernateExt/ejb/src: java/org/hibernate/ejb test/org/hibernate/ejb/test/transaction

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Tue Nov 7 18:50:29 EST 2006


Author: epbernard
Date: 2006-11-07 18:50:28 -0500 (Tue, 07 Nov 2006)
New Revision: 10758

Modified:
   branches/Branch_3_2/HibernateExt/ejb/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java
   branches/Branch_3_2/HibernateExt/ejb/src/java/org/hibernate/ejb/HibernateEntityManagerImplementor.java
   branches/Branch_3_2/HibernateExt/ejb/src/java/org/hibernate/ejb/TransactionImpl.java
   branches/Branch_3_2/HibernateExt/ejb/src/test/org/hibernate/ejb/test/transaction/FlushAndTransactionTest.java
Log:
EJB-248 wrap SSE into OptimisticLockException whe em.getTransaction().commit() is used

Modified: branches/Branch_3_2/HibernateExt/ejb/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java
===================================================================
--- branches/Branch_3_2/HibernateExt/ejb/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java	2006-11-07 21:35:44 UTC (rev 10757)
+++ branches/Branch_3_2/HibernateExt/ejb/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java	2006-11-07 23:50:28 UTC (rev 10758)
@@ -596,26 +596,8 @@
 
 	public void throwPersistenceException(HibernateException e) {
 		if ( e instanceof StaleStateException ) {
-			if ( e instanceof StaleObjectStateException ) {
-				StaleObjectStateException sose = (StaleObjectStateException) e;
-				Serializable identifier = sose.getIdentifier();
-				if (identifier != null) {
-					Object entity = getRawSession().load( sose.getEntityName(), identifier );
-					if ( entity instanceof Serializable ) {
-						//avoid some user errors regarding boundary crossing
-						throwPersistenceException( new OptimisticLockException(	null, e, entity ) );
-					}
-					else {
-						throwPersistenceException( new OptimisticLockException( e ) );
-					}
-				}
-				else {
-					throwPersistenceException( new OptimisticLockException( e ) );
-				}
-			}
-			else {
-				throwPersistenceException( new OptimisticLockException( e ) );
-			}
+			PersistenceException pe = wrapStaleStateException( (StaleStateException) e );
+			throwPersistenceException( pe );
 		}
 		else if ( e instanceof ConstraintViolationException ) {
 			//FIXME this is bad cause ConstraintViolationException happens in other circumstances
@@ -647,4 +629,29 @@
 			throwPersistenceException( new PersistenceException( e ) );
 		}
 	}
+
+	public PersistenceException wrapStaleStateException(StaleStateException e) {
+		PersistenceException pe;
+		if ( e instanceof StaleObjectStateException ) {
+			StaleObjectStateException sose = (StaleObjectStateException) e;
+			Serializable identifier = sose.getIdentifier();
+			if (identifier != null) {
+				Object entity = getRawSession().load( sose.getEntityName(), identifier );
+				if ( entity instanceof Serializable ) {
+					//avoid some user errors regarding boundary crossing
+					pe = new OptimisticLockException(	null, e, entity );
+				}
+				else {
+					pe = new OptimisticLockException( e );
+				}
+			}
+			else {
+				pe = new OptimisticLockException( e );
+			}
+		}
+		else {
+			pe = new OptimisticLockException( e );
+		}
+		return pe;
+	}
 }

Modified: branches/Branch_3_2/HibernateExt/ejb/src/java/org/hibernate/ejb/HibernateEntityManagerImplementor.java
===================================================================
--- branches/Branch_3_2/HibernateExt/ejb/src/java/org/hibernate/ejb/HibernateEntityManagerImplementor.java	2006-11-07 21:35:44 UTC (rev 10757)
+++ branches/Branch_3_2/HibernateExt/ejb/src/java/org/hibernate/ejb/HibernateEntityManagerImplementor.java	2006-11-07 23:50:28 UTC (rev 10758)
@@ -4,6 +4,7 @@
 import javax.persistence.PersistenceException;
 
 import org.hibernate.HibernateException;
+import org.hibernate.StaleStateException;
 
 /**
  * @author Emmanuel Bernard
@@ -14,4 +15,6 @@
 	public void throwPersistenceException(PersistenceException e);
 
 	public void throwPersistenceException(HibernateException e);
+
+	public PersistenceException wrapStaleStateException(StaleStateException e);
 }

Modified: branches/Branch_3_2/HibernateExt/ejb/src/java/org/hibernate/ejb/TransactionImpl.java
===================================================================
--- branches/Branch_3_2/HibernateExt/ejb/src/java/org/hibernate/ejb/TransactionImpl.java	2006-11-07 21:35:44 UTC (rev 10757)
+++ branches/Branch_3_2/HibernateExt/ejb/src/java/org/hibernate/ejb/TransactionImpl.java	2006-11-07 23:50:28 UTC (rev 10758)
@@ -8,6 +8,7 @@
 import org.hibernate.HibernateException;
 import org.hibernate.Session;
 import org.hibernate.Transaction;
+import org.hibernate.StaleStateException;
 
 /**
  * @author Gavin King
@@ -15,7 +16,7 @@
  */
 public class TransactionImpl implements EntityTransaction {
 
-	private AbstractEntityManagerImpl entityManager;
+	private HibernateEntityManagerImplementor entityManager;
 	private Transaction tx;
 	private boolean rollbackOnly;
 
@@ -53,6 +54,13 @@
 			tx.commit();
 		}
 		catch (Exception e) {
+			Exception wrappedException;
+			if (e instanceof StaleStateException) {
+				wrappedException = entityManager.wrapStaleStateException( (StaleStateException) e );
+			}
+			else {
+				wrappedException = e;
+			}
 			try {
 				//as per the spec we should rollback if commit fails
 				tx.rollback();
@@ -60,7 +68,7 @@
 			catch (Exception re) {
 				//swallow
 			}
-			throw new RollbackException( "Error while commiting the transaction", e );
+			throw new RollbackException( "Error while commiting the transaction", wrappedException );
 		}
 		finally {
 			rollbackOnly = false;

Modified: branches/Branch_3_2/HibernateExt/ejb/src/test/org/hibernate/ejb/test/transaction/FlushAndTransactionTest.java
===================================================================
--- branches/Branch_3_2/HibernateExt/ejb/src/test/org/hibernate/ejb/test/transaction/FlushAndTransactionTest.java	2006-11-07 21:35:44 UTC (rev 10757)
+++ branches/Branch_3_2/HibernateExt/ejb/src/test/org/hibernate/ejb/test/transaction/FlushAndTransactionTest.java	2006-11-07 23:50:28 UTC (rev 10758)
@@ -7,10 +7,12 @@
 import javax.persistence.RollbackException;
 import javax.persistence.TransactionRequiredException;
 import javax.persistence.PersistenceException;
+import javax.persistence.OptimisticLockException;
 
 import org.hibernate.ejb.HibernateEntityManagerFactory;
 import org.hibernate.ejb.test.TestCase;
 import org.hibernate.stat.Statistics;
+import org.hibernate.Session;
 
 /**
  * @author Emmanuel Bernard
@@ -235,6 +237,34 @@
 
 	}
 
+	public void testRollbackExceptionOnOptimisticLockException() throws Exception {
+		Book book = new Book();
+		book.name = "Stolen keys";
+		book.id = null; //new Integer( 50 );
+		EntityManager em = factory.createEntityManager();
+		em.getTransaction().begin();
+		em.persist( book );
+		em.flush();
+		em.clear();
+		book.setName( "kitty kid");
+		em.merge( book );
+		em.flush();
+		em.clear();
+		book.setName( "kitty kid2"); //non updated version
+		( (Session) em.getDelegate() ).update( book );
+		try {
+			em.getTransaction().commit();
+			fail( "Commit should be rollbacked" );
+		}
+		catch (RollbackException e) {
+			assertTrue( "During flush a StateStateException is wrapped into a OptimisticLockException", e.getCause() instanceof OptimisticLockException );
+		}
+		finally {
+			em.close();
+		}
+
+	}
+
 	public void testRollbackClearPC() throws Exception {
 		Book book = new Book();
 		book.name = "Stolen keys";




More information about the hibernate-commits mailing list