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

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Jun 23 10:28:58 EDT 2009


Author: wolfc
Date: 2009-06-23 10:28:57 -0400 (Tue, 23 Jun 2009)
New Revision: 90524

Added:
   projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1850/
   projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1850/SyncStateful.java
   projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1850/SyncStatefulBean.java
   projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1850/unit/
   projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1850/unit/TxSyncTestCase.java
Modified:
   projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/stateful/SessionSynchronizationInterceptor.java
   projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/stateful/StatefulBeanContext.java
   projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/stateful/StatefulInstanceInterceptor.java
   projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/stateful/StatefulRemoveInterceptor.java
Log:
EJBTHREE-1850: have a single Synchronization in StatefulBeanContext which manages ordering

Modified: projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/stateful/SessionSynchronizationInterceptor.java
===================================================================
--- projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/stateful/SessionSynchronizationInterceptor.java	2009-06-23 13:46:59 UTC (rev 90523)
+++ projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/stateful/SessionSynchronizationInterceptor.java	2009-06-23 14:28:57 UTC (rev 90524)
@@ -151,7 +151,7 @@
       SFSBSessionSynchronization synch = new SFSBSessionSynchronization(ctx);
       try
       {
-         tx.registerSynchronization(synch);
+         ctx.registerSynchronization(tx, synch);
       }
       catch(RollbackException e)
       {

Modified: projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/stateful/StatefulBeanContext.java
===================================================================
--- projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/stateful/StatefulBeanContext.java	2009-06-23 13:46:59 UTC (rev 90523)
+++ projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/stateful/StatefulBeanContext.java	2009-06-23 14:28:57 UTC (rev 90524)
@@ -34,7 +34,9 @@
 
 import javax.ejb.EJBContext;
 import javax.persistence.EntityManager;
+import javax.transaction.RollbackException;
 import javax.transaction.Synchronization;
+import javax.transaction.SystemException;
 import javax.transaction.Transaction;
 
 import org.jboss.aop.metadata.SimpleMetaData;
@@ -67,6 +69,41 @@
    /** The serialVersionUID */
    private static final long serialVersionUID = -102470788178912606L;
    
+   private class StatefulSynchronization implements Synchronization
+   {
+      public void afterCompletion(int status)
+      {
+         RuntimeException cause = null;
+         for(Synchronization sync : synchronizations)
+         {
+            try
+            {
+               sync.afterCompletion(status);
+            }
+            catch(RuntimeException e)
+            {
+               log.warn("afterCompletion failed on " + sync, e);
+               cause = e;
+            }
+         }
+         synchronizations.clear();
+         currentSyncTx = null;
+         
+         // EJBTHREE-1745: report at least one of the problems upward
+         if(cause != null)
+            throw cause;
+      }
+
+      public void beforeCompletion()
+      {
+         // if one of them throws up, the TM will initiate a rollback
+         for(Synchronization sync : synchronizations)
+         {
+            sync.beforeCompletion();
+         }
+      }
+   }
+   
    protected Object id;
 
    protected boolean txSynchronized = false;
@@ -100,6 +137,9 @@
    protected boolean replicationIsPassivation = true;
    
    protected transient boolean passivated = false;
+   
+   private transient Transaction currentSyncTx;
+   private transient List<Synchronization> synchronizations;
 
    /**
     * An incoming context from serialization.
@@ -1004,6 +1044,32 @@
       return result;
    }
    
+   /**
+    * To make sure Synchronizations are executed in a given order we maintain
+    * a sync registry within this object.
+    *  
+    * @param sync
+    * @throws IllegalStateException
+    * @throws RollbackException
+    * @throws SystemException
+    */
+   protected void registerSynchronization(Transaction tx, Synchronization sync) throws IllegalStateException, RollbackException, SystemException
+   {
+      // sanity check
+      if(currentSyncTx != null)
+      {
+         if(currentSyncTx != tx)
+            throw new IllegalStateException("StatefulBeanContext " + this + " can't be synced with " + tx + ", it is already synced with " + currentSyncTx);
+      }
+      else
+      {
+         currentSyncTx = tx;
+         synchronizations = new ArrayList<Synchronization>();
+         tx.registerSynchronization(new StatefulSynchronization());
+      }
+      synchronizations.add(sync);
+   }
+   
    private static class XPCCloseSynchronization implements Synchronization
    {
       private StatefulBeanContext ctx;

Modified: projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/stateful/StatefulInstanceInterceptor.java
===================================================================
--- projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/stateful/StatefulInstanceInterceptor.java	2009-06-23 13:46:59 UTC (rev 90523)
+++ projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/stateful/StatefulInstanceInterceptor.java	2009-06-23 14:28:57 UTC (rev 90524)
@@ -102,6 +102,7 @@
          synchronized (target)
          {
             target.setInInvocation(false);
+            // nobody attached the bean to the tx and nobody discarded it
             if (!target.isTxSynchronized() && !target.isDiscarded()) container.getCache().release(target);
             if (block) target.getLock().unlock();
          }

Modified: projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/stateful/StatefulRemoveInterceptor.java
===================================================================
--- projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/stateful/StatefulRemoveInterceptor.java	2009-06-23 13:46:59 UTC (rev 90523)
+++ projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/stateful/StatefulRemoveInterceptor.java	2009-06-23 14:28:57 UTC (rev 90524)
@@ -159,7 +159,7 @@
       {
          try
          {
-            tx.registerSynchronization(new RemoveSynchronization(container, id, exceptionThrown != true && retainIfException));
+            ctx.registerSynchronization(tx, new RemoveSynchronization(container, id, exceptionThrown != true && retainIfException));
          }
          catch (RollbackException e)
          {

Added: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1850/SyncStateful.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1850/SyncStateful.java	                        (rev 0)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1850/SyncStateful.java	2009-06-23 14:28:57 UTC (rev 90524)
@@ -0,0 +1,40 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.core.test.ejbthree1850;
+
+import java.io.Serializable;
+
+import javax.ejb.Local;
+
+/**
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+ at Local
+public interface SyncStateful
+{
+   Serializable getState();
+   
+   void remove();
+   
+   void setState(Serializable state);
+}

Added: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1850/SyncStatefulBean.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1850/SyncStatefulBean.java	                        (rev 0)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1850/SyncStatefulBean.java	2009-06-23 14:28:57 UTC (rev 90524)
@@ -0,0 +1,83 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.core.test.ejbthree1850;
+
+import java.io.Serializable;
+import java.rmi.RemoteException;
+
+import javax.ejb.EJBException;
+import javax.ejb.Remove;
+import javax.ejb.SessionSynchronization;
+import javax.ejb.Stateful;
+
+import org.jboss.logging.Logger;
+
+/**
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+ at Stateful
+public class SyncStatefulBean implements SyncStateful, SessionSynchronization
+{
+   private static final Logger log = Logger.getLogger(SyncStatefulBean.class);
+   
+   // for unit tests only
+   public static int afterBegins = 0;
+   public static int afterCompletions = 0;
+   public static int beforeCompletions = 0;
+   
+   private Serializable state;
+   
+   public void afterBegin() throws EJBException, RemoteException
+   {
+      log.info("afterBegin");
+      afterBegins++;
+   }
+
+   public void afterCompletion(boolean committed) throws EJBException, RemoteException
+   {
+      log.info("afterCompletion " + committed);
+      afterCompletions++;
+   }
+
+   public void beforeCompletion() throws EJBException, RemoteException
+   {
+      log.info("beforeCompletion");
+      beforeCompletions++;
+   }
+
+   public Serializable getState()
+   {
+      return state;
+   }
+
+   @Remove
+   public void remove()
+   {
+      log.info("remove");
+   }
+
+   public void setState(Serializable state)
+   {
+      this.state = state;
+   }
+}

Added: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1850/unit/TxSyncTestCase.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1850/unit/TxSyncTestCase.java	                        (rev 0)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1850/unit/TxSyncTestCase.java	2009-06-23 14:28:57 UTC (rev 90524)
@@ -0,0 +1,68 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.core.test.ejbthree1850.unit;
+
+import static junit.framework.Assert.assertEquals;
+
+import javax.naming.NamingException;
+
+import org.jboss.ejb3.core.test.common.AbstractEJB3TestCase;
+import org.jboss.ejb3.core.test.ejbthree1850.SyncStateful;
+import org.jboss.ejb3.core.test.ejbthree1850.SyncStatefulBean;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class TxSyncTestCase extends AbstractEJB3TestCase
+{
+   @Before
+   public void before()
+   {
+      SyncStatefulBean.afterBegins = 0;
+      SyncStatefulBean.afterCompletions = 0;
+      SyncStatefulBean.beforeCompletions = 0;
+   }
+   
+   @BeforeClass
+   public static void beforeClass() throws Exception
+   {
+      AbstractEJB3TestCase.beforeClass();
+      
+      deploySessionEjb(SyncStatefulBean.class);
+   }
+   
+   @Test
+   public void testRemove() throws NamingException
+   {
+      SyncStateful bean = lookup("SyncStatefulBean/local", SyncStateful.class);
+      // if the remove sync runs before session sync, then SessionSync.afterCompletion won't have run
+      bean.remove();
+      
+      assertEquals("afterBegin has not run", 1, SyncStatefulBean.afterBegins);
+      assertEquals("beforeCompletion has not run", 1, SyncStatefulBean.beforeCompletions);
+      assertEquals("afterCompletion has not run", 1, SyncStatefulBean.afterCompletions);
+   }
+}




More information about the jboss-cvs-commits mailing list