<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    Re: jboss as 5 jbpm 5.1 persistence
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="http://community.jboss.org/people/zorin">Victor Zorin</a> in <i>jBPM</i> - <a href="http://community.jboss.org/message/603549#603549">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>Note sure if you find any hints here, but this is the way we currently handle similar situations, I mean bringing together EJB Session transaction management and JBPM5. </p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>What we found is that the major issue with JBPM StatefulKnowledgeSession is that it does not attach itself to CMP transaction during the startProcess(...) or signal(...). Playing with Bitronix Transaction manager did bring much help.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>However, any subsequent calls from the workflow to EJB Session methods work as they should (i.e. JBoss's transaction manager attaches itself well to a transaction that is started from process instance). For example, transaction is executed properly when making a call from Script node to local or remote interface of an EJB session.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>So, without breaking the framework, and until a proper fix or better workaround is found, we use the following approach to transaction management:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>When invoking ksession.startProcess(...) or signal(...) from within CMP Session, mark relevant EJB session method as @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED), and do manual transaction management of your records, while making sure that ksession methods are invoked without the transaction, eg.:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>@Stateless</p><p>public class InstanceManager implements InstanceManagerRemote,</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; InstanceManagerLocal {</p><p>...</p><p>@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED</p><p>public long createLicenseManagementProcess(String procId, ...)</p><p>{</p><p>&#160;&#160;&#160;&#160; ...</p><p>&#160;&#160;&#160;&#160; // create workflow instance, WITHOUT TX!!!!</p><p>&#160;&#160;&#160; ProcessInstance inst = ksession.startProcess(procId);</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; // handle your own EJB Entity records</p><p>&#160;&#160;&#160; UserTransaction tx = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");</p><p>&#160;&#160;&#160; logger.info("have UserTransaction " + tx.toString());</p><p>&#160;&#160;&#160; tx.begin();</p><p>&#160;&#160;&#160; LicenseEntity entity = new LicenseEntity();</p><p>&#160;&#160;&#160; entity.setWorkflowInstanceId(inst.getId());</p><p>&#160;&#160;&#160; ...</p><p>&#160;&#160;&#160; em.persist();</p><p>&#160;&#160;&#160; tx.commit();</p><p>}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>// this is the method which is called from ProcessEventListener</p><p>public void callbackProcessCompleted(long workflowId)</p><p>{</p><p>&#160; // no need to do manual transaction handling</p><p>&#160; ... update your own record status using EntityManager directly</p><p>}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>// this is the method which is called from BPMN Script via Local interface</p><p>public void paymentMade(long workflowId, ....)</p><p>{</p><p>&#160; // no need to do manual transaction handling</p><p>&#160; ... create payment record status using EntityManager directly</p><p>}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>} // end of stateless session bean</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Here is an important bit. </p><p>If the first node of a workflow tries to get back to EJB Session, make sure that your data is already there and commited to the database.</p><p>Have a look again at createLicenseManagementProcess(...) method.</p><p>- First step: we create a process, by calling startProcess(...).</p><p>- Second step: we create own record by storing the workflow Id of a created process.</p><p>- If the first node in a workflow is a Script which tries to get back to and call any method, such as paymentMade(long workflowId), transaction will work properly, but data will not be there yet. </p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>One of the solutions/tricks, is breaking the transaction by breaking the workflow via making a first node to be an Event Node waiting for 'start' event. See extended code below:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; // create workflow instance, WITHOUT TX!!!!</p><p>&#160;&#160;&#160; // but now this workflow will stop on first Event Node waiting for 'start' signal</p><p>&#160;&#160;&#160; ProcessInstance inst = ksession.startProcess(procId);</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; // handle your own EJB Entity records</p><p>&#160;&#160;&#160; ...</p><p>&#160;&#160;&#160; tx.commit();</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; // all data is commited, now we can kick-start the workflow</p><p>&#160;&#160;&#160; ksession.signalEvent("start", null, inst.getId());</p><p>&#160;&#160;&#160; // this is the end of startPrtocess method</p><p>}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>[Disclaimer: We did not have a lot of time to do deep investigation why things do not work as we think they should, so we had to quickly come up with own approach to persistence, transaction handling, clustering and safe restarts of JBoss AS. This approach may limit the use of BPMN and JBPM API, but can not bend the rules. So when a better solution is found, system would remain backward compatible.]</p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/603549#603549">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in jBPM at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>