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>
hibernate.propertieshibernate.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
<hibernate-session/> 自動以 session.beginTransaction() 開啟事務,封裝為 HibernateTransactionResource 註冊至 StandardTransaction。environment.getTransaction().setRollbackOnly() 設定回滾。流程定義:三步等待狀態流程(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 邏輯)
DBVERSION_ 樂觀鎖欄位,支援多 JVM/叢集環境。用於在不依賴資料庫主鍵的情況下,以業務唯一標識(如訂單號)查詢/關聯執行實例。