1

没有任何关于 Activiti 的内容被保存到数据库中。应用程序实体正在保存到数据库中。下面依次是 spring 文件、persitence.xml 文件和测试用例。

使用 SQL Server 分析器,我看到一个单独的数据库事务正在为由 Activiti 引起的数据库交互启动,并且我进一步看到该单独的事务被回滚而不是被提交。其他应用程序数据库交互正在另一个事务上发生,并且正在提交此特定事务。

  1. 我想,根据我的配置,Activiti 数据库交互将与应用程序的其余部分发生在同一事务上。我已经多次检查了我的配置文件和代码,并没有发现任何错误。任何想法为什么要为 Activiti db 交互启动单独的事务?
  2. 当然,前一项是关键问题。但是,知道为什么要回滚单独的事务也会很有趣?

弹簧文件:

<context:annotation-config/>
<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="ActivitiTrialDataSource" class="org.apache.commons.dbcp.BasicDataSource" >
    <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
    <property name="url" value="jdbc:jtds:sqlserver://localhost:1433/ActivitiTrial" />
    <property name="username" value="ActivitiTrial" />
    <property name="password" value="ActivitiTrial" />
    <property name="defaultAutoCommit" value="false" />
    <property name="initialSize" value="5" />
</bean>


<aop:aspectj-autoproxy proxy-target-class="true" expose-proxy="true"/>


<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="ActivitiTrialDataSource" />
    <property name="persistenceUnitName" value="ActivitiTrial"/>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>



<!-- Activiti -->
<bean id="activitiDataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
    <property name="targetDataSource" ref="ActivitiTrialDataSource" />
</bean>

<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
    <property name="databaseType" value="mssql" />
    <property name="dataSource" ref="activitiDataSource" />
    <property name="transactionsExternallyManaged" value="true" />
    <property name="transactionManager" ref="transactionManager" />
    <property name="databaseSchemaUpdate" value="false" />
    <property name="history" value="audit" />
    <property name="jobExecutorActivate" value="false" />
</bean>

<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
    <property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean>

<bean id="activitiRepositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
<bean id="activitiRuntimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
<bean id="activitiTaskService" factory-bean="processEngine" factory-method="getTaskService" />
<bean id="activitiHistoryService" factory-bean="processEngine" factory-method="getHistoryService" />
<bean id="activitiManagementService" factory-bean="processEngine" factory-method="getManagementService" />

persitence.xml 文件:

<persistence-unit name="ActivitiTrial">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <properties>
        <property name="hibernate.archive.autodetection" value="hbm,class"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
        <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
        <property name="hibernate.hbm2ddl.auto" value="none"/>
        <property name="hibernate.show_sql" value="false"/>
        <property name="hibernate.ejb.metamodel.generation" value="disabled"/>
    </properties>
</persistence-unit>

测试用例:

@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(defaultRollback=false)
@ContextConfiguration({"classpath:/springApplicationContext.xml"})
public class TrialTest {
    @Autowired
    RepositoryService activitiRepositoryService;

    @Autowired
    RuntimeService activitiRuntimeService;

    @Autowired
    TaskService activitiTaskService;


    @PersistenceContext(unitName="ActivitiTrial")
    EntityManager entityManager;



    @Test
    @Transactional
    public void trialTest() throws Exception {
        long entryMilliseconds = new Date().getTime();


        activitiRepositoryService.createDeployment().addClasspathResource("process-definitions/neville.bpmn20.xml").deploy();


        ApplicationEntity applicationEntity1 = new ApplicationEntity();
        applicationEntity1.name = "App entity 1";
        applicationEntity1.createDate = new Date();
        Session hibernateSessionBeforeActiviti = ((Session) entityManager.getDelegate());
        entityManager.persist(applicationEntity1);
        entityManager.flush();


        Map<String, Object> processVariables = new HashMap<String, Object>();
        processVariables.put("ApplicationEntityID", applicationEntity1.id);
        ProcessInstance processInstance = activitiRuntimeService.startProcessInstanceByKey("neville", processVariables);
        String processInstanceId = processInstance.getId();
        Task userTask = activitiTaskService.createTaskQuery().processInstanceId(processInstanceId).list().get(0);


        ApplicationEntity applicationEntity2 = new ApplicationEntity();
        applicationEntity2.name = "App entity 2";
        applicationEntity2.createDate = new Date();
        Session hibernateSessionAfterActiviti = ((Session) entityManager.getDelegate());
        entityManager.persist(applicationEntity2);
        entityManager.flush();


        System.out.println("Leaving trialTest() in : " + (new Date().getTime() - entryMilliseconds) + " milliseconds.");
    }
}
4

2 回答 2

1

我通过解决 MyBatis (JDBC) 和 Hibernate (JPA) 之间的冲突解决了这个问题:

您应该向bean添加jpaVendorAdapter属性:entityManagerFactory

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="ActivitiTrialDataSource" />
    <property name="persistenceUnitName" value="ActivitiTrial"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
</bean>

有关更多详细信息,请参阅此问题的答案。

于 2017-06-25T06:52:42.387 回答
0

我已经浪费了很多时间试图将通用事务管理器传递给 Spring 和 Activiti——但没有结果。无法完整回答您的问题,但我希望它可以为您节省一些时间

活动论坛:

演示:

这些 repos 说明 Activiti 不接受 jpa/hibernate 事务:

您还可以查看演示https://github.com/Activiti/Activiti(Activiti + Spring + Bitronix)它可能会有所帮助(我还没有时间检查它)。

附言

作为普通交易的替代品,我看到了补偿事件。

于 2013-09-26T07:14:04.870 回答