Spring4+hibernate4+jbpm5集成事务问题

发布于 2021-11-21 17:32:17 字数 16300 浏览 862 评论 0

persistence.xml 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                                 http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <!-- jbpm初始化加载文件  RESOURCE_LOCAL		JTA-->
  <persistence-unit name="org.jbpm.persistence.jta" transaction-type="RESOURCE_LOCAL">
 	<provider>org.hibernate.ejb.HibernatePersistence</provider>
	<mapping-file>META-INF/ProcessInstanceInfo.hbm.xml</mapping-file>
	<mapping-file>META-INF/JBPMorm.xml</mapping-file>
	<!-- mapping-file>META-INF/JBPMorm-JPA2.xml</mapping-file -->
	<mapping-file>META-INF/Taskorm.xml</mapping-file>
	<class>org.drools.persistence.info.SessionInfo</class>
	<class>org.drools.persistence.info.WorkItemInfo</class>
	<class>org.drools.persistence.info.SessionInfo</class>
	<class>org.drools.persistence.info.WorkItemInfo</class>
	<class>org.jbpm.process.audit.ProcessInstanceLog</class>
	<class>org.jbpm.process.audit.NodeInstanceLog</class>
	<class>org.jbpm.process.audit.VariableInstanceLog</class>
	<class>org.jbpm.task.Attachment</class>
	<class>org.jbpm.task.Content</class>
	<class>org.jbpm.task.BooleanExpression</class>
	<class>org.jbpm.task.Comment</class>
	<class>org.jbpm.task.Deadline</class>
	<class>org.jbpm.task.Comment</class>
	<class>org.jbpm.task.Deadline</class>
	<class>org.jbpm.task.Delegation</class>
	<class>org.jbpm.task.Escalation</class>
	<class>org.jbpm.task.Group</class>
	<class>org.jbpm.task.I18NText</class>
	<class>org.jbpm.task.Notification</class>
	<class>org.jbpm.task.EmailNotification</class>
	<class>org.jbpm.task.EmailNotificationHeader</class>
	<class>org.jbpm.task.PeopleAssignments</class>
	<class>org.jbpm.task.Reassignment</class>
	<class>org.jbpm.task.Status</class>
	<class>org.jbpm.task.Task</class>
	<class>org.jbpm.task.TaskData</class>
	<class>org.jbpm.task.SubTasksStrategy</class>
	<class>org.jbpm.task.OnParentAbortAllSubTasksEndStrategy</class>
	<class>org.jbpm.task.OnAllSubTasksEndParentEndStrategy</class>
	<class>org.jbpm.task.User</class>
     <properties>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.max_fetch_depth" value="7" />
	  <property name="hibernate.jdbc.fetch_size" value="10" />
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
      <!--<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.service.jta.platform.spi.JtaPlatform" />
      
      <property name="hibernate.connection.username" value="root" />
      <property name="hibernate.connection.password" value="123456" />
      <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
      <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jbpm5?characterEncoding=utf-8" />
      <property name="hibernate.transaction.factory_class" value="org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory"/>
      <property name="hibernate.transaction.manager_lookup_class" value="com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup" />
      -->
    </properties> 
  </persistence-unit>
</persistence>



spring-jbpm.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:aop="http://www.springframework.org/schema/aop"
	   xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jbpm="http://drools.org/schema/drools-spring"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       					   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           				   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                           http://drools.org/schema/drools-spring http://drools.org/schema/drools-spring-1.3.0.xsd">



<!-- jpa配置需要,jbpm5实体管理工厂 -->
  <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  		<property name="persistenceUnitName" value="org.jbpm.persistence.jta"/>
  		<property name="dataSource" ref="datasourcejBPM"></property>
  		<property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
                <property name="generateDdl" value="true" />
                <property name="database" value="MYSQL" />
            </bean>
        </property>
  </bean>

<!-- 配置事务管理器2 -->
	<!-- 声明事务 -->
  <bean id="jbpmTxManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  	  <property name="entityManagerFactory" ref="entityManagerFactory"/>
  </bean>
  
  <!--atomikos 数据源 -->
  <!-- <bean id="datasourcejBPMJTA" class="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean"
        init-method="init" destroy-method="close">
        <description>jbpm5</description>
        <property name="uniqueResourceName" value="dsBac" />
        <property name="driverClassName">
		 	<value>${jbpm5.jdbc.driver}</value>
		 </property>
		 <property name="url">
		 	<value>${jbpm5.jdbc.url}</value>
		 </property>
		 <property name="user">
		 	<value>${jbpm5.jdbc.username}</value>
		 </property>
		 <property name="password">
		 	<value>${jbpm5.jdbc.password}</value>
		 </property>
        <property name="minPoolSize" value="3" />
        <property name="maxPoolSize" value="15" />
        <property name="testQuery" value="SELECT 1" />
	</bean> -->
  
  <!-- JBPM  c3p0数据源 -->
  <bean id="datasourcejBPM" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		 <property name="driverClass">
		 	<value>${jbpm5.jdbc.driver}</value>
		 </property>
		 <property name="jdbcUrl">
		 	<value>${jbpm5.jdbc.url}</value>
		 </property>
		 <property name="user">
		 	<value>${jbpm5.jdbc.username}</value>
		 </property>
		 <property name="password">
		 	<value>${jbpm5.jdbc.password}</value>
		 </property>
		 <!--当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出
			SQLException,如设为0则无限期等待。单位毫秒。Default: 0 -->
		 <property name="checkoutTimeout">
		 	<value>${jdbc.checkoutTimeout}</value>
		 </property>
		 <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
	     <property name="idleConnectionTestPeriod">
	     	<value>${jdbc.idleConnectionTestPeriod}</value>
	     </property>
	     <!-- 初始化池连接大小 -->
	     <property name="initialPoolSize">
	     	<value>${jdbc.initialPoolSize}</value>
	     </property>
	     <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
	     <property name="maxIdleTime">
	     	<value>${jdbc.maxIdleTime}</value>
	     </property>
	     <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
		 <property name="acquireIncrement">
		 	<value>${jdbc.acquireIncrement}</value>
		 </property>
	     <!--连接池中保留的最大连接数。Default: 15 -->
	     <property name="maxPoolSize">
	     	<value>${jdbc.maxPoolSize}</value>
	     </property>
	     <!--连接池中保留的最小连接数。 -->
	     <property name="minPoolSize">
	     	<value>${jdbc.minPoolSize}</value>
	     </property>
	     <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements
			属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
			如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0-->
	     <property name="maxStatements">
	     	<value>${jdbc.maxStatements}</value>
	     </property>
	</bean>
  

  <jbpm:kbase id="kbase1">
	  <jbpm:resources>
	  	<jbpm:resource id="resource1" type="BPMN2" source="classpath:bpmn/special01.bpmn"/>
	  </jbpm:resources>
  </jbpm:kbase>
  
  	<jbpm:ksession id="ksession1" type="stateful" kbase="kbase1">
		<jbpm:configuration>
			<jbpm:jpa-persistence>
				<jbpm:transaction-manager ref="jbpmTxManager"/>
				<jbpm:entity-manager-factory ref="entityManagerFactory"/>
			</jbpm:jpa-persistence>
		</jbpm:configuration>
  	</jbpm:ksession>
  
  <!-- 实例化JbpmAPIUtil类 -->
  <bean id="jbpmApiUtil1" class="com.dulian.jbmp.JbpmAPIUtil">
     <property name="kbase" ref="kbase1"></property>
     <property name="ksession" ref="ksession1"></property>
  </bean>
  
  <!-- <tx:annotation-driven transaction-manager="transactionManager2"/> -->

</beans>




jbpm的系统数据库和我自己的系统数据库是分开的,两个数据库

/**
	 * 启动流程实例
	 * 
	 * @return
	 */
	public ProcessInstance startWorkflowProcess() {
		ProcessInstance pi = null;
		try {
			HornetQHTWorkItemHandler hornetQHTWorkItemHandler = new HornetQHTWorkItemHandler(
					ksession);
			ksession.getWorkItemManager().registerWorkItemHandler("Human Task",
					hornetQHTWorkItemHandler);
			pi = ksession.startProcess(processName, params);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pi;
	}



程序在调用这个函数启动实例流程的时候报事务错误:


java.lang.RuntimeException: Could not commit transaction
	at org.jbpm.task.service.TaskServiceSession.doOperationInTransaction(TaskServiceSession.java:1130)
	at org.jbpm.task.service.TaskServiceSession.addTask(TaskServiceSession.java:189)
	at org.jbpm.task.service.TaskServerHandler.messageReceived(TaskServerHandler.java:151)
	at org.jbpm.task.service.hornetq.HornetQTaskServerHandler.messageReceived(HornetQTaskServerHandler.java:43)
	at org.jbpm.task.service.hornetq.BaseHornetQTaskServer.run(BaseHornetQTaskServer.java:104)
	at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.RuntimeException: Unable to rollback transaction
	at org.jbpm.task.service.persistence.TaskLocalTransactionManager.rollback(TaskLocalTransactionManager.java:77)
	at org.jbpm.task.service.persistence.TaskPersistenceManager.endTransaction(TaskPersistenceManager.java:110)
	at org.jbpm.task.service.TaskServiceSession.doOperationInTransaction(TaskServiceSession.java:1118)
	... 5 more
Caused by: java.lang.IllegalStateException: Transaction not active
	at org.hibernate.ejb.TransactionImpl.rollback(TransactionImpl.java:101)
	at org.jbpm.task.service.persistence.TaskLocalTransactionManager.rollback(TaskLocalTransactionManager.java:70)
	... 7 more
Hibernate: update SessionInfo set lastModificationDate=?, rulesByteArray=?, startDate=?, OPTLOCK=? where id=? and OPTLOCK=?
java.lang.RuntimeException: Could not commit session or rollback
	at org.drools.persistence.SingleSessionCommandService.rollbackTransaction(SingleSessionCommandService.java:406)
	at org.drools.persistence.SingleSessionCommandService.execute(SingleSessionCommandService.java:381)
	at org.drools.command.impl.CommandBasedStatefulKnowledgeSession.startProcess(CommandBasedStatefulKnowledgeSession.java:223)
	at com.dulian.jbmp.JbpmAPIUtil.startWorkflowProcess(JbpmAPIUtil.java:235)
	at com.dulian.service.impl.ManpowerServiceImpl.getProcessInstance(ManpowerServiceImpl.java:66)
	at com.dulian.service.impl.ManpowerServiceImpl.addDocForProcess(ManpowerServiceImpl.java:54)
	at com.jbpm.TestLeaveAddDoc.testForWork(TestLeaveAddDoc.java:94)
	at com.jbpm.TestLeaveAddDoc.startHornetQServer(TestLeaveAddDoc.java:71)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.RuntimeException: Unable to rollback transaction
	at org.drools.container.spring.beans.persistence.DroolsSpringTransactionManager.rollback(DroolsSpringTransactionManager.java:85)
	at org.drools.persistence.SingleSessionCommandService.rollbackTransaction(SingleSessionCommandService.java:402)
	... 35 more
Caused by: org.springframework.transaction.IllegalTransactionStateException: Transaction is already completed - do not call commit or rollback more than once per transaction
	at org.springframework.transaction.support.AbstractPlatformTransactionManager.rollback(AbstractPlatformTransactionManager.java:821)
	at org.drools.container.spring.beans.persistence.DroolsSpringTransactionManager.rollback(DroolsSpringTransactionManager.java:79)
	... 36 more



求解答小弟在线等!!!



如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文