[jboss-cvs] JBossAS SVN: r70771 - in projects/ejb3/trunk/core/src: test/java/org/jboss/ejb3/test/txexceptions and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Mar 12 10:21:19 EDT 2008


Author: alex.loubyansky at jboss.com
Date: 2008-03-12 10:21:18 -0400 (Wed, 12 Mar 2008)
New Revision: 70771

Modified:
   projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/tx/Ejb3TxPolicy.java
   projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/test/txexceptions/Dao.java
   projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/test/txexceptions/DaoBean.java
   projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/test/txexceptions/RequiresNewTest.java
   projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/test/txexceptions/unit/TxExceptionsTestCase.java
Log:
EJBTHREE-1072

Modified: projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/tx/Ejb3TxPolicy.java
===================================================================
--- projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/tx/Ejb3TxPolicy.java	2008-03-12 14:17:08 UTC (rev 70770)
+++ projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/tx/Ejb3TxPolicy.java	2008-03-12 14:21:18 UTC (rev 70771)
@@ -53,15 +53,27 @@
          if (ae.rollback()) setRollbackOnly(tx);
          throw t;
       }
-      if (!(t instanceof RuntimeException || t instanceof RemoteException))
+
+      // if it's neither EJBException nor RemoteException
+      if(!(t instanceof EJBException || t instanceof RemoteException))
       {
-         throw t;
+         // errors and unchecked are wrapped into EJBException
+         if(t instanceof Error)
+         {
+            t = new EJBException(formatException("Unexpected Error", t));
+         }
+         else if (t instanceof RuntimeException)
+         {
+            t = new EJBException((Exception)t);
+         }
+         else
+         {
+            // an application exception
+            throw t;
+         }
       }
+
       setRollbackOnly(tx);
-      if (t instanceof RuntimeException && !(t instanceof EJBException))
-      {
-         throw new EJBException((Exception) t);
-      }
       throw t;
    }
 
@@ -74,24 +86,39 @@
          if (ae.rollback()) setRollbackOnly(tx);
          throw t;
       }
-      if (!(t instanceof RuntimeException || t instanceof RemoteException))
+      
+      // if it's not EJBTransactionRolledbackException
+      if(!(t instanceof EJBTransactionRolledbackException))
       {
-         throw t;
+         if(t instanceof Error)
+         {
+            t = new EJBTransactionRolledbackException(formatException("Unexpected Error", t));
+         }
+         else if(t instanceof RuntimeException || t instanceof RemoteException)
+         {
+            t = new EJBTransactionRolledbackException(t.getMessage(), (Exception) t);
+         }
+         else // application exception
+         {
+            throw t;
+         }
       }
+      
       setRollbackOnly(tx);
-      // its either a RuntimeException or RemoteException
-      
-      if (t instanceof EJBTransactionRolledbackException)
+      log.error(t);
+      throw t;
+   }
+
+   private String formatException(String msg, Throwable t)
+   {
+      java.io.StringWriter sw = new java.io.StringWriter();
+      java.io.PrintWriter pw = new java.io.PrintWriter(sw);
+      if (msg != null)
+         pw.println(msg);
+      if (t != null)
       {
-         log.error(t);
-         throw t;
-      }
-      else
-      {
-         Throwable ejbtre = new EJBTransactionRolledbackException(t.getMessage(), (Exception) t);
-         log.error(ejbtre);
-         throw ejbtre;
-      }
+         t.printStackTrace(pw);
+      } // end of if ()
+      return sw.toString();
    }
-
 }

Modified: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/test/txexceptions/Dao.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/test/txexceptions/Dao.java	2008-03-12 14:17:08 UTC (rev 70770)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/test/txexceptions/Dao.java	2008-03-12 14:21:18 UTC (rev 70771)
@@ -55,4 +55,6 @@
    void remove(int id);
 
    void testRequiresNewWithLookedUpEntityManager() throws Exception;
+
+   void testRollbackErrorFromCallerTx() throws Exception;
 }

Modified: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/test/txexceptions/DaoBean.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/test/txexceptions/DaoBean.java	2008-03-12 14:17:08 UTC (rev 70770)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/test/txexceptions/DaoBean.java	2008-03-12 14:21:18 UTC (rev 70771)
@@ -40,8 +40,7 @@
 @Remote(Dao.class)
 public class DaoBean implements Dao
 {
-   private static final Logger log = Logger
-   .getLogger(DaoBean.class);
+   private static final Logger log = Logger.getLogger(DaoBean.class);
    
    @PersistenceContext EntityManager manager;
 
@@ -51,6 +50,12 @@
       RequiresNewTest.doit();
    }
 
+   @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
+   public void testRollbackErrorFromCallerTx() throws Exception
+   {
+      RequiresNewTest.daoCreateThrowRollbackError();
+   }
+
    public SimpleEntity get(int id)
    {
       SimpleEntity en = manager.find(SimpleEntity.class, id);

Modified: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/test/txexceptions/RequiresNewTest.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/test/txexceptions/RequiresNewTest.java	2008-03-12 14:17:08 UTC (rev 70770)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/test/txexceptions/RequiresNewTest.java	2008-03-12 14:21:18 UTC (rev 70771)
@@ -24,6 +24,7 @@
 import org.jboss.ejb3.tx.TxUtil;
 
 import javax.persistence.EntityManager;
+import javax.ejb.EJBException;
 import javax.naming.InitialContext;
 import javax.transaction.TransactionManager;
 import javax.transaction.Transaction;
@@ -56,4 +57,38 @@
       tm.resume(tx);
       tm.commit();
    }
+
+   public static void daoCreateThrowRollbackError() throws Exception
+   {
+      TransactionManager tm = TxUtil.getTransactionManager();
+      tm.begin();
+
+      InitialContext ctx = new InitialContext();
+      Dao dao = (Dao) ctx.lookup("DaoBean/remote");
+
+      try
+      {
+         dao.createThrowRollbackError(1);
+         throw new RuntimeException("Expected error not thrown");
+      }
+      catch (EJBException e)
+      {
+         // AFAIK, the spec doesn't define how the causing error should be delivered
+         // Currently, it's done the same way our EJB2 containers handle errors,
+         // i.e. the msg is formatted including the stacktrace of the error
+         // and re-thrown as the EJBException
+      }
+      finally
+      {
+         tm.rollback();
+      }
+      
+      SimpleEntity entity = dao.get(1);
+      
+      if (entity != null)
+         dao.remove(1);
+      
+      if(entity != null)
+         throw new RuntimeException("Entity is there");
+   }
 }

Modified: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/test/txexceptions/unit/TxExceptionsTestCase.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/test/txexceptions/unit/TxExceptionsTestCase.java	2008-03-12 14:17:08 UTC (rev 70770)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/test/txexceptions/unit/TxExceptionsTestCase.java	2008-03-12 14:21:18 UTC (rev 70771)
@@ -22,6 +22,7 @@
 package org.jboss.ejb3.test.txexceptions.unit;
 
 import javax.ejb.EJBException;
+
 import org.jboss.ejb3.test.txexceptions.AnnotatedAppException;
 import org.jboss.ejb3.test.txexceptions.DeploymentDescriptorAppException;
 import org.jboss.ejb3.test.txexceptions.AppException;
@@ -30,7 +31,6 @@
 import org.jboss.ejb3.test.txexceptions.Dao;
 import org.jboss.ejb3.test.txexceptions.NoRollbackRemoteException;
 import org.jboss.ejb3.test.txexceptions.NoRollbackRuntimeException;
-import org.jboss.ejb3.test.txexceptions.RollbackError;
 import org.jboss.ejb3.test.txexceptions.RollbackRemoteException;
 import org.jboss.ejb3.test.txexceptions.RollbackRuntimeException;
 import org.jboss.ejb3.test.txexceptions.SimpleEntity;
@@ -245,11 +245,11 @@
       catch (EJBException e)
       {
          // AFAIK, the spec doesn't define how the causing error should be delivered
-         // so, this is based on the current impl
-         assertTrue(e.getCausedByException() instanceof RuntimeException);
-         assertTrue(((RuntimeException)e.getCausedByException()).getCause() instanceof RollbackError);
+         // Currently, it's done the same way our EJB2 containers handle errors,
+         // i.e. the msg is formatted including the stacktrace of the error
+         // and re-thrown as the EJBException
       }
-     
+      
       SimpleEntity entity = dao.get(1);
       
       if (entity != null)
@@ -258,6 +258,12 @@
       assertNull(entity);
    }
 
+   public void testRollbackErrorInCallerTx() throws Exception
+   {
+      Dao dao = (Dao) getInitialContext().lookup("DaoBean/remote");
+      dao.testRollbackErrorFromCallerTx();
+   }
+   
    public static Test suite() throws Exception
    {
       return getDeploySetup(TxExceptionsTestCase.class, "txexceptions-test.jar");




More information about the jboss-cvs-commits mailing list