[jboss-cvs] JBossAS SVN: r58197 - in trunk/ejb3/src: main/org/jboss/ejb3/stateful test/org/jboss/ejb3/test/stateful test/org/jboss/ejb3/test/stateful/unit

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Nov 7 20:13:35 EST 2006


Author: bdecoste
Date: 2006-11-07 20:13:31 -0500 (Tue, 07 Nov 2006)
New Revision: 58197

Modified:
   trunk/ejb3/src/main/org/jboss/ejb3/stateful/StatefulRemoveInterceptor.java
   trunk/ejb3/src/test/org/jboss/ejb3/test/stateful/EntityFacade.java
   trunk/ejb3/src/test/org/jboss/ejb3/test/stateful/EntityFacadeBean.java
   trunk/ejb3/src/test/org/jboss/ejb3/test/stateful/unit/RemoteUnitTestCase.java
Log:
fix and test for removing a bean after exception in PreDestroy

Modified: trunk/ejb3/src/main/org/jboss/ejb3/stateful/StatefulRemoveInterceptor.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/ejb3/stateful/StatefulRemoveInterceptor.java	2006-11-08 00:18:14 UTC (rev 58196)
+++ trunk/ejb3/src/main/org/jboss/ejb3/stateful/StatefulRemoveInterceptor.java	2006-11-08 01:13:31 UTC (rev 58197)
@@ -53,13 +53,15 @@
 
    private static class RemoveSynchronization implements Synchronization
    {
+      protected boolean retainIfException;
       private StatefulContainer container;
       private Object id;
 
-      public RemoveSynchronization(StatefulContainer container, Object id)
+      public RemoveSynchronization(StatefulContainer container, Object id, boolean removeOnException)
       {
          this.container = container;
          this.id = id;
+         this.retainIfException = removeOnException;
       }
 
 
@@ -69,6 +71,19 @@
 
       public void afterCompletion(int status)
       {
+         try
+         {
+            StatefulBeanContext ctx = container.getCache().get(id);
+            container.invokePreDestroy(ctx);
+         }
+         catch (Throwable t)
+         {
+            if (!retainIfException)
+            {
+               container.getCache().remove(id);
+            }
+            throw new RuntimeException(t);
+         }
          container.getCache().remove(id);
       }
    }
@@ -92,7 +107,7 @@
       return rtn;
    }
 
-   protected void removeSession(Invocation invocation)
+   protected void removeSession(Invocation invocation) throws Throwable
    {
       StatefulContainerInvocation ejb = (StatefulContainerInvocation) invocation;
       Object id = ejb.getId();
@@ -106,11 +121,12 @@
       {
          throw new RuntimeException(e);
       }
+    
       if (tx != null && TxUtils.isActive(tx))
       {
          try
          {
-            tx.registerSynchronization(new RemoveSynchronization(container, id));
+            tx.registerSynchronization(new RemoveSynchronization(container, id, retainIfException));
          }
          catch (RollbackException e)
          {
@@ -125,7 +141,18 @@
       {
          // Release it properly.
          StatefulBeanContext ctx = container.getCache().get(id);
-         container.getCache().remove(id);
+         try
+         {
+            container.invokePreDestroy(ctx);
+         }
+         catch (Throwable t)
+         {
+            if (!retainIfException)
+            {
+               container.getCache().remove(id);
+            }
+            throw t;
+         }
       }
    }
 }

Modified: trunk/ejb3/src/test/org/jboss/ejb3/test/stateful/EntityFacade.java
===================================================================
--- trunk/ejb3/src/test/org/jboss/ejb3/test/stateful/EntityFacade.java	2006-11-08 00:18:14 UTC (rev 58196)
+++ trunk/ejb3/src/test/org/jboss/ejb3/test/stateful/EntityFacade.java	2006-11-08 01:13:31 UTC (rev 58197)
@@ -29,4 +29,8 @@
    Entity createEntity(String name);
    
    Entity loadEntity(Long id);
+   
+   void remove();
+   
+   void removeWithTx();
 }

Modified: trunk/ejb3/src/test/org/jboss/ejb3/test/stateful/EntityFacadeBean.java
===================================================================
--- trunk/ejb3/src/test/org/jboss/ejb3/test/stateful/EntityFacadeBean.java	2006-11-08 00:18:14 UTC (rev 58196)
+++ trunk/ejb3/src/test/org/jboss/ejb3/test/stateful/EntityFacadeBean.java	2006-11-08 01:13:31 UTC (rev 58197)
@@ -24,9 +24,11 @@
 import org.jboss.annotation.ejb.cache.simple.CacheConfig;
 import org.jboss.logging.Logger;
 
+import javax.annotation.PreDestroy;
 import javax.ejb.PostActivate;
 import javax.ejb.PrePassivate;
 import javax.ejb.Remote;
+import javax.ejb.Remove;
 import javax.ejb.Stateful;
 import javax.ejb.TransactionAttribute;
 import javax.ejb.TransactionAttributeType;
@@ -74,4 +76,26 @@
    {
       log.info("************ activating");
    }
+   
+   @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
+   @Remove(retainIfException=true)
+   public void remove()
+   {
+      
+   }
+   
+   @Remove(retainIfException=true)
+   public void removeWithTx()
+   {
+      
+   }
+   
+   @PreDestroy
+   public void destroy()
+   {
+      log.info("************ destroying");  
+      // throw RuntimeException
+      Object o = null;
+      o.getClass();
+   }
 }

Modified: trunk/ejb3/src/test/org/jboss/ejb3/test/stateful/unit/RemoteUnitTestCase.java
===================================================================
--- trunk/ejb3/src/test/org/jboss/ejb3/test/stateful/unit/RemoteUnitTestCase.java	2006-11-08 00:18:14 UTC (rev 58196)
+++ trunk/ejb3/src/test/org/jboss/ejb3/test/stateful/unit/RemoteUnitTestCase.java	2006-11-08 01:13:31 UTC (rev 58197)
@@ -574,6 +574,36 @@
       kernel.invoke(deployment, "stop", new Object[0], new String[0]);
       kernel.invoke(deployment, "start", new Object[0], new String[0]);
    }
+   
+   public void testDestroyException() throws Exception
+   {
+      EntityFacade stateful = (EntityFacade)getInitialContext().lookup("EntityFacadeBean/remote");
+      assertNotNull(stateful);
+      stateful.createEntity("Kalin");
+      
+      try
+      {
+         stateful.remove();
+         fail("should catch RuntimeException");
+      }
+      catch (RuntimeException e)
+      {
+      }
+      
+      stateful.createEntity("Cabernet");
+      
+      try
+      {
+         stateful.removeWithTx();
+         fail("should catch RuntimeException");
+      }
+      catch (RuntimeException e)
+      {
+      }
+      
+      stateful.createEntity("Bailey");
+      
+   }
 
    public static Test suite() throws Exception
    {




More information about the jboss-cvs-commits mailing list