Search This Blog

Monday, December 15, 2014

User Transaction in jBPM

Handling User Transactions (jBPM)

Whenever you do not provide transaction boundaries inside your application, the engine will automatically execute each method invocation on the engine in a separate transaction. If this behavior is acceptable, you don't need to do anything else. You can, however, also specify the transaction boundaries yourself. This allows you, for example, to combine multiple commands into one transaction.
You need to register a transaction manager at the environment before using user-defined transactions. The following sample code uses the Bitronix transaction manager. Next, we use the Java Transaction API (JTA) to specify transaction boundaries, as shown below:


// create the entity manager factory and register it in the environment

EntityManagerFactory emf =

    Persistence.createEntityManagerFactory( "org.jbpm.persistence.jpa" );

Environment env = KnowledgeBaseFactory.newEnvironment();

env.set( EnvironmentName.ENTITY_MANAGER_FACTORY, emf );

env.set( EnvironmentName.TRANSACTION_MANAGER,

         TransactionManagerServices.getTransactionManager() );


// create a new knowledge session that uses JPA to store the runtime state

StatefulKnowledgeSession ksession =

    JPAKnowledgeService.newStatefulKnowledgeSession( kbase, null, env );


// start the transaction

UserTransaction ut =

  (UserTransaction) new InitialContext().lookup( "java:comp/UserTransaction" );

ut.begin();


// perform multiple commands inside one transaction

ksession.insert( new Person( "John Doe" ) );

ksession.startProcess( "MyProcess" );


// commit the transaction

ut.commit();
---------------------------------------------------------------------------------
---------------------------------------------------------------------------------
 
 
If you're deploying to an application server, you can usually create a datasource
by dropping a configuration file in the deploy directory,or if using jBoss
application server configure the data source in Standalone.xml file.
for example: 


   <xa-datasource jndi-name="java:/jbpmOracleSource" 
           pool-name="jbpmOracleSource" enabled="true" use-ccm="false">
       <xa-datasource-property name="URL">
                  jdbc:oracle:thin:@Ip-Address:Port:ORCL
                </xa-datasource-property>
               <driver>oracleDriver</driver>
                    <xa-pool>
                        <is-same-rm-override>false</is-same-rm-override>
                        <no-tx-separate-pools>true</no-tx-separate-pools>
                    </xa-pool>
                    <security>
                        <user-name>jbpmUser</user-name>
                        <password>jbpmUser</password>
                    </security>
                    <recovery>
                        <recover-credential>
                            <user-name>jbpmUser</user-name>
                            <password>jbpmUser</password>
                        </recover-credential>
                    </recovery>
                    <validation>
                        <valid-connection-checker class-name=
"org.jboss.jca.adapters.jdbc.extensions.oracle.OracleValidConnectionChecker"/>
                        <stale-connection-checker class-name=
"org.jboss.jca.adapters.jdbc.extensions.oracle.OracleStaleConnectionChecker"/>
                        <exception-sorter class-name=
"org.jboss.jca.adapters.jdbc.extensions.oracle.OracleExceptionSorter"/>
                    </validation>
                </xa-datasource>

-----------------------------------------------------------------------------
-----------------------------------------------------------------------------

Also the jBPM user should have following privileges on the jBPM data source.
or can be given by executing following SQL on jBPM data source as:

---------------------------------------------------------------------
---------------------------------------------------------------------
 
grant select on sys.dba_pending_transactions to jbpmUser;
grant select on sys.pending_trans$ to jbpmUser;
grant select on sys.dba_2pc_pending to jbpmUser;
grant execute on sys.dbms_system to jbpmUser;

-------------------------------------------------------------------- 
---------------------------------------------------------------------



For more information follow my Tutorial  online @ http://jbpmmaster.blogspot.com/ 

No comments:

Post a Comment