<!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;">
    How to configure jbpm5 to store and retrieve Knowledge Sessions from the database
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="http://community.jboss.org/people/MicheleMauro">Michele Mauro</a> in <i>jBPM</i> - <a href="http://community.jboss.org/message/587992#587992">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 trying to setup a web application to work with jbpm5 processes, but I'm stuck at the jbpm5 configuration. I use spring, and I can write up (messing a bit with the various examples) a test case that can create and launch a process, with a configuration along these lines in the applicationContext.xml:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&lt;drools:ksession id="jpaSingleSessionCommandService" type="stateful" kbase="knowledgeBase"&gt;</p><p>&#160;&#160;&#160;&#160; &lt;drools:configuration&gt;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;drools:work-item-handlers&gt;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;drools:work-item-handler name="Human Task" ref="humanTaskHandler" /&gt;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/drools:work-item-handlers&gt;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;drools:jpa-persistence load="1"&gt;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;drools:transaction-manager ref="transactionManager" /&gt;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;drools:entity-manager-factory ref="entityManagerFactory" /&gt;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/drools:jpa-persistence&gt;</p><p>&#160;&#160;&#160; &lt;/drools:configuration&gt;</p><p>&lt;/drools:ksession&gt;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>However, this setup creates a new ksession each time the application is loaded. In my case, I need to load a specific ksession by id, work with the process instance contained in it, inspect it for current status and results, and then persist it back.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I've tried setting up something more basic in Spring, that is building only the KnowledgeBase and then getting the session with:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>StatefulKnowledgeSession ks = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>but I keep getting strange NPEs and errors. In particular, I found that, while the documentation states (jBPM 5.0.0 manual, chapter 8) that you should build the environment to pass the JPAKnowledgeService in this way:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a class="jive-link-anchor-small"><span class="java_type" style="color: #000000;">Environment</span><span class="java_plain" style="color: #000000;"> env </span><span class="java_operator" style="color: #000000;">=</span><span class="java_plain" style="color: #000000;"> </span><span class="java_type" style="color: #000000;">KnowledgeBaseFactory</span><span class="java_separator" style="color: #000000;">.</span><span class="java_plain" style="color: #000000;">newEnvironment</span><span class="java_separator" style="color: #000000;">();</span><br/><span class="java_plain" style="color: #000000;">env</span><span class="java_separator" style="color: #000000;">.</span><span class="java_plain" style="color: #000000;">set</span><span class="java_separator" style="color: #000000;">(</span><span class="java_plain" style="color: #000000;"> </span><span class="java_type" style="color: #000000;">EnvironmentName</span><span class="java_separator" style="color: #000000;">.</span><span class="java_plain" style="color: #000000;">ENTITY_MANAGER_FACTORY</span><span class="java_separator" style="color: #000000;">,</span><span class="java_plain" style="color: #000000;"> emf </span><span class="java_separator" style="color: #000000;">);</span><br/><span class="java_plain" style="color: #000000;">env</span><span class="java_separator" style="color: #000000;">.</span><span class="java_plain" style="color: #000000;">set</span><span class="java_separator" style="color: #000000;">(</span><span class="java_plain" style="color: #000000;"> </span><span class="java_type" style="color: #000000;">EnvironmentName</span><span class="java_separator" style="color: #000000;">.</span><span class="java_plain" style="color: #000000;">TRANSACTION_MANAGER</span><span class="java_separator" style="color: #000000;">,</span><br/><span class="java_plain" style="color: #000000;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span class="java_type" style="color: #000000;">TransactionManagerServices</span><span class="java_separator" style="color: #000000;">.</span><span class="java_plain" style="color: #000000;">getTransactionManager</span><span class="java_separator" style="color: #000000;">()</span><span class="java_plain" style="color: #000000;"> </span><span class="java_separator" style="color: #000000;">);</span></a></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>the SingleSessionCommandService, that is the final implementation of the KnowledgeSession that gets called, wants a different environment:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a class="jive-link-external-small" href="https://github.com/droolsjbpm/droolsjbpm/blob/master/drools-persistence-jpa/src/main/java/org/drools/persistence/SingleSessionCommandService.java">https://github.com/droolsjbpm/droolsjbpm/blob/master/drools-persistence-jpa/src/main/java/org/drools/persistence/SingleSessionCommandService.java</a></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>line 56:</p><p class="line" id="LC56" style="padding-left: 1em; line-height: 1.4em;"><span class="k" style="line-height: 1.4em; font-weight: bold;">if</span> <span class="o" style="line-height: 1.4em; font-weight: bold;">(</span> <span class="n" style="line-height: 1.4em;">env</span><span class="o" style="line-height: 1.4em; font-weight: bold;">.</span><span class="na" style="line-height: 1.4em; color: #008080;">get</span><span class="o" style="line-height: 1.4em; font-weight: bold;">(</span> <span class="n" style="line-height: 1.4em;">EnvironmentName</span><span class="o" style="line-height: 1.4em; font-weight: bold;">.</span><span class="na" style="line-height: 1.4em; color: #008080;">ENTITY_MANAGER_FACTORY</span> <span class="o" style="line-height: 1.4em; font-weight: bold;">)</span> <span class="o" style="line-height: 1.4em; font-weight: bold;">==</span> <span class="kc" style="line-height: 1.4em; font-weight: bold;">null</span> <span class="o" style="line-height: 1.4em; font-weight: bold;">&amp;&amp;</span></p><p class="line" id="LC57" style="padding-left: 1em; line-height: 1.4em;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="n" style="line-height: 1.4em;">env</span><span class="o" style="line-height: 1.4em; font-weight: bold;">.</span><span class="na" style="line-height: 1.4em; color: #008080;">get</span><span class="o" style="line-height: 1.4em; font-weight: bold;">(</span> <span class="n" style="line-height: 1.4em;">EnvironmentName</span><span class="o" style="line-height: 1.4em; font-weight: bold;">.</span><span class="na" style="line-height: 1.4em; color: #008080;">PERSISTENCE_CONTEXT_MANAGER</span> <span class="o" style="line-height: 1.4em; font-weight: bold;">)</span> <span class="o" style="line-height: 1.4em; font-weight: bold;">==</span> <span class="kc" style="line-height: 1.4em; font-weight: bold;">null</span>&#160; <span class="o" style="line-height: 1.4em; font-weight: bold;">)</span> <span class="o" style="line-height: 1.4em; font-weight: bold;">{</span></p><p class="line" id="LC58" style="padding-left: 1em; line-height: 1.4em;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="k" style="line-height: 1.4em; font-weight: bold;">throw</span> <span class="k" style="line-height: 1.4em; font-weight: bold;">new</span> <span class="nf" style="line-height: 1.4em; color: #990000; font-weight: bold;">IllegalArgumentException</span><span class="o" style="line-height: 1.4em; font-weight: bold;">(</span> <span class="s" style="line-height: 1.4em; color: #dd1144;">"Environment must have an EntityManagerFactory "</span> <span class="o" style="line-height: 1.4em; font-weight: bold;">+</span></p><p class="line" id="LC59" style="padding-left: 1em; line-height: 1.4em;">&#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; <span class="s" style="line-height: 1.4em; color: #dd1144;">"or a PersistenceContextManager instance"</span> <span class="o" style="line-height: 1.4em; font-weight: bold;">);</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Now I'm completely confused. How does this work?</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Michele</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/587992#587992">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>