<!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="https://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;">
    jBPM session persistence - "Removing a detached instance" encountered under load / concurrency
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="https://community.jboss.org/people/xmrk">Mark Egan</a> in <i>jBPM</i> - <a href="https://community.jboss.org/message/778981#778981">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>I'm encountering an exception with jbpm-persistence-jpa when performing load testing on an application-embedded jBPM.&#160; The stack trace is:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Caused by: java.lang.IllegalArgumentException: Removing a detached instance org.drools.persistence.info.WorkItemInfo#234</p><p>&#160;&#160;&#160;&#160; at org.hibernate.ejb.event.EJB3DeleteEventListener.performDetachedEntityDeletionCheck(EJB3DeleteEventListener.java:67) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]</p><p>&#160;&#160;&#160;&#160; at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:107) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]</p><p>&#160;&#160;&#160;&#160; at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:74) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]</p><p>&#160;&#160;&#160;&#160; at org.hibernate.internal.SessionImpl.fireDelete(SessionImpl.java:822) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]</p><p>&#160;&#160;&#160;&#160; at org.hibernate.internal.SessionImpl.delete(SessionImpl.java:801) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]</p><p>&#160;&#160;&#160;&#160; at org.hibernate.ejb.AbstractEntityManagerImpl.remove(AbstractEntityManagerImpl.java:883) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]</p><p>&#160;&#160;&#160;&#160; at org.drools.persistence.jpa.JpaPersistenceContext.remove(JpaPersistenceContext.java:54) [drools-persistence-jpa-5.5.0.Final.jar:5.5.0.Final]</p><p>&#160;&#160;&#160;&#160; at org.drools.persistence.jpa.processinstance.JPAWorkItemManager.completeWorkItem(JPAWorkItemManager.java:123) [drools-persistence-jpa-5.5.0.Final.jar:5.5.0.Final]</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I'm using 5.4.0.Final (however I observed the same behavior in 5.3.0.Final and 5.4.0.CR1).&#160; The application server is JBoss AS 7.&#160; I'm using MySQL InnoDB having row-locking enabled.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>The process calls EJBs within separate WorkItemHandlers.&#160; Here's the basic process:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>1) start process,</p><p>2) call a local, no-interface EJB (injected with @Inject) and persist a domain object (using a separate persistence context),</p><p>3) lookup another EJB (using InitialContext.doLookup) and perform some synchorous work,</p><p>4) end process.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I'm initiating the process from a REST endpoint.&#160; The jBPM session.startProcess(...) occurs in a Local EJB exposed service.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>The load test calls the REST endpoint.&#160; I'm using jMeter.&#160; The thread group only consists of 2 threads.&#160; There is a short ramp-up.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Here's a rundown of my observations:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>During ramp-up where the process service is a single thread, no issues are observed.&#160; Once the second thread spins up, the exception is received on all subsequent calls.&#160; The WorkItemInfo which fails to be removed is for step #2 (the local EJB call to a separate persistence service).&#160; If I remove the third step from the definition, the issue is not observed.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Here's my Environment for the JPAKnowledgeService.newStatefulKnowledgeSession(...).&#160; </p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>@Produces</p><p>public Environment getEnvironment() {</p><p>&#160;&#160;&#160; Environment env = KnowledgeBaseFactory.newEnvironment();</p><p>&#160;&#160;&#160; env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);</p><p>&#160;&#160;&#160; env.set(EnvironmentName.TRANSACTION_MANAGER, new ContainerManagedTransactionManager());</p><p>&#160;&#160;&#160; env.set(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER, new JpaProcessPersistenceContextManager(env));</p><p>&#160;&#160;&#160; return env;</p><p>}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I'm only using a session once and dispose of it after a single process completes.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I'm setting the following for my persistence context:</p><p>hibernate.transaction.factory_class=org.hibernate.transaction.CMTTransactionFactory</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I understand that what I'm encountering could be related to general container handled transactions across my EJB calls, but I wanted to start with the jBPM community.&#160; I had already investigated the transaction handling on those EJB calls and wasn't able to resolve the issue using various @TransactionAttribute or alterations to the persistence context's properties.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><span>I also reviewed the persistence chapter in the 5.4 User Guide: </span><a class="jive-link-external-small" href="http://docs.jboss.org/jbpm/v5.4/userguide/ch.core-persistence.html" target="_blank">http://docs.jboss.org/jbpm/v5.4/userguide/ch.core-persistence.html</a><span>, as well as the Multi-Threading section: </span><a class="jive-link-external-small" href="http://docs.jboss.org/jbpm/v5.4/userguide/ch.core-basics.html#d0e2358" target="_blank">http://docs.jboss.org/jbpm/v5.4/userguide/ch.core-basics.html#d0e2358</a><span>.</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Any help is appreciated.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Thanks,</p><p>-mark</p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Reply to this message by <a href="https://community.jboss.org/message/778981#778981">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in jBPM at <a href="https://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>