此内容没有您所选择的语言版本。

15.4. Persistence configuration


15.4.1. Persistence configuration

Although persistence is not used by default, the dependencies needed are available in the runtime directory as jar files.

Persistence is defined per session and you can define it either using the JBPMHelper class after you create a session or using the JPAKnowledgeService to create your session. The latter option provides more flexibility, while JBPMHelper has a method to create a session, and uses a configuration file to configure this session.

15.4.2. Configuring persistence using JBPMHelper

To configure persistence of your session using JBPMHelper, do the following:

  1. Define your application to use an appropriate JBPMHelper session constructor:

    • KieSession ksession = JBPMHelper.newKieSession(kbase);
    • KieSession ksession = JBPMHelper.loadKieSession(kbase, sessionId);
  2. Configure the persistence in the jBPM.properties file.

    Example 15.1. A sample jBPM.properties file with persistence for the in-memory H2 database

    # for creating a datasource
    persistence.datasource.name=jdbc/jbpm-ds
    persistence.datasource.user=sa
    persistence.datasource.password=
    persistence.datasource.url=jdbc:h2:tcp://localhost/~/jbpm-db
    persistence.datasource.driverClassName=org.h2.Driver
    
    # for configuring persistence of the session
    persistence.enabled=true
    persistence.persistenceunit.name=org.jbpm.persistence.jpa
    persistence.persistenceunit.dialect=org.hibernate.dialect.H2Dialect
    
    # for configuring the human task service
    taskservice.enabled=true
    taskservice.datasource.name=org.jbpm.task
    taskservice.transport=mina
    taskservice.usergroupcallback=org.jbpm.task.service.DefaultUserGroupCallbackImpl
    Copy to Clipboard Toggle word wrap

Any invocations on the session will now trigger the persistence process.

Make sure the datasource is up and running on engine start. If you are running the in-memory H2 database, you can start the database from your application using the JBPMHelper.startH2Server(); method call and register it with the engine using JBPMHelper.setupDataSource(); method call.

15.4.3. Configuring persistence using JPAKnowledgeService

To create your knowledge session and configure its persistence using JPAKnowledgeService, do the following:

  1. Define your application to use the knowledge session created by JPAKnowledgeService:

    1. Define the session based on a knowledge base, a knowledge session configuration, and an environment. The environment must contain a reference to your Entity Manager Factory:

      // 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 );
      
      // create a new knowledge session that uses JPA to store the runtime state
      KieSession ksession = JPAKnowledgeService.newKieSession( kbase, null, env );
      int sessionId = ksession.getId();
      
      // invoke methods on your method here
      ksession.startProcess( "MyProcess" );
      ksession.dispose();
      Copy to Clipboard Toggle word wrap
    2. Define the session based on a specific session id.

      // recreate the session from database using the sessionId
      ksession = JPAKnowledgeService.loadKieSession(sessionId, kbase, null, env );
      Copy to Clipboard Toggle word wrap
  2. Configure the persistence in the META-INF/persistence.xml file: configure JPA to use Hibernate and the respective database.

    Information on how to configure data source on your application server should be available in the documentation delivered with the application server. For this information for JBoss Enterprise Application Platform, see the Administration and Configuration Guide for this product.

    Example 15.2. A sample persistence.xml file with persistence for an H2 data source jdbc/jbpm-ds

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <persistence
      version="1.0"
      xsi:schemaLocation=
        "http://java.sun.com/xml/ns/persistence
         http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd
         http://java.sun.com/xml/ns/persistence/orm
         http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
      xmlns:orm="http://java.sun.com/xml/ns/persistence/orm"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://java.sun.com/xml/ns/persistence">
      <persistence-unit name="org.jbpm.persistence.jpa" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>jdbc/jbpm-ds</jta-data-source>
        <mapping-file>META-INF/JBPMorm.xml</mapping-file>
        <class>org.drools.persistence.info.SessionInfo</class>
        <class>org.jbpm.persistence.processinstance.ProcessInstanceInfo</class>
        <class>org.drools.persistence.info.WorkItemInfo</class>
        <properties>
          <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
          <property name="hibernate.max_fetch_depth" value="3"/>
          <property name="hibernate.hbm2ddl.auto" value="update"/>
          <property name="hibernate.show_sql" value="true"/>
          <property name="hibernate.transaction.manager_lookup_class"
                    value="org.hibernate.transaction.BTMTransactionManagerLookup"/>
        </properties>
      </persistence-unit>
    </persistence>
    Copy to Clipboard Toggle word wrap

Any invocations on the session will now trigger the persistence process.

Make sure the datasource is up and running on engine start. If you are running the in-memory H2 database, you can start the database from your application using the JBPMHelper.startH2Server(); method call and register it with the engine using JBPMHelper.setupDataSource(); method call.

Sample data source configuration for simple Java environment

If you are running JBoss BPM Suite in a simple Java environment, your data source configuration will be similar to the following:

PoolingDataSource ds = new PoolingDataSource();
ds.setUniqueName("jdbc/jbpm-ds");
ds.setClassName("bitronix.tm.resource.jdbc.lrc.LrcXADataSource");
ds.setMaxPoolSize(3);
ds.setAllowLocalTransactions(true);
ds.getDriverProperties().put("user", "sa");
ds.getDriverProperties().put("password", "sasa");
ds.getDriverProperties().put("URL", "jdbc:h2:tcp://localhost/~/jbpm-db");
ds.getDriverProperties().put("driverClassName", "org.h2.Driver");
ds.init();
Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat