[jbpm-commits] JBoss JBPM SVN: r2746 - in projects/spec/trunk/modules: ri/src/main/java/org/jbpm/ri/runtime and 1 other directory.
do-not-reply at jboss.org
do-not-reply at jboss.org
Sat Nov 1 14:23:50 EDT 2008
Author: thomas.diesler at jboss.com
Date: 2008-11-01 14:23:50 -0400 (Sat, 01 Nov 2008)
New Revision: 2746
Modified:
projects/spec/trunk/modules/cts/src/test/java/org/jbpm/test/cts/transaction/TxRequiredTest.java
projects/spec/trunk/modules/ri/src/main/java/org/jbpm/ri/runtime/TransactionInterceptor.java
Log:
Tx ok
Modified: projects/spec/trunk/modules/cts/src/test/java/org/jbpm/test/cts/transaction/TxRequiredTest.java
===================================================================
--- projects/spec/trunk/modules/cts/src/test/java/org/jbpm/test/cts/transaction/TxRequiredTest.java 2008-11-01 17:37:34 UTC (rev 2745)
+++ projects/spec/trunk/modules/cts/src/test/java/org/jbpm/test/cts/transaction/TxRequiredTest.java 2008-11-01 18:23:50 UTC (rev 2746)
@@ -55,39 +55,39 @@
public void testNoRollback() throws Exception
{
ProcessDefinition procDef = unregisterOnTearDown(getProcessDefinition());
-
+
Node taskA = procDef.getNode("TaskA");
Node taskB = procDef.getNode("TaskB");
Group group = procDef.getGroup("TxRequired");
Group groupA = taskA.getGroupRef();
Group groupB = taskB.getGroupRef();
-
+
assertNotNull("Group not null", group);
assertSame("Group same", group, groupA);
assertSame("Group same", group, groupB);
-
+
Process proc = procDef.newInstance();
proc.startProcess();
proc.waitForEnd();
-
+
List<Message> messages = getMessages();
assertEquals("Message expected", 1, messages.size());
}
- public void _testRollback() throws Exception
+ public void testRollback() throws Exception
{
ProcessDefinition procDef = unregisterOnTearDown(getProcessDefinition());
Process proc = procDef.newInstance();
BasicAttachments att = new BasicAttachments();
att.addAttachment(Boolean.class, "rollback", Boolean.TRUE);
-
+
proc.startProcess(att);
proc.waitForEnd(5000);
-
+
List<Message> messages = getMessages();
- assertEquals("Two messages expected", 2, messages.size());
+ assertEquals("No messages expected", 0, messages.size());
}
protected ProcessDefinition getProcessDefinition() throws IOException
@@ -102,23 +102,20 @@
procBuilder.addProcessMessage("TaskAMessage").addToRef(getTestID()).addProperty("msgProp", "msgA");
return procBuilder.getProcessDefinition();
}
-
+
public static class TextExecutionHandler implements ExecutionHandler
{
private static final long serialVersionUID = 1L;
-
+
private Node node;
-
+
@Override
public void execute(Token token)
{
Transaction tx = TransactionAssociation.getTransaction();
- if (tx != null)
- {
- Boolean doRollback = token.getExecutionContext().getAttachment(Boolean.class, "rollback");
- if (doRollback == Boolean.TRUE)
- tx.rollback();
- }
+ Boolean doRollback = token.getExecutionContext().getAttachment(Boolean.class, "rollback");
+ if (doRollback == Boolean.TRUE)
+ tx.rollback();
}
@Override
Modified: projects/spec/trunk/modules/ri/src/main/java/org/jbpm/ri/runtime/TransactionInterceptor.java
===================================================================
--- projects/spec/trunk/modules/ri/src/main/java/org/jbpm/ri/runtime/TransactionInterceptor.java 2008-11-01 17:37:34 UTC (rev 2745)
+++ projects/spec/trunk/modules/ri/src/main/java/org/jbpm/ri/runtime/TransactionInterceptor.java 2008-11-01 18:23:50 UTC (rev 2746)
@@ -25,8 +25,14 @@
import org.hibernate.Session;
import org.hibernate.Transaction;
+import org.jbpm.api.Constants;
+import org.jbpm.api.NotImplementedException;
+import org.jbpm.api.Constants.TxType;
import org.jbpm.api.client.ProcessEngine;
+import org.jbpm.api.model.Group;
import org.jbpm.api.model.Node;
+import org.jbpm.api.model.Property;
+import org.jbpm.api.model.Group.GroupType;
import org.jbpm.api.runtime.Token;
import org.jbpm.api.service.PersistenceService;
import org.slf4j.Logger;
@@ -47,12 +53,31 @@
{
Node node = rtContext.getNode();
Token token = rtContext.getToken();
+ Session session = token.getSession();
- // Begin the Tx
- Session session = token.getSession();
- Transaction tx = session.beginTransaction();
+ // Get the Thread associated Tx
+ Transaction tx = TransactionAssociation.getTransaction();
+
+ TxType txType = getTxType(node);
+ if (txType == TxType.REQUIRESNEW)
+ {
+ tx = session.beginTransaction();
+ }
+ else if (txType == TxType.REQUIRED)
+ {
+ // Only create a new Tx if there is none already
+ if (tx == null)
+ tx = session.beginTransaction();
+ }
+ else
+ {
+ throw new NotImplementedException("NotImplemented: " + txType);
+ }
+
+ // Associate the Tx with the Thread
TransactionAssociation.setTransaction(tx);
+ TxType txTypeNext = null;
try
{
// Load the node
@@ -63,9 +88,22 @@
// Call the next interceptor in the chain
rtContext.next();
- // Save the node and commit the Tx
+ // Save the node
service.saveNode(session, node);
- tx.commit();
+
+ // Get TxType of the next node
+ String targetRef = token.getSequenceFlow().getTargetRef();
+ if (targetRef != null)
+ {
+ Node nextNode = node.getProcess().getNode(targetRef);
+ txTypeNext = getTxType(nextNode);
+ }
+
+ // Commit the Tx if it was not rolled back already
+ if (!tx.wasRolledBack() && txTypeNext != TxType.REQUIRED)
+ {
+ tx.commit();
+ }
}
catch (RuntimeException rte)
{
@@ -82,7 +120,28 @@
}
finally
{
- TransactionAssociation.setTransaction(null);
+ // Reset the associated Tx
+ if (txTypeNext != TxType.REQUIRED)
+ TransactionAssociation.setTransaction(null);
}
}
+
+ private TxType getTxType(Node node)
+ {
+ // Initialize with the default
+ TxType txType = TxType.REQUIRESNEW;
+
+ // Get the TxType from the group
+ Group groupRef = node.getGroupRef();
+ if (groupRef != null && groupRef.getGroupType() == GroupType.Transaction)
+ {
+ Property typeProp = groupRef.getProperty(Constants.PROP_TX_TYPE);
+ if (typeProp == null)
+ throw new IllegalStateException("Cannot obtain '" + Constants.PROP_TX_TYPE + "' from group: " + groupRef);
+
+ txType = Constants.TxType.valueOf(typeProp.getValue());
+ }
+
+ return txType;
+ }
}
More information about the jbpm-commits
mailing list