[jbpm-commits] JBoss JBPM SVN: r2970 - in jbpm3/branches/jpdl-3.2.2-SOA-4.2: enterprise/src/test/java/org/jbpm/persistence and 2 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Tue Nov 18 02:02:47 EST 2008


Author: alex.guizar at jboss.com
Date: 2008-11-18 02:02:47 -0500 (Tue, 18 Nov 2008)
New Revision: 2970

Added:
   jbpm3/branches/jpdl-3.2.2-SOA-4.2/enterprise/src/test/java/org/jbpm/persistence/
   jbpm3/branches/jpdl-3.2.2-SOA-4.2/enterprise/src/test/java/org/jbpm/persistence/jta/
   jbpm3/branches/jpdl-3.2.2-SOA-4.2/enterprise/src/test/java/org/jbpm/persistence/jta/JtaDbPersistenceTest.java
Modified:
   jbpm3/branches/jpdl-3.2.2-SOA-4.2/jpdl/jar/src/main/java/org/jbpm/persistence/jta/JtaDbPersistenceService.java
   jbpm3/branches/jpdl-3.2.2-SOA-4.2/jpdl/jar/src/main/java/org/jbpm/persistence/jta/JtaDbPersistenceServiceFactory.java
Log:
ported JBPM-1179 to soa branch

Added: jbpm3/branches/jpdl-3.2.2-SOA-4.2/enterprise/src/test/java/org/jbpm/persistence/jta/JtaDbPersistenceTest.java
===================================================================
--- jbpm3/branches/jpdl-3.2.2-SOA-4.2/enterprise/src/test/java/org/jbpm/persistence/jta/JtaDbPersistenceTest.java	                        (rev 0)
+++ jbpm3/branches/jpdl-3.2.2-SOA-4.2/enterprise/src/test/java/org/jbpm/persistence/jta/JtaDbPersistenceTest.java	2008-11-18 07:02:47 UTC (rev 2970)
@@ -0,0 +1,203 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt 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.jbpm.persistence.jta;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.transaction.UserTransaction;
+
+import org.apache.cactus.ServletTestCase;
+import org.jbpm.JbpmConfiguration;
+import org.jbpm.JbpmContext;
+import org.jbpm.graph.def.ProcessDefinition;
+import org.jbpm.graph.exe.ProcessInstance;
+
+public class JtaDbPersistenceTest extends ServletTestCase {
+
+  private JbpmConfiguration jbpmConfiguration;
+  private UserTransaction tx;
+  private boolean rollback;
+
+  private static long definitionId;
+
+  protected void setUp() throws Exception {
+    jbpmConfiguration = JbpmConfiguration.getInstance();
+    getUserTransaction();
+  }
+
+  public void testUserTx() throws Exception {
+    tx = getUserTransaction();
+    testServiceTx();
+  }
+
+  public void testUserTxRollback() throws Exception {
+    tx = getUserTransaction();
+    testServiceTxRollback();
+  }
+
+  public void testServiceTx() throws Exception {
+    long definitionId = deployProcess();
+    long instanceId = launchProcess(definitionId);
+    signal(instanceId);
+    assertTrue(hasEnded(instanceId));
+  }
+
+  public void testServiceTxRollback() throws Exception {
+    rollback = true;
+    long definitionId = deployProcess();
+    long instanceId = launchProcess(definitionId);
+    signal(instanceId);
+    assertFalse(hasEnded(instanceId));
+  }
+
+  private long deployProcess() throws Exception {
+    if (definitionId == 0) {
+      try {
+        if (tx != null) tx.begin();
+        JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
+        try {
+          assertEquals(tx == null, isTxCreatedByService(jbpmContext));
+          ProcessDefinition definition = ProcessDefinition.parseXmlString("<process-definition name='tx'>"
+              + "  <start-state name='start'>"
+              + "    <transition to='midway' />"
+              + "  </start-state>"
+              + "  <state name='midway'>"
+              + "    <transition to='end' />"
+              + "  </state>"
+              + "  <end-state name='end' />"
+              + "</process-definition>");
+          jbpmContext.deployProcessDefinition(definition);
+          definitionId = definition.getId();
+        }
+        catch (RuntimeException e) {
+          if (tx == null) jbpmContext.setRollbackOnly();
+          throw e;
+        }
+        finally {
+          jbpmContext.close();
+        }
+        if (tx != null) tx.commit();
+      }
+      catch (Exception e) {
+        if (tx != null) tx.rollback();
+        throw e;
+      }
+    }
+    return definitionId;
+  }
+
+  private long launchProcess(long definitionId) throws Exception {
+    try {
+      if (tx != null) tx.begin();
+      JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
+      ProcessInstance instance;
+      try {
+        assertEquals(tx == null, isTxCreatedByService(jbpmContext));
+        ProcessDefinition definition = jbpmContext.getGraphSession().loadProcessDefinition(
+            definitionId);
+        instance = new ProcessInstance(definition);
+        instance.signal();
+        jbpmContext.save(instance);
+      }
+      catch (RuntimeException e) {
+        if (tx == null) jbpmContext.setRollbackOnly();
+        throw e;
+      }
+      finally {
+        jbpmContext.close();
+      }
+      if (tx != null) tx.commit();
+      return instance.getId();
+    }
+    catch (Exception e) {
+      if (tx != null) tx.rollback();
+      throw e;
+    }
+  }
+
+  private void signal(long instanceId) throws Exception {
+    try {
+      if (tx != null) tx.begin();
+      JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
+      try {
+        assertEquals(tx == null, isTxCreatedByService(jbpmContext));
+        ProcessInstance instance = jbpmContext.loadProcessInstanceForUpdate(instanceId);
+        instance.signal();
+        if (rollback && tx == null) jbpmContext.setRollbackOnly();
+      }
+      catch (RuntimeException e) {
+        if (tx == null) jbpmContext.setRollbackOnly();
+        throw e;
+      }
+      finally {
+        jbpmContext.close();
+      }
+      if (tx != null) {
+        if (rollback) 
+          tx.rollback();
+        else
+          tx.commit();
+      }
+    }
+    catch (Exception e) {
+      if (tx != null) tx.rollback();
+      throw e;
+    }
+  }
+
+  private boolean hasEnded(long instanceId) throws Exception {
+    try {
+      if (tx != null) tx.begin();
+      JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
+      ProcessInstance instance;
+      try {
+        assertEquals(tx == null, isTxCreatedByService(jbpmContext));
+        instance = jbpmContext.loadProcessInstanceForUpdate(instanceId);
+      }
+      catch (RuntimeException e) {
+        if (tx == null) jbpmContext.setRollbackOnly();
+        throw e;
+      }
+      finally {
+        jbpmContext.close();
+      }
+      if (tx != null) tx.commit();
+      return instance.hasEnded();
+    }
+    catch (Exception e) {
+      if (tx != null) tx.rollback();
+      throw e;
+    }
+  }
+
+  private boolean isTxCreatedByService(JbpmContext jbpmContext) {
+    JtaDbPersistenceService persistenceService = (JtaDbPersistenceService) jbpmContext.getServices()
+        .getPersistenceService();
+    return persistenceService.isJtaTxCreated();
+  }
+
+  private static UserTransaction getUserTransaction() throws NamingException {
+    Context initial = new InitialContext();
+    return (UserTransaction) initial.lookup("java:comp/UserTransaction");
+  }
+}

Modified: jbpm3/branches/jpdl-3.2.2-SOA-4.2/jpdl/jar/src/main/java/org/jbpm/persistence/jta/JtaDbPersistenceService.java
===================================================================
--- jbpm3/branches/jpdl-3.2.2-SOA-4.2/jpdl/jar/src/main/java/org/jbpm/persistence/jta/JtaDbPersistenceService.java	2008-11-18 00:50:30 UTC (rev 2969)
+++ jbpm3/branches/jpdl-3.2.2-SOA-4.2/jpdl/jar/src/main/java/org/jbpm/persistence/jta/JtaDbPersistenceService.java	2008-11-18 07:02:47 UTC (rev 2970)
@@ -1,6 +1,28 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt 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.jbpm.persistence.jta;
 
 import javax.naming.InitialContext;
+import javax.naming.NamingException;
 import javax.transaction.SystemException;
 import javax.transaction.UserTransaction;
 
@@ -19,55 +41,45 @@
   
   private static Log log = LogFactory.getLog(JbpmContext.class);  
   
-  boolean isJtaTxCreated = false;
+  private UserTransaction userTransaction;
 
   public JtaDbPersistenceService(DbPersistenceServiceFactory persistenceServiceFactory) {
     super(persistenceServiceFactory);
     
-    if (! isCurrentJtaTransactionAvailable()) {
+    if (!isJtaTransactionInProgress()) {
       beginJtaTransaction();
-      isJtaTxCreated = true;
     }
   }
 
   protected boolean isTransactionActive() {
-      return isJtaTxCreated() ; 
+    return isJtaTxCreated() ; 
   }
   
   public void close() {
     super.close();
 
-    if (isJtaTxCreated) {
+    if (userTransaction != null) {
       endJtaTransaction();
     }
   }
 
-  boolean isCurrentJtaTransactionAvailable() {
-    SessionFactoryImplementor sessionFactoryImplementor = (SessionFactoryImplementor) persistenceServiceFactory.getSessionFactory();
-    return JTAHelper.isTransactionInProgress(sessionFactoryImplementor);
+  boolean isJtaTransactionInProgress() {
+    SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) persistenceServiceFactory.getSessionFactory();
+    return JTAHelper.isTransactionInProgress(sessionFactory);
   }
 
   void beginJtaTransaction() {
     try {
     	log.debug("start user JTA transaction");
-      getUserTransaction().begin();
+    	userTransaction = getUserTransaction();
+      userTransaction.begin();
     } catch (Exception e) {
       throw new JbpmException("couldn't start JTA transaction", e);
     }
   }
 
   void endJtaTransaction() {
-    int status = -1;
-	log.debug("end user JTA transaction");
-    UserTransaction userTransaction = getUserTransaction();
-    try {
-      status = userTransaction.getStatus();
-    } catch (SystemException e) {
-      throw new JbpmException("couldn't get status for user transaction", e); 
-    }
-    
-    boolean isRollback = JTAHelper.isRollback(status);
-    if (isRollback || isRollbackOnly()) {
+    if (isRollbackOnly() || JTAHelper.isMarkedForRollback(getJtaTransactionStatus())) {
       log.debug("end jta transation with ROLLBACK");
       try {
         userTransaction.rollback();
@@ -83,25 +95,37 @@
       }
     }
   }
-  
+
   UserTransaction getUserTransaction() {
-    UserTransaction userTransaction = null;
-    if (userTransaction == null) {
-      String jndiName = "UserTransaction";
-      try {
-        userTransaction = (UserTransaction) new InitialContext().lookup(jndiName);
-      } catch (Exception e) {
-        throw new JbpmException("couldn't lookup UserTransaction in JNDI with name "+jndiName, e);
-      }
+    String jndiName = persistenceServiceFactory.getConfiguration().getProperty("jta.UserTransaction");
+    if (jndiName == null) {
+      /*
+       * EJB 2.1 section 20.9 The container must make the UserTransaction interface available to
+       * enterprise beans that are allowed to use this interface (only session and message-
+       * driven beans with bean-managed transaction demarcation are allowed to use this 
+       * interface) in JNDI under the name java:comp/UserTransaction.
+       * J2EE 1.4 section 4.2.1.1 The J2EE platform must provide an object implementing the
+       * UserTransaction interface to all web components. The platform must publish the 
+       * UserTransaction object in JNDI under the name java:comp/UserTransaction.
+       */
+      jndiName = "java:comp/UserTransaction";
     }
-    return userTransaction;
+    try {
+      return (UserTransaction) new InitialContext().lookup(jndiName);
+    } catch (NamingException e) {
+      throw new JbpmException("couldn't lookup UserTransaction in JNDI with name "+jndiName, e);
+    }
   }
 
+  int getJtaTransactionStatus() {
+    try {
+      return userTransaction.getStatus();
+    } catch (SystemException e) {
+      throw new JbpmException("couldn't get status for user transaction", e); 
+    }
+  }
 
   public boolean isJtaTxCreated() {
-    return isJtaTxCreated;
+    return userTransaction != null;
   }
-  public void setJtaTxCreated(boolean isJtaTxCreated) {
-    this.isJtaTxCreated = isJtaTxCreated;
-  }
 }

Modified: jbpm3/branches/jpdl-3.2.2-SOA-4.2/jpdl/jar/src/main/java/org/jbpm/persistence/jta/JtaDbPersistenceServiceFactory.java
===================================================================
--- jbpm3/branches/jpdl-3.2.2-SOA-4.2/jpdl/jar/src/main/java/org/jbpm/persistence/jta/JtaDbPersistenceServiceFactory.java	2008-11-18 00:50:30 UTC (rev 2969)
+++ jbpm3/branches/jpdl-3.2.2-SOA-4.2/jpdl/jar/src/main/java/org/jbpm/persistence/jta/JtaDbPersistenceServiceFactory.java	2008-11-18 07:02:47 UTC (rev 2970)
@@ -1,3 +1,24 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt 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.jbpm.persistence.jta;
 
 import org.jbpm.persistence.db.DbPersistenceServiceFactory;
@@ -3,5 +24,23 @@
 import org.jbpm.svc.Service;
 
-
+/**
+ * The JTA persistence service enables jBPM to participate in JTA transactions.
+ * If an existing transaction is underway, {@link JtaDbPersistenceService} 
+ * clings to it; otherwise it starts a new transaction.
+ * 
+ * <h3>Configuration</h3>
+ * 
+ * The JTA persistence service factory has the configurable fields described
+ * below.
+ * 
+ * <ul>
+ * <li><code>isCurrentSessionEnabled</code></li>
+ * <li><code>isTransactionEnabled</code></li>
+ * </ul>
+ * 
+ * Refer to the jBPM manual for details.
+ * 
+ * @author Tom Baeyens
+ */
 public class JtaDbPersistenceServiceFactory extends DbPersistenceServiceFactory {
 
@@ -10,13 +49,10 @@
 
   public JtaDbPersistenceServiceFactory() {
     setCurrentSessionEnabled(true);
-    setTransactionEnabled(true);
+    setTransactionEnabled(false);
   }
   
   public Service openService() {
     return new JtaDbPersistenceService(this);
   }
-
-  public void close() {
-  }
 }




More information about the jbpm-commits mailing list