[jboss-cvs] JBossAS SVN: r76425 - in projects/ejb3/trunk/transactions/src: test/java/org/jboss/ejb3/test/tx/common and 4 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Jul 29 09:28:47 EDT 2008


Author: wolfc
Date: 2008-07-29 09:28:47 -0400 (Tue, 29 Jul 2008)
New Revision: 76425

Added:
   projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/SessionSynchronizationInterceptor.java
   projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/tx/test/sessionsynchronization/
   projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/tx/test/sessionsynchronization/SessionSyncTest.java
   projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/tx/test/sessionsynchronization/SessionSyncTestBean.java
   projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/tx/test/sessionsynchronization/unit/
   projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/tx/test/sessionsynchronization/unit/SessionSyncTestCase.java
Modified:
   projects/ejb3/trunk/transactions/src/main/java/org/jboss/ejb3/tx/Ejb3TxPolicy.java
   projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/StatefulBeanContext.java
   projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/StatefulContainer.java
   projects/ejb3/trunk/transactions/src/test/resources/instance/jboss-aop.xml
Log:
EJBTHREE-1443: throwing EJBTransactionRolledbackException

Modified: projects/ejb3/trunk/transactions/src/main/java/org/jboss/ejb3/tx/Ejb3TxPolicy.java
===================================================================
--- projects/ejb3/trunk/transactions/src/main/java/org/jboss/ejb3/tx/Ejb3TxPolicy.java	2008-07-29 13:24:12 UTC (rev 76424)
+++ projects/ejb3/trunk/transactions/src/main/java/org/jboss/ejb3/tx/Ejb3TxPolicy.java	2008-07-29 13:28:47 UTC (rev 76425)
@@ -27,6 +27,7 @@
 import javax.ejb.EJBException;
 import javax.ejb.EJBTransactionRequiredException;
 import javax.ejb.EJBTransactionRolledbackException;
+import javax.transaction.RollbackException;
 import javax.transaction.Transaction;
 
 import org.jboss.aop.joinpoint.Invocation;
@@ -45,6 +46,14 @@
       throw new EJBTransactionRequiredException(((MethodInvocation) invocation).getActualMethod().toString());
    }
 
+   @Override
+   public void handleEndTransactionException(Exception e)
+   {
+      if(e instanceof RollbackException)
+         throw new EJBTransactionRolledbackException("Transaction rolled back", e);
+      super.handleEndTransactionException(e);
+   }
+   
    public void handleExceptionInOurTx(Invocation invocation, Throwable t, Transaction tx) throws Throwable
    {
       ApplicationException ae = TxUtil.getApplicationException(t.getClass(), invocation);

Added: projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/SessionSynchronizationInterceptor.java
===================================================================
--- projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/SessionSynchronizationInterceptor.java	                        (rev 0)
+++ projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/SessionSynchronizationInterceptor.java	2008-07-29 13:28:47 UTC (rev 76425)
@@ -0,0 +1,148 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, 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.test.tx.common;
+
+import java.rmi.RemoteException;
+
+import javax.ejb.EJBException;
+import javax.ejb.SessionSynchronization;
+import javax.transaction.RollbackException;
+import javax.transaction.Status;
+import javax.transaction.Synchronization;
+import javax.transaction.SystemException;
+import javax.transaction.Transaction;
+import javax.transaction.TransactionManager;
+
+import org.jboss.aop.advice.Interceptor;
+import org.jboss.aop.joinpoint.Invocation;
+import org.jboss.ejb3.interceptors.container.ContainerMethodInvocation;
+import org.jboss.ejb3.tx.TxUtil;
+import org.jboss.logging.Logger;
+
+/**
+ * Registers a bean which implements SessionSynchronization with the transaction.
+ * 
+ * EJB 3 4.3.7: The Optional SessionSynchronization Interface for Stateful Session Beans
+ * 
+ * FIXME: Since this class depends on internals of stateful container it can't be separated
+ *
+ * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
+ * @version $Revision: 71139 $
+ */
+public class SessionSynchronizationInterceptor implements Interceptor
+{
+   private static final Logger log = Logger.getLogger(SessionSynchronizationInterceptor.class);
+   
+   private TransactionManager tm;
+
+   public SessionSynchronizationInterceptor()
+   {
+      this.tm = TxUtil.getTransactionManager();
+   }
+
+   public String getName()
+   {
+      return null;
+   }
+
+   protected static class SFSBSessionSynchronization<T> implements Synchronization
+   {
+      private StatefulBeanContext<T> ctx;
+
+      public SFSBSessionSynchronization(StatefulBeanContext<T> ctx)
+      {
+         this.ctx = ctx;
+      }
+
+      public void beforeCompletion()
+      {
+         SessionSynchronization bean = (SessionSynchronization) ctx.getInstance();
+         try
+         {
+            bean.beforeCompletion();
+         }
+         catch (RemoteException e)
+         {
+            throw new RuntimeException(e);
+         }
+      }
+
+      public void afterCompletion(int status)
+      {
+         ctx.setTxSynchronized(false);
+         SessionSynchronization bean = (SessionSynchronization) ctx.getInstance();
+         try
+         {
+            if (status == Status.STATUS_COMMITTED)
+            {
+               bean.afterCompletion(true);
+            }
+            else
+            {
+               bean.afterCompletion(false);
+            }
+         }
+         catch (RemoteException ignore)
+         {
+         }
+         finally
+         {
+            StatefulContainer<T> container = ctx.getContainer();
+            container.getCache().release(ctx);
+         }
+      }
+   }
+
+   protected <T> void registerSessionSynchronization(StatefulBeanContext<T> ctx) throws RemoteException, SystemException
+   {
+      if (ctx.isTxSynchronized()) return;
+      Transaction tx = tm.getTransaction();
+      if (tx == null) return;
+      // tx.registerSynchronization will throw RollbackException, so no go
+      if (tx.getStatus() == Status.STATUS_MARKED_ROLLBACK) return;
+      SFSBSessionSynchronization<T> synch = new SFSBSessionSynchronization<T>(ctx);
+      try
+      {
+         tx.registerSynchronization(synch);
+      }
+      catch(RollbackException e)
+      {
+         log.warn("Unexpected RollbackException from tx " + tx + " with status " + tx.getStatus());
+         throw new EJBException(e);
+      }
+      // Notify StatefulInstanceInterceptor that the synch will take care of the release.
+      ctx.setTxSynchronized(true);
+      SessionSynchronization bean = (SessionSynchronization) ctx.getInstance();
+      // EJB 3 4.3.7 paragraph 2
+      bean.afterBegin();
+   }
+
+   public Object invoke(Invocation invocation) throws Throwable
+   {
+      StatefulBeanContext<?> target = (StatefulBeanContext<?>) ContainerMethodInvocation.getContainerMethodInvocation(invocation).getBeanContext();
+      if (target.getInstance() instanceof SessionSynchronization)
+      {
+         registerSessionSynchronization(target);
+      }
+      return invocation.invokeNext();
+   }
+}

Modified: projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/StatefulBeanContext.java
===================================================================
--- projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/StatefulBeanContext.java	2008-07-29 13:24:12 UTC (rev 76424)
+++ projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/StatefulBeanContext.java	2008-07-29 13:28:47 UTC (rev 76425)
@@ -48,9 +48,15 @@
 public class StatefulBeanContext<T> extends DummyBeanContext<T>
    implements Identifiable, org.jboss.ejb3.tx.container.StatefulBeanContext<T>
 {
+   private StatefulContainer<T> container;
    private Object id = UUID.randomUUID();
    private SimpleMetaData metaData = new SimpleMetaData();
    
+   /**
+    * Is this bean taking part of transaction synchronization?
+    */
+   private boolean txSynchronized = false;
+   
    private SessionContext sessionContext = new SessionContext()
    {
       public <T> T getBusinessObject(Class<T> businessInterface) throws IllegalStateException
@@ -144,11 +150,18 @@
     * @param instance
     * @param interceptors
     */
-   public StatefulBeanContext(T instance, List<Object> interceptors)
+   public StatefulBeanContext(StatefulContainer<T> container, T instance, List<Object> interceptors)
    {
       super(instance, interceptors);
+      
+      this.container = container;
    }
 
+   protected StatefulContainer<T> getContainer()
+   {
+      return container;
+   }
+   
    public Object getId()
    {
       return id;
@@ -163,4 +176,14 @@
    {
       return sessionContext;
    }
+   
+   protected boolean isTxSynchronized()
+   {
+      return txSynchronized;
+   }
+   
+   protected void setTxSynchronized(boolean b)
+   {
+      this.txSynchronized = b;
+   }
 }

Modified: projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/StatefulContainer.java
===================================================================
--- projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/StatefulContainer.java	2008-07-29 13:24:12 UTC (rev 76424)
+++ projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/StatefulContainer.java	2008-07-29 13:28:47 UTC (rev 76425)
@@ -96,7 +96,7 @@
             Object initargs[] = null;
             T targetObject = getBeanClass().cast(advisor.invokeNew(initargs, idx));
             
-            StatefulBeanContext<T> component = new StatefulBeanContext<T>(targetObject, ejb3Interceptors);
+            StatefulBeanContext<T> component = new StatefulBeanContext<T>(StatefulContainer.this, targetObject, ejb3Interceptors);
             
             // Do injection (sort of)
             try

Added: projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/tx/test/sessionsynchronization/SessionSyncTest.java
===================================================================
--- projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/tx/test/sessionsynchronization/SessionSyncTest.java	                        (rev 0)
+++ projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/tx/test/sessionsynchronization/SessionSyncTest.java	2008-07-29 13:28:47 UTC (rev 76425)
@@ -0,0 +1,31 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, 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.tx.test.sessionsynchronization;
+
+/**
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public interface SessionSyncTest
+{
+   String sayHi(String name);
+}

Added: projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/tx/test/sessionsynchronization/SessionSyncTestBean.java
===================================================================
--- projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/tx/test/sessionsynchronization/SessionSyncTestBean.java	                        (rev 0)
+++ projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/tx/test/sessionsynchronization/SessionSyncTestBean.java	2008-07-29 13:28:47 UTC (rev 76425)
@@ -0,0 +1,64 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, 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.tx.test.sessionsynchronization;
+
+import java.rmi.RemoteException;
+
+import javax.annotation.Resource;
+import javax.ejb.EJBContext;
+import javax.ejb.EJBException;
+import javax.ejb.Local;
+import javax.ejb.SessionSynchronization;
+import javax.ejb.Stateful;
+
+import org.jboss.ejb3.test.tx.common.MockEJBContext;
+
+/**
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+ at Stateful
+ at Local(SessionSyncTest.class)
+public class SessionSyncTestBean implements SessionSynchronization
+{
+   // Normally this is injected
+   @Resource
+   private EJBContext ctx = new MockEJBContext();
+   
+   public void afterBegin() throws EJBException, RemoteException
+   {
+   }
+
+   public void afterCompletion(boolean committed) throws EJBException, RemoteException
+   {
+   }
+
+   public void beforeCompletion() throws EJBException, RemoteException
+   {
+      ctx.setRollbackOnly();
+   }
+   
+   public String sayHi(String name)
+   {
+      return "Hi " + name;
+   }
+}

Added: projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/tx/test/sessionsynchronization/unit/SessionSyncTestCase.java
===================================================================
--- projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/tx/test/sessionsynchronization/unit/SessionSyncTestCase.java	                        (rev 0)
+++ projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/tx/test/sessionsynchronization/unit/SessionSyncTestCase.java	2008-07-29 13:28:47 UTC (rev 76425)
@@ -0,0 +1,83 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, 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.tx.test.sessionsynchronization.unit;
+
+import static org.junit.Assert.fail;
+
+import java.net.URL;
+
+import javax.ejb.EJBTransactionRolledbackException;
+
+import org.jboss.ejb3.test.tx.common.StatefulContainer;
+import org.jboss.ejb3.test.tx.mc.UnitTestBootstrap;
+import org.jboss.ejb3.tx.test.sessionsynchronization.SessionSyncTest;
+import org.jboss.ejb3.tx.test.sessionsynchronization.SessionSyncTestBean;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class SessionSyncTestCase
+{
+   private static UnitTestBootstrap bootstrap;
+   private static StatefulContainer<SessionSyncTestBean> container;
+   
+   private static URL getResource(String name)
+   {
+      return Thread.currentThread().getContextClassLoader().getResource(name);
+   }
+   
+   @BeforeClass
+   public static void setUpBeforeClass() throws Throwable
+   {
+      bootstrap = new UnitTestBootstrap();
+      bootstrap.deploy(getResource("instance/beans.xml"));
+      
+      container = new StatefulContainer<SessionSyncTestBean>("SessionSyncTest", "Stateful Container", SessionSyncTestBean.class);
+   }
+
+   @AfterClass
+   public static void tearDownAfterClass() throws Exception
+   {
+      if(bootstrap != null)
+         bootstrap.shutdown();
+   }
+
+   @Test
+   public void test1() throws Throwable
+   {
+      SessionSyncTest bean = container.constructProxy(SessionSyncTest.class);
+      
+      try
+      {
+         bean.sayHi("me");
+         fail("Should have thrown an EJBTransactionRolledbackException");
+      }
+      catch(EJBTransactionRolledbackException e)
+      {
+         // whoopie
+      }
+   }
+}

Modified: projects/ejb3/trunk/transactions/src/test/resources/instance/jboss-aop.xml
===================================================================
--- projects/ejb3/trunk/transactions/src/test/resources/instance/jboss-aop.xml	2008-07-29 13:24:12 UTC (rev 76424)
+++ projects/ejb3/trunk/transactions/src/test/resources/instance/jboss-aop.xml	2008-07-29 13:28:47 UTC (rev 76425)
@@ -1,6 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <aop>
    <interceptor class="org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor" scope="PER_VM"/>
+   <interceptor class="org.jboss.ejb3.test.tx.common.SessionSynchronizationInterceptor" scope="PER_VM"/>
    <interceptor class="org.jboss.ejb3.test.tx.common.StatefulInstanceInterceptor" scope="PER_VM"/>
    <interceptor factory="org.jboss.ejb3.tx.BMTTxInterceptorFactory" scope="PER_CLASS_JOINPOINT"/>
    <interceptor factory="org.jboss.ejb3.tx.CMTTxInterceptorFactory" scope="PER_CLASS_JOINPOINT"/>
@@ -22,10 +23,8 @@
          <interceptor-ref name="org.jboss.ejb3.test.tx.common.StatefulInstanceInterceptor"/>
          <interceptor-ref name="org.jboss.ejb3.tx.BMTTxInterceptorFactory"/>
       </bind>
-      <!--
       <bind pointcut="execution(public * $instanceof{javax.ejb.SessionSynchronization}->*(..))">
-         <interceptor-ref name="org.jboss.ejb3.stateful.SessionSynchronizationInterceptor"/>
+         <interceptor-ref name="org.jboss.ejb3.test.tx.common.SessionSynchronizationInterceptor"/>
       </bind>
-      -->
    </domain>
 </aop>
\ No newline at end of file




More information about the jboss-cvs-commits mailing list