<!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: Drools, Spring integration and JTA in container
</h3>
<span style="margin-bottom: 10px;">
created by <a href="http://community.jboss.org/people/alexmanly">Alex Manly</a> in <i>jBPM</i> - <a href="http://community.jboss.org/message/599448#599448">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><p>Hi Rodri,</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Here is my drools spring configuration file:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-xml">
<span class="jive-xml-tag"><?xml version="1.0" encoding="UTF-8"?></span>
<span class="jive-xml-tag"><span><beans xmlns="</span><a class="jive-link-external-small" href="http://www.springframework.org/schema/beans" target="_blank">http://www.springframework.org/schema/beans</a><span>"
    xmlns:xsi="</span><a class="jive-link-external-small" href="http://www.w3.org/2001/XMLSchema-instance" target="_blank">http://www.w3.org/2001/XMLSchema-instance</a><span>" xmlns:p="</span><a class="jive-link-external-small" href="http://www.springframework.org/schema/p" target="_blank">http://www.springframework.org/schema/p</a><span>"
    xmlns:context="</span><a class="jive-link-external-small" href="http://www.springframework.org/schema/context" target="_blank">http://www.springframework.org/schema/context</a><span>" xmlns:tx="</span><a class="jive-link-external-small" href="http://www.springframework.org/schema/tx" target="_blank">http://www.springframework.org/schema/tx</a><span>"
    xmlns:aop="</span><a class="jive-link-external-small" href="http://www.springframework.org/schema/aop" target="_blank">http://www.springframework.org/schema/aop</a><span>" xmlns:jee="</span><a class="jive-link-external-small" href="http://www.springframework.org/schema/jee" target="_blank">http://www.springframework.org/schema/jee</a><span>"
    xmlns:util="</span><a class="jive-link-external-small" href="http://www.springframework.org/schema/util" target="_blank">http://www.springframework.org/schema/util</a><span>" xmlns:drools="</span><a class="jive-link-external-small" href="http://drools.org/schema/drools-spring" target="_blank">http://drools.org/schema/drools-spring</a><span>"
    xmlns:camel="</span><a class="jive-link-external-small" href="http://camel.apache.org/schema/spring" target="_blank">http://camel.apache.org/schema/spring</a><span>"
    xsi:schemaLocation="
       </span><a class="jive-link-external-small" href="http://www.springframework.org/schema/beans" target="_blank">http://www.springframework.org/schema/beans</a><span>
       </span><a class="jive-link-external-small" href="http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" target="_blank">http://www.springframework.org/schema/beans/spring-beans-3.0.xsd</a><span>
       </span><a class="jive-link-external-small" href="http://www.springframework.org/schema/tx" target="_blank">http://www.springframework.org/schema/tx</a><span>
       </span><a class="jive-link-external-small" href="http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" target="_blank">http://www.springframework.org/schema/tx/spring-tx-3.0.xsd</a><span>
       </span><a class="jive-link-external-small" href="http://www.springframework.org/schema/aop" target="_blank">http://www.springframework.org/schema/aop</a><span>
       </span><a class="jive-link-external-small" href="http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" target="_blank">http://www.springframework.org/schema/aop/spring-aop-3.0.xsd</a><span>
       </span><a class="jive-link-external-small" href="http://www.springframework.org/schema/context" target="_blank">http://www.springframework.org/schema/context</a><span>
       </span><a class="jive-link-external-small" href="http://www.springframework.org/schema/context/spring-context-3.0.xsd" target="_blank">http://www.springframework.org/schema/context/spring-context-3.0.xsd</a><span>
       </span><a class="jive-link-external-small" href="http://www.springframework.org/schema/jee" target="_blank">http://www.springframework.org/schema/jee</a><span>
       </span><a class="jive-link-external-small" href="http://www.springframework.org/schema/jee/spring-jee-3.0.xsd" target="_blank">http://www.springframework.org/schema/jee/spring-jee-3.0.xsd</a><span>
       </span><a class="jive-link-external-small" href="http://www.springframework.org/schema/util" target="_blank">http://www.springframework.org/schema/util</a><span>
       </span><a class="jive-link-external-small" href="http://www.springframework.org/schema/util/spring-util-3.0.xsd" target="_blank">http://www.springframework.org/schema/util/spring-util-3.0.xsd</a><span>
       </span><a class="jive-link-external-small" href="http://drools.org/schema/drools-spring" target="_blank">http://drools.org/schema/drools-spring</a><span>
       </span><a class="jive-link-external-small" href="http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-container/drools-spring/src/main/resources/org/drools/container/spring/drools-spring.xsd" target="_blank">http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-container/drools-spring/src/main/resources/org/drools/container/spring/drools-spring.xsd</a><span>
       </span><a class="jive-link-external-small" href="http://camel.apache.org/schema/spring" target="_blank">http://camel.apache.org/schema/spring</a><span>
       </span><a class="jive-link-external-small" href="http://camel.apache.org/schema/spring/camel-spring.xsd" target="_blank">http://camel.apache.org/schema/spring/camel-spring.xsd</a><span>"></span></span>
    <span class="jive-xml-tag"><context:annotation-config /></span>
    <span class="jive-xml-tag"><context:property-placeholder location="classpath:/app_default.properties"/></span>
    <span class="jive-xml-tag"><jee:jndi-lookup id="dataSource" jndi-name="jdbc/appDS_V1" /></span>
    <span class="jive-xml-tag"><jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/app-domain-pu" /></span>
    <span class="jive-xml-tag"><tx:jta-transaction-manager /></span>
    <span class="jive-xml-tag"><tx:annotation-driven transaction-manager="transactionManager"/></span>
    <span class="jive-xml-tag"><context:component-scan base-package="com.company.app" /></span>
    <span class="jive-xml-tag"><drools:kbase id="kbase1"></span>
        <span class="jive-xml-tag"><drools:resources></span>
            <span class="jive-xml-tag"><drools:resource id="cloneFlow" type="DRF" source="classpath:Clone.rf"/></span>
            <span class="jive-xml-tag"><drools:resource id="approvalFlow" type="DRF" source="classpath:Approval.rf"/></span>
          <span class="jive-xml-tag"></drools:resources></span>
        <span class="jive-xml-tag"><drools:configuration></span>
            <span class="jive-xml-tag"><drools:mbeans enabled="true" /></span>
            <span class="jive-xml-tag"><drools:event-processing-mode mode="STREAM" /></span>
        <span class="jive-xml-tag"></drools:configuration></span>
    <span class="jive-xml-tag"></drools:kbase></span>
    <span class="jive-xml-tag"><drools:ksession id="jpaSingleSessionCommandService" type="stateful" kbase="kbase1" name="stateful-session"></span>
        <span class="jive-xml-tag"><drools:configuration></span>           
            <span class="jive-xml-tag"><drools:jpa-persistence></span>
                <span class="jive-xml-tag"><drools:transaction-manager ref="transactionManager" /></span>
                <span class="jive-xml-tag"><drools:entity-manager-factory ref="entityManagerFactory" /></span>
                <span class="jive-xml-tag"><drools:variable-persisters></span>
                    <span class="jive-xml-tag"><drools:persister for-class="javax.persistence.Entity" implementation="org.drools.persistence.processinstance.persisters.JPAVariablePersister"/></span>
                    <span class="jive-xml-tag"><drools:persister for-class="java.io.Serializable" implementation="org.drools.persistence.processinstance.persisters.SerializableVariablePersister"/></span>
                <span class="jive-xml-tag"></drools:variable-persisters></span>
            <span class="jive-xml-tag"></drools:jpa-persistence></span>
            <span class="jive-xml-tag"><drools:work-item-handlers></span>
                <span class="jive-xml-tag"><drools:work-item-handler name="Human Task" ref="humanTaskHandler"/></span>
            <span class="jive-xml-tag"></drools:work-item-handlers></span>
            <span class="jive-xml-tag"><drools:keep-reference enabled="true" /></span>
            <span class="jive-xml-tag"><drools:clock-type type="REALTIME" /></span>
        <span class="jive-xml-tag"></drools:configuration></span>
        <span class="jive-xml-tag"><drools:script></span>
            <span class="jive-xml-tag"><drools:fire-all-rules/></span>
        <span class="jive-xml-tag"></drools:script></span>
    <span class="jive-xml-tag"></drools:ksession></span>
<span class="jive-xml-tag"></beans></span>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>I am using drools version 5.1.1, here is my maven config:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-xml">
<span class="jive-xml-tag"><dependency></span>
            <span class="jive-xml-tag"><groupId></span>org.drools<span class="jive-xml-tag"></groupId></span>
            <span class="jive-xml-tag"><artifactId></span>drools-core<span class="jive-xml-tag"></artifactId></span>
            <span class="jive-xml-tag"><version></span>5.1.1<span class="jive-xml-tag"></version></span>
            <span class="jive-xml-tag"><scope></span>compile<span class="jive-xml-tag"></scope></span>
        <span class="jive-xml-tag"></dependency></span>
        <span class="jive-xml-tag"><dependency></span>
            <span class="jive-xml-tag"><groupId></span>org.drools<span class="jive-xml-tag"></groupId></span>
            <span class="jive-xml-tag"><artifactId></span>drools-api<span class="jive-xml-tag"></artifactId></span>
            <span class="jive-xml-tag"><version></span>5.1.1<span class="jive-xml-tag"></version></span>
            <span class="jive-xml-tag"><scope></span>compile<span class="jive-xml-tag"></scope></span>
        <span class="jive-xml-tag"></dependency></span>
        <span class="jive-xml-tag"><dependency></span>
            <span class="jive-xml-tag"><groupId></span>org.drools<span class="jive-xml-tag"></groupId></span>
            <span class="jive-xml-tag"><artifactId></span>drools-compiler<span class="jive-xml-tag"></artifactId></span>
            <span class="jive-xml-tag"><version></span>5.1.1<span class="jive-xml-tag"></version></span>
            <span class="jive-xml-tag"><scope></span>compile<span class="jive-xml-tag"></scope></span>
        <span class="jive-xml-tag"></dependency></span>
        <span class="jive-xml-tag"><dependency></span>
            <span class="jive-xml-tag"><groupId></span>org.drools<span class="jive-xml-tag"></groupId></span>
            <span class="jive-xml-tag"><artifactId></span>drools-persistence-jpa<span class="jive-xml-tag"></artifactId></span>
            <span class="jive-xml-tag"><version></span>5.1.1<span class="jive-xml-tag"></version></span>
            <span class="jive-xml-tag"><scope></span>compile<span class="jive-xml-tag"></scope></span>
         <span class="jive-xml-tag"></dependency></span>
        <span class="jive-xml-tag"><dependency></span>
            <span class="jive-xml-tag"><groupId></span>org.drools<span class="jive-xml-tag"></groupId></span>
            <span class="jive-xml-tag"><artifactId></span>drools-spring<span class="jive-xml-tag"></artifactId></span>
            <span class="jive-xml-tag"><version></span>5.1.1<span class="jive-xml-tag"></version></span>
            <span class="jive-xml-tag"><scope></span>compile<span class="jive-xml-tag"></scope></span>
        <span class="jive-xml-tag"></dependency></span>
        <span class="jive-xml-tag"><dependency></span>
            <span class="jive-xml-tag"><groupId></span>com.thoughtworks.xstream<span class="jive-xml-tag"></groupId></span>
            <span class="jive-xml-tag"><artifactId></span>xstream<span class="jive-xml-tag"></artifactId></span>
            <span class="jive-xml-tag"><version></span>1.3.1<span class="jive-xml-tag"></version></span>
            <span class="jive-xml-tag"><scope></span>compile<span class="jive-xml-tag"></scope></span>
        <span class="jive-xml-tag"></dependency></span>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>In my web application I copied the java file: org.drools.persistence.session.SingleSessionCommandService from the drools SVN site and then overriden and modified the method initTransactionManager (as long as this is higher up in the classpath the it will execute):</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code">
public void initTransactionManager(Environment env) {
        Object tm = env.get( EnvironmentName.TRANSACTION_MANAGER );
        //    START CODE OVERRIDE - so drools works with org.springframework.transaction.jta.JtaTransactionManager
        if ( tm != null &amp;&amp; tm.getClass().getName().equals( "org.springframework.transaction.jta.JtaTransactionManager" ) ) {
            logger.debug( "Instantiating Spring JtaTransactionManager" );
            this.txm = new JtaTransactionManager(((org.springframework.transaction.jta.JtaTransactionManager)tm).getUserTransaction(),
                                                  env.get( EnvironmentName.TRANSACTION_SYNCHRONIZATION_REGISTRY ),
                                                  ((org.springframework.transaction.jta.JtaTransactionManager)tm).getTransactionManager() );
            this.jpm = new DefaultJpaManager(this.env);
        } else {
        //    END CODE OVERRIDE - so drools works with org.springframework.transaction.jta.JtaTransactionManager
            if ( tm != null &amp;&amp; tm.getClass().getName().startsWith( "org.springframework" ) ) {
                try {
                    Class<?> cls = Class.forName( "org.drools.container.spring.beans.persistence.DroolsSpringTransactionManager" );
                    Constructor<?> con = cls.getConstructors()[0];
                    this.txm = (TransactionManager) con.newInstance( tm );
                    logger.debug( "Instantiating  DroolsSpringTransactionManager" );
                    if ( tm.getClass().getName().toLowerCase().contains( "jpa" ) ) {
                        // configure spring for JPA and local transactions
                        cls = Class.forName( "org.drools.container.spring.beans.persistence.DroolsSpringJpaManager" );
                        con = cls.getConstructors()[0];
                        this.jpm =  ( JpaManager) con.newInstance( new Object[] { this.env } );
                    } else {
                        // configure spring for JPA and distributed transactions
                    }
                } catch ( Exception e ) {
                    logger.warn( "Could not instatiate DroolsSpringTransactionManager" );
                    throw new RuntimeException( "Could not instatiate org.drools.container.spring.beans.persistence.DroolsSpringTransactionManager", e );
                }
            } else {
                logger.debug( "Instantiating  JtaTransactionManager" );
                this.txm = new JtaTransactionManager( env.get( EnvironmentName.TRANSACTION ),
                                                      env.get( EnvironmentName.TRANSACTION_SYNCHRONIZATION_REGISTRY ),
                                                      tm );
                this.jpm = new DefaultJpaManager(this.env);
            }
        //    START CODE OVERRIDE
        }
        //    END CODE OVERRIDE
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Hope this helps</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/599448#599448">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>