<!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: Completing a Task Fails after Calling statefulSession.dispose
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="http://community.jboss.org/people/williajd">James Williams</a> in <i>jBPM</i> - <a href="http://community.jboss.org/message/621903#621903">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>OK. I've fixed it. I had to write my own version of the PersistenceContextManager.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>import javax.persistence.EntityManager;</p><p>import javax.persistence.EntityManagerFactory;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>import org.drools.persistence.PersistenceContext;</p><p>import org.drools.persistence.jpa.JpaPersistenceContext;</p><p>import org.drools.runtime.Environment;</p><p>import org.drools.runtime.EnvironmentName;</p><p>import org.jbpm.persistence.JpaProcessPersistenceContext;</p><p>import org.jbpm.persistence.ProcessPersistenceContext;</p><p>import org.jbpm.persistence.ProcessPersistenceContextManager;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>public class MultipleUseJpaPersistenceContextManager implements ProcessPersistenceContextManager {</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; Environment env;</p><p>&#160;&#160;&#160; private EntityManagerFactory emf;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; private EntityManager appScopedEntityManager;</p><p>&#160;&#160;&#160; protected EntityManager cmdScopedEntityManager;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; private boolean internalAppScopedEntityManager;</p><p>&#160;&#160;&#160; private boolean internalCmdScopedEntityManager;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; public MultipleUseJpaPersistenceContextManager(Environment env) {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.env = env;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.emf = (EntityManagerFactory) env.get(EnvironmentName.ENTITY_MANAGER_FACTORY);</p><p>&#160;&#160;&#160; }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; public PersistenceContext getApplicationScopedPersistenceContext() {</p><p><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160; checkAppScopedEntityManager();</strong></p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; return new JpaPersistenceContext(appScopedEntityManager);</p><p>&#160;&#160;&#160; }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; private void checkAppScopedEntityManager() {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (this.appScopedEntityManager == null) {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; // Use the App scoped EntityManager if the user has provided it, and it is open.</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.appScopedEntityManager = (EntityManager) this.env.get(EnvironmentName.APP_SCOPED_ENTITY_MANAGER);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (this.appScopedEntityManager != null &amp;&amp; !this.appScopedEntityManager.isOpen()) {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; throw new RuntimeException("Provided APP_SCOPED_ENTITY_MANAGER is not open");</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (this.appScopedEntityManager == null) {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; internalAppScopedEntityManager = true;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.appScopedEntityManager = this.emf.createEntityManager();</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.env.set(EnvironmentName.APP_SCOPED_ENTITY_MANAGER, this.appScopedEntityManager);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; } else {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; internalAppScopedEntityManager = false;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p>&#160;&#160;&#160; }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; public PersistenceContext getCommandScopedPersistenceContext() {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; return new JpaPersistenceContext(this.cmdScopedEntityManager);</p><p>&#160;&#160;&#160; }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; public void beginCommandScopedEntityManager() {</p><p><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160; checkAppScopedEntityManager();</strong></p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; EntityManager cmdScopedEntityManager = (EntityManager) env.get(EnvironmentName.CMD_SCOPED_ENTITY_MANAGER);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (cmdScopedEntityManager == null</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; || (this.cmdScopedEntityManager != null &amp;&amp; !this.cmdScopedEntityManager.isOpen())) {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; internalCmdScopedEntityManager = true;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.cmdScopedEntityManager = this.emf.createEntityManager(); // no need to call joinTransaction as it will</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; // do so if one already exists</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.env.set(EnvironmentName.CMD_SCOPED_ENTITY_MANAGER, this.cmdScopedEntityManager);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; cmdScopedEntityManager = this.cmdScopedEntityManager;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; } else {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; internalCmdScopedEntityManager = false;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; cmdScopedEntityManager.joinTransaction();</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; appScopedEntityManager.joinTransaction();</p><p>&#160;&#160;&#160; }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; public void endCommandScopedEntityManager() {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (this.internalCmdScopedEntityManager) {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.env.set(EnvironmentName.CMD_SCOPED_ENTITY_MANAGER, null);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p>&#160;&#160;&#160; }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; public void dispose() {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (this.internalAppScopedEntityManager) {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (this.appScopedEntityManager != null &amp;&amp; this.appScopedEntityManager.isOpen()) {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.appScopedEntityManager.close();</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.internalAppScopedEntityManager = false;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.env.set(EnvironmentName.APP_SCOPED_ENTITY_MANAGER, null);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.appScopedEntityManager = null;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (this.internalCmdScopedEntityManager) {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (this.cmdScopedEntityManager != null &amp;&amp; this.cmdScopedEntityManager.isOpen()) {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.cmdScopedEntityManager.close();</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.internalCmdScopedEntityManager = false;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.env.set(EnvironmentName.CMD_SCOPED_ENTITY_MANAGER, null);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.cmdScopedEntityManager = null;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p>&#160;&#160;&#160; }</p><p>&#160;&#160;&#160;&#160;&#160; </p><p>&#160;&#160;&#160; @Override</p><p>&#160;&#160;&#160; public ProcessPersistenceContext getProcessPersistenceContext() {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (cmdScopedEntityManager == null) {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.emf.createEntityManager();;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; return new JpaProcessPersistenceContext(cmdScopedEntityManager);</p><p>&#160;&#160;&#160; }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>This version checks that it has a valid appScopedEntityManager and creates one if necessary. </p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I then make sure this one is used instead of the default one provided by JBPM when creating my knowledgeSession:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Environment env = KnowledgeBaseFactory.newEnvironment();</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, entityManagerFactory);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; InitialContext ctx = new InitialContext();</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; UserTransaction transactionManager = (UserTransaction) ctx.lookup("java:comp/UserTransaction");</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <strong> env.set(EnvironmentName.TRANSACTION_MANAGER, </strong></p><p><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; new JtaTransactionManager(transactionManager, null, transactionManager));</strong></p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <strong>env.set(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER, </strong></p><p><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; new MultipleUseJpaPersistenceContextManager(env));</strong></p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; StatefulKnowledgeSession knowledgeSession = JPAKnowledgeService.newStatefulKnowledgeSession(knowledgeBase, null, env);</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I hope this helps others.</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/621903#621903">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>