Flowable 6.6.0 BPMN用户指南 - (4)Spring集成 - 4.2 事务

《Flowable 6.6.0 BPMN用户指南》

1. 入门

2. 配置

3 The Flowable API

4 Flowable 6.6.0 BPMN用户指南 - (4)Spring集成

4.1 ProcessEngineFactoryBean
4.2 事务
4.3 表达式(Expressions)
4.4 自动资源部署(Automatic resource deployment)
4.5 单元测试
4.6 使用Hibernate 4.2.x的JPA

Flowable 6.6.0 用户指南相关文档下载

《精编Flowable 6.6.0 应用程序指南中文PDF版》
《精编Flowable 6.6.0 表单用户指南中文PDF版》

有关Flowable文档的其他资料,参见:

《Flowable文档大全》
Flowable 6.6.0 BPMN用户指南 - (4)Spring集成 - 4.2 事务_第1张图片

4.2 事务

We’ll explain the SpringTransactionIntegrationTest found in the Spring examples of the distribution step by step. Below is the Spring configuration file that we use in this example (you can find it in SpringTransactionIntegrationTest-context.xml). The section shown below contains the dataSource, transactionManager, processEngine and the Flowable engine services.

我们将逐步解释发行版包含的Spring示例中的SpringTransactionIntegrationTest。下面是我们在本例中使用的Spring配置文件(您可以在SpringTransactionIntegrationTest-context.xml文件中找到). 下面章节包含了dataSource、transactionManager、processEngine和Flowable 引擎服务。

When passing the DataSource to the SpringProcessEngineConfiguration (using property “dataSource”), Flowable uses a org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy internally, which wraps the passed DataSource. This is done to make sure the SQL connections retrieved from the DataSource and the Spring transactions play well together. This implies that it’s no longer necessary to proxy the dataSource yourself in Spring configuration, although it’s still possible to pass a TransactionAwareDataSourceProxy into the SpringProcessEngineConfiguration. In this case, no additional wrapping will occur.

Make sure when declaring a TransactionAwareDataSourceProxy in Spring configuration yourself that you don’t use it for resources that are already aware of Spring transactions (e.g. DataSourceTransactionManager and JPATransactionManager need the un-proxied dataSource).

当将DataSource传递给SpringProcessEngineConfiguration(使用属性“DataSource”)时,Flowable内部使用org.springframework.jdbc.datasource.TransactionWareDatasourceProxy,它封装了传递的DataSource。这样做是为了确保从DataSource检索到的SQL连接和Spring事务能够很好地协同工作。这意味着不再需要在Spring配置中自己委托数据源(dataSource),尽管仍然可以将TransactionAwareDataSourceProxy传递到SpringProcessEngineConfiguration中。在这种情况下,不会发生额外的封装。

在Spring配置中自己声明TransactionAwareDataSourceProxy时,请确保不要将其用于已经知道的Spring事务资源(例如DataSourceTransactionManager和JPATransactionManager需要未委托的数据源(un-proxied dataSource))。

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                             http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                             http://www.springframework.org/schema/context/spring-context-2.5.xsd
                           http://www.springframework.org/schema/tx
                             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

  <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
    <property name="driverClass" value="org.h2.Driver" />
    <property name="url" value="jdbc:h2:mem:flowable;DB_CLOSE_DELAY=1000" />
    <property name="username" value="sa" />
    <property name="password" value="" />
  </bean>

  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
  </bean>

  <bean id="processEngineConfiguration" class="org.flowable.spring.SpringProcessEngineConfiguration">
    <property name="dataSource" ref="dataSource" />
    <property name="transactionManager" ref="transactionManager" />
    <property name="databaseSchemaUpdate" value="true" />
    <property name="asyncExecutorActivate" value="false" />
  </bean>

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

  <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
  <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
  <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
  <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
  <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />

...

The remainder of that Spring configuration file contains the beans and configuration that we’ll use in this particular example:

Spring配置文件的其余部分包含我们将在这个特定示例中使用的bean和配置:

<beans>
  ...
  <tx:annotation-driven transaction-manager="transactionManager"/>

  <bean id="userBean" class="org.flowable.spring.test.UserBean">
    <property name="runtimeService" ref="runtimeService" />
  </bean>

  <bean id="printer" class="org.flowable.spring.test.Printer" />

</beans>

First, the application context is created using any of the ways supported by Spring. In this example, you could use a classpath XML resource to configure our Spring application context:

首先,使用Spring支持的方式创建应用程序上下文。在本例中,您可以使用类路径XML资源来配置Spring应用程序上下文:

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
    "org/flowable/examples/spring/SpringTransactionIntegrationTest-context.xml");

or, as it’s a test:
或者,作为一个测试:

@ContextConfiguration(
  "classpath:org/flowable/spring/test/transaction/SpringTransactionIntegrationTest-context.xml")

Then we can get the service beans and invoke methods on them. The ProcessEngineFactoryBean will have added an extra interceptor to the services that applies Propagation.REQUIRED transaction semantics on the Flowable service methods. So, for example, we can use the repositoryService to deploy a process like this:

然后我们可以获取服务bean并调用bean上的方法。ProcessEngineFactoryBean将为服务添加一个额外的拦截器,该服务应用Flowable 方法上的Propagation.REQUIRED事务语义。因此,如下例所示,我们可以使用repositoryService来部署如下流程:

RepositoryService repositoryService =
  (RepositoryService) applicationContext.getBean("repositoryService");
String deploymentId = repositoryService
  .createDeployment()
  .addClasspathResource("org/flowable/spring/test/hello.bpmn20.xml")
  .deploy()
  .getId();

The other way around also works. In this case, the Spring transaction will be around the userBean.hello() method and the Flowable service method invocation will join that same transaction.

另一种方法也有效。在这种情况下,Spring事务将围绕userBean.hello()方法,并且Flowable服务方法调用将加入同一事务。

UserBean userBean = (UserBean) applicationContext.getBean("userBean");
userBean.hello();

The UserBean looks like this. Remember, from above in the Spring bean configuration, we injected the repositoryService into the userBean.

UserBean看起来像这样。请记住,在上述Spring bean配置中,我们将repositoryService注入到userBean中。

public class UserBean {
     

  /** injected by Spring */
  private RuntimeService runtimeService;

  @Transactional
  public void hello() {
     
    // here you can do transactional stuff in your domain model
    // and it will be combined in the same transaction as
    // the startProcessInstanceByKey to the Flowable RuntimeService
    runtimeService.startProcessInstanceByKey("helloProcess");
  }

  public void setRuntimeService(RuntimeService runtimeService) {
     
    this.runtimeService = runtimeService;
  }
}

你可能感兴趣的:(Flowable,6.6.0,BPMN用户指南,-,(4)Spring)