Author: swd847
Date: 2010-07-15 09:54:12 -0400 (Thu, 15 Jul 2010)
New Revision: 13408
Added:
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/UserTransactionTest.java
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/util/DontRollBackException.java
Removed:
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/SimpleTest.java
Modified:
modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/Work.java
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionManagedBean.java
Log:
fix transaction interceptor scemantics
add test for ApplicationException
Modified: modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/Work.java
===================================================================
---
modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/Work.java 2010-07-15
13:14:30 UTC (rev 13407)
+++
modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/Work.java 2010-07-15
13:54:12 UTC (rev 13408)
@@ -56,11 +56,24 @@
}
catch (Exception e)
{
- if (newTransactionRequired && userTransaction.getStatus() !=
Status.STATUS_NO_TRANSACTION && isRollbackRequired(e, true))
+ if (newTransactionRequired && userTransaction.getStatus() !=
Status.STATUS_NO_TRANSACTION )
{
- log.debug("rolling back transaction");
- userTransaction.rollback();
+ if(isRollbackRequired(e, true))
+ {
+ log.debug("rolling back transaction");
+ userTransaction.rollback();
+ }
+ else
+ {
+ log.debug("committing transaction after
ApplicationException(rollback=false):" + e.getMessage());
+ userTransaction.commit();
+ }
}
+ else if (userTransaction.getStatus() != Status.STATUS_NO_TRANSACTION &&
isRollbackRequired(e, true))
+ {
+ userTransaction.setRollbackOnly();
+ }
+
throw e;
}
Deleted:
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/SimpleTest.java
===================================================================
---
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/SimpleTest.java 2010-07-15
13:14:30 UTC (rev 13407)
+++
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/SimpleTest.java 2010-07-15
13:54:12 UTC (rev 13408)
@@ -1,79 +0,0 @@
-package org.jboss.seam.transactions.test;
-
-import java.util.List;
-
-import javax.inject.Inject;
-import javax.persistence.EntityManager;
-import javax.persistence.EntityManagerFactory;
-import javax.persistence.PersistenceUnit;
-import javax.transaction.HeuristicMixedException;
-import javax.transaction.HeuristicRollbackException;
-import javax.transaction.NotSupportedException;
-import javax.transaction.RollbackException;
-import javax.transaction.SystemException;
-
-import junit.framework.Assert;
-
-import org.jboss.arquillian.api.Deployment;
-import org.jboss.arquillian.junit.Arquillian;
-import org.jboss.seam.transaction.Transaction;
-import org.jboss.seam.transaction.UserTransaction;
-import org.jboss.seam.transactions.test.util.ArtifactNames;
-import org.jboss.seam.transactions.test.util.MavenArtifactResolver;
-import org.jboss.shrinkwrap.api.Archive;
-import org.jboss.shrinkwrap.api.ShrinkWrap;
-import org.jboss.shrinkwrap.api.asset.ByteArrayAsset;
-import org.jboss.shrinkwrap.api.spec.WebArchive;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-(a)RunWith(Arquillian.class)
-public class SimpleTest
-{
- @Deployment
- public static Archive<?> createTestArchive()
- {
-
- WebArchive war = ShrinkWrap.create(WebArchive.class);
- war.addLibraries(MavenArtifactResolver.resolve(ArtifactNames.WELD_EXTENSIONS));
- war.addPackage(Transaction.class.getPackage()).addClasses(SimpleTest.class,
Hotel.class);
- war.addWebResource("META-INF/persistence.xml",
"classes/META-INF/persistence.xml");
- war.addWebResource(new ByteArrayAsset(new byte[0]), "beans.xml");
-
- return war;
- }
-
- @Inject
- UserTransaction transaction;
-
- @PersistenceUnit
- EntityManagerFactory emf;
-
- @Test
- public void simpleTest() throws NotSupportedException, SystemException,
SecurityException, IllegalStateException, RollbackException, HeuristicMixedException,
HeuristicRollbackException
- {
- transaction.begin();
- EntityManager em = emf.createEntityManager();
- em.joinTransaction();
- Hotel h = new Hotel("test", "Fake St", "Wollongong",
"NSW", "2518", "Australia");
- em.persist(h);
- em.flush();
- transaction.commit();
-
- transaction.begin();
- em = emf.createEntityManager();
- em.joinTransaction();
- h = new Hotel("test2", "Fake St", "Wollongong",
"NSW", "2518", "Australia");
- em.persist(h);
- em.flush();
- transaction.rollback();
-
- transaction.begin();
- em = emf.createEntityManager();
- em.joinTransaction();
- List<Hotel> hotels = em.createQuery("select h from Hotel
h").getResultList();
- Assert.assertTrue(hotels.size() == 1);
- transaction.rollback();
-
- }
-}
Modified:
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java
===================================================================
---
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java 2010-07-15
13:14:30 UTC (rev 13407)
+++
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java 2010-07-15
13:54:12 UTC (rev 13408)
@@ -19,6 +19,7 @@
import org.jboss.seam.transaction.TransactionInterceptor;
import org.jboss.seam.transaction.UserTransaction;
import org.jboss.seam.transactions.test.util.ArtifactNames;
+import org.jboss.seam.transactions.test.util.DontRollBackException;
import org.jboss.seam.transactions.test.util.EntityManagerProvider;
import org.jboss.seam.transactions.test.util.MavenArtifactResolver;
import org.jboss.shrinkwrap.api.Archive;
@@ -38,7 +39,7 @@
WebArchive war = ShrinkWrap.create("test.war", WebArchive.class);
war.addLibraries(MavenArtifactResolver.resolve(ArtifactNames.WELD_EXTENSIONS));
war.addPackage(Transaction.class.getPackage());
- war.addClasses(TransactionInterceptorTest.class, TransactionManagedBean.class,
Hotel.class, EntityManagerProvider.class);
+ war.addClasses(TransactionInterceptorTest.class, TransactionManagedBean.class,
Hotel.class, EntityManagerProvider.class, DontRollBackException.class);
war.addWebResource("META-INF/persistence.xml",
"classes/META-INF/persistence.xml");
war.addWebResource(new
ByteArrayAsset(("<beans><interceptors><class>" +
TransactionInterceptor.class.getName() +
"</class></interceptors></beans>").getBytes()),
"beans.xml");
@@ -59,21 +60,33 @@
{
bean.addHotel();
-
+ assertHotels(1);
try
{
bean.failToAddHotel();
}
catch (Exception e)
{
+ }
+ assertHotels(1);
+ try
+ {
+ bean.addHotelWithApplicationException();
}
+ catch (DontRollBackException e)
+ {
+ }
+ assertHotels(2);
+ }
+
+ public void assertHotels(int count) throws NotSupportedException, SystemException
+ {
transaction.begin();
em.joinTransaction();
List<Hotel> hotels = em.createQuery("select h from Hotel
h").getResultList();
- Assert.assertTrue("Wrong number of hotels: " + hotels.size(),
hotels.size() == 1);
+ Assert.assertTrue("Wrong number of hotels: " + hotels.size(),
hotels.size() == count);
transaction.rollback();
-
}
}
Modified:
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionManagedBean.java
===================================================================
---
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionManagedBean.java 2010-07-15
13:14:30 UTC (rev 13407)
+++
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionManagedBean.java 2010-07-15
13:54:12 UTC (rev 13408)
@@ -5,6 +5,7 @@
import org.jboss.seam.transaction.TransactionPropagation;
import org.jboss.seam.transaction.Transactional;
+import org.jboss.seam.transactions.test.util.DontRollBackException;
@Transactional(TransactionPropagation.REQUIRED)
public class TransactionManagedBean
@@ -29,4 +30,13 @@
entityManager.flush();
throw new RuntimeException("Roll back transaction");
}
+
+ public void addHotelWithApplicationException() throws DontRollBackException
+ {
+ entityManager.joinTransaction();
+ Hotel h = new Hotel("test3", "Fake St", "Wollongong",
"NSW", "2518", "Australia");
+ entityManager.persist(h);
+ entityManager.flush();
+ throw new DontRollBackException();
+ }
}
Copied:
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/UserTransactionTest.java
(from rev 13406,
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/SimpleTest.java)
===================================================================
---
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/UserTransactionTest.java
(rev 0)
+++
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/UserTransactionTest.java 2010-07-15
13:54:12 UTC (rev 13408)
@@ -0,0 +1,79 @@
+package org.jboss.seam.transactions.test;
+
+import java.util.List;
+
+import javax.inject.Inject;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.PersistenceUnit;
+import javax.transaction.HeuristicMixedException;
+import javax.transaction.HeuristicRollbackException;
+import javax.transaction.NotSupportedException;
+import javax.transaction.RollbackException;
+import javax.transaction.SystemException;
+
+import junit.framework.Assert;
+
+import org.jboss.arquillian.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.seam.transaction.Transaction;
+import org.jboss.seam.transaction.UserTransaction;
+import org.jboss.seam.transactions.test.util.ArtifactNames;
+import org.jboss.seam.transactions.test.util.MavenArtifactResolver;
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.ByteArrayAsset;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+(a)RunWith(Arquillian.class)
+public class UserTransactionTest
+{
+ @Deployment
+ public static Archive<?> createTestArchive()
+ {
+
+ WebArchive war = ShrinkWrap.create("test.war", WebArchive.class);
+ war.addLibraries(MavenArtifactResolver.resolve(ArtifactNames.WELD_EXTENSIONS));
+
war.addPackage(Transaction.class.getPackage()).addClasses(UserTransactionTest.class,
Hotel.class);
+ war.addWebResource("META-INF/persistence.xml",
"classes/META-INF/persistence.xml");
+ war.addWebResource(new ByteArrayAsset(new byte[0]), "beans.xml");
+
+ return war;
+ }
+
+ @Inject
+ UserTransaction transaction;
+
+ @PersistenceUnit
+ EntityManagerFactory emf;
+
+ @Test
+ public void userTransactionTest() throws NotSupportedException, SystemException,
SecurityException, IllegalStateException, RollbackException, HeuristicMixedException,
HeuristicRollbackException
+ {
+ transaction.begin();
+ EntityManager em = emf.createEntityManager();
+ em.joinTransaction();
+ Hotel h = new Hotel("test", "Fake St", "Wollongong",
"NSW", "2518", "Australia");
+ em.persist(h);
+ em.flush();
+ transaction.commit();
+
+ transaction.begin();
+ em = emf.createEntityManager();
+ em.joinTransaction();
+ h = new Hotel("test2", "Fake St", "Wollongong",
"NSW", "2518", "Australia");
+ em.persist(h);
+ em.flush();
+ transaction.rollback();
+
+ transaction.begin();
+ em = emf.createEntityManager();
+ em.joinTransaction();
+ List<Hotel> hotels = em.createQuery("select h from Hotel
h").getResultList();
+ Assert.assertTrue(hotels.size() == 1);
+ transaction.rollback();
+ }
+
+}
Added:
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/util/DontRollBackException.java
===================================================================
---
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/util/DontRollBackException.java
(rev 0)
+++
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/util/DontRollBackException.java 2010-07-15
13:54:12 UTC (rev 13408)
@@ -0,0 +1,9 @@
+package org.jboss.seam.transactions.test.util;
+
+import javax.ejb.ApplicationException;
+
+@ApplicationException(rollback = false)
+public class DontRollBackException extends Exception
+{
+
+}