首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

jBPM 4.0 開發指南第 14 章:Hibernate 持久化配置與流程實例化實戰

     編輯:Java 相關知識

14.1 標準環境配置 jbpm-configuration.xml

<jbpm-configuration>
  <process-engine>
    <hibernate-session-factory/> <!-- 延遲建立 SessionFactory -->
    <hibernate-configuration>
      <properties resource="hibernate.properties"/>
      <mapping resource="jbpm.pvm.typedefs.hbm.xml"/>
      <mapping resource="jbpm.pvm.wire.hbm.xml"/>
      <mapping resource="jbpm.pvm.definition.hbm.xml"/>
      <mapping resource="jbpm.pvm.execution.hbm.xml"/>
      <mapping resource="jbpm.pvm.variable.hbm.xml"/>
      <mapping resource="jbpm.pvm.job.hbm.xml"/>
      <mapping resource="jbpm.jpdl.hbm.xml"/>
      <cache-configuration resource="jbpm.pvm.cache.xml" usage="nonstrict-read-write"/>
    </hibernate-configuration>
  </process-engine>

  <transaction-context>
    <standard-transaction/>   <!-- 簡單全域事務 -->
    <hibernate-session/>      <!-- 自動註冊同步 -->
    <pvm-db-session/>         <!-- 提供特定查詢方法 -->
  </transaction-context>
</jbpm-configuration>

14.2 標準 Hibernate 配置 hibernate.properties

hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.connection.driver_class=org.hsqldb.jdbcDriver
hibernate.connection.url=jdbc:hsqldb:mem:.
hibernate.connection.username=sa
hibernate.connection.password=
hibernate.hbm2ddl.auto=create-drop
hibernate.cache.use_second_level_cache=true
hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider

14.3 事務管理

  • <hibernate-session/> 自動以 session.beginTransaction() 開啟事務,封裝為 HibernateTransactionResource 註冊至 StandardTransaction。
  • 環境塊內透過 environment.getTransaction().setRollbackOnly() 設定回滾。
  • 環境關閉時自動提交或回滾。

14.4 流程實例化完整範例

流程定義:三步等待狀態流程(one → two → three)。

// 1. 建立 EnvironmentFactory
EnvironmentFactory ef = EnvironmentFactory.parse(new ResourceStreamSource("environment.cfg.xml"));

// 2. 事務 1:部署流程定義
Environment env = ef.openEnvironment();
try {
    PvmDbSession db = env.get(PvmDbSession.class);
    ProcessDefinition pd = ProcessFactory.build("persisted process")
        .activity("one").initial().behaviour(new State())
        .transition().to("two")
        .activity("two").behaviour(new State())
        .transition().to("three")
        .activity("three").behaviour(new State())
        .done();
    db.save(pd);
} finally { env.close(); }

// 3. 事務 2:啟動執行實例(帶 businessKey "first")
env = ef.openEnvironment();
try {
    PvmDbSession db = env.get(PvmDbSession.class);
    ProcessDefinition pd = db.findProcessDefinition("persisted process");
    Execution exec = pd.startExecution("first");
    assertEquals("one", exec.getActivity().getName());
    db.save(exec);
} finally { env.close(); }

// 4. 事務 3:發送訊號推進到 two
env = ef.openEnvironment();
try {
    PvmDbSession db = env.get(PvmDbSession.class);
    Execution exec = db.findExecution("persisted process", "first");
    exec.signal();
    assertEquals("two", exec.getActivity().getName());
    db.save(exec);
} finally { env.close(); }

// 5. 事務 4:發送訊號推進到 three(同事務 3 邏輯)

關鍵觀察

  • 每個事務對應一個狀態轉移(等待狀態之間)。
  • 資料庫更新 SQL 包含 DBVERSION_ 樂觀鎖欄位,支援多 JVM/叢集環境。
  • 自動活動若耗時過長,可在流程定義中劃分事務邊界(安全點)。

14.5 業務鍵

用於在不依賴資料庫主鍵的情況下,以業務唯一標識(如訂單號)查詢/關聯執行實例。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved