Activiti 使用spring 進行統一事務管理

由於Activiti使用自己的表單結構和操作,爲了和業務數據處理的時候統一在一個事務管理裏面,需要通過spring 的 applicationContext.xml 進行配置

由於activiti 的相關配置 processEngineConfiguration 中需要制定 dataSource ,並且沒有發現怎麼從jpa的persistence-unit中獲取相關jdbc的配置。所以只能通過讓spring的tx 和 processEngineConfiguration 中的 dataSource指向同一個數據鏈接,這樣才能管理在同一個事務裏面。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="http://www.springframework.org/schema/beans     
          http://www.springframework.org/schema/beans/spring-beans-4.2.xsd     
          http://www.springframework.org/schema/context     
          http://www.springframework.org/schema/context/spring-context-4.2.xsd     
          http://www.springframework.org/schema/aop     
          http://www.springframework.org/schema/aop/spring-aop-4.2.xsd     
          http://www.springframework.org/schema/tx      
          http://www.springframework.org/schema/tx/spring-tx-4.2.xsd  
          http://www.springframework.org/schema/cache   
          http://www.springframework.org/schema/cache/spring-cache-4.2.xsd  
          http://www.springframework.org/schema/data/jpa  
          http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
    <context:annotation-config />
    <context:component-scan base-package="com.ninelephas.whale" />
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://192.168.1.111:3306/whale?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false" />
        <property name="user" value="whale" />
        <property name="password" value="whale" />
        <!--當連接池中的連接耗盡的時候c3p0一次同時獲取的連接數。Default: 3 -->
        <property name="acquireIncrement" value="3" />
        <!--定義在從數據庫獲取新連接失敗後重復嘗試的次數。Default: 30 -->
        <property name="acquireRetryAttempts" value="30" />
        <!--兩次連接中間隔時間,單位毫秒。Default: 1000 -->
        <property name="acquireRetryDelay" value="1000" />

        <!--連接關閉時默認將所有未提交的操作回滾。Default: false -->
        <!-- <property name="autoCommitOnClose" value="false" /> -->
        <!--c3p0將建一張名爲Test的空表,並使用其自帶的查詢語句進行測試。如果定義了這個參數那麼 屬性preferredTestQuery將被忽略。你不能在這張Test表上進行任何操作,它將只供c3p0測試 使用。Default: null -->
        <!-- <property name="automaticTestTable" value="test" /> -->
        <!--獲取連接失敗將會引起所有等待連接池來獲取連接的線程拋出異常。但是數據源仍有效 保留,並在下次調用getConnection()的時候繼續嘗試獲取連接。如果設爲true,那麼在嘗試 獲取連接失敗後該數據源將申明已斷開並永久關閉。Default: false -->
        <!-- <property name="breakAfterAcquireFailure" value="false" /> -->
        <!--當連接池用完時客戶端調用getConnection()後等待獲取新連接的時間,超時後將拋出 SQLException,如設爲0則無限期等待。單位毫秒。Default: 0 -->
        <!-- <property name="checkoutTimeout" value="100" /> -->
        <!--通過實現ConnectionTester或QueryConnectionTester的類來測試連接。類名需制定全路徑。 Default: com.mchange.v2.c3p0.impl.DefaultConnectionTester -->
        <!-- <property name="connectionTesterClassName" value=""></property> -->
        <!--指定c3p0 libraries的路徑,如果(通常都是這樣)在本地即可獲得那麼無需設置,默認null即可 Default: null -->
        <!-- <property name="factoryClassLocation" value="null"/> -->
        <!--Strongly disrecommended. Setting this to true may lead to subtle and bizarre bugs. (文檔原文)作者強烈建議不使用的一個屬性 -->
        <!-- <property name="forceIgnoreUnresolvedTransactions" value="false"/> -->
        <!--每60秒檢查所有連接池中的空閒連接。Default: 0 -->
        <!-- <property name="idleConnectionTestPeriod" value="60"/> -->
        <!--初始化時獲取三個連接,取值應在minPoolSize與maxPoolSize之間。Default: 3 -->
        <!-- <property name="initialPoolSize" value="3" /> -->
        <!--最大空閒時間,60秒內未使用則連接被丟棄。若爲0則永不丟棄。Default: 0 -->
        <!-- <property name="maxIdleTime" value="60" /> -->
        <!--連接池中保留的最大連接數。Default: 15 -->
        <!-- <property name="maxPoolSize" value="15"/> -->
        <!--JDBC的標準參數,用以控制數據源內加載的PreparedStatements數量。但由於預緩存的statements 屬於單個connection而不是整個連接池。所以設置這個參數需要考慮到多方面的因素。 如果maxStatements與maxStatementsPerConnection均爲0,則緩存被關閉。Default: 0 -->
        <!-- <property name="maxStatements" value="100"/> -->
        <!--maxStatementsPerConnection定義了連接池內單個連接所擁有的最大緩存statements數。Default: 0 -->
        <!-- <property name="maxStatementsPerConnection" value="0"></property> -->
        <!--c3p0是異步操作的,緩慢的JDBC操作通過幫助進程完成。擴展這些操作可以有效的提升性能 通過多線程實現多個操作同時被執行。Default: 3 -->
        <!-- <property name="numHelperThreads" value="3" /> -->
        <!--當用戶調用getConnection()時使root用戶成爲去獲取連接的用戶。主要用於連接池連接非c3p0 的數據源時。Default: null -->
        <!-- <property name="overrideDefaultUser" value="root" /> -->
        <!--與overrideDefaultUser參數對應使用的一個參數。Default: null -->
        <!-- <property name="overrideDefaultPassword" value="password" /> -->
        <!--定義所有連接測試都執行的測試語句。在使用連接測試的情況下這個一顯著提高測試速度。注意: 測試的表必須在初始數據源的時候就存在。Default: null -->
        <!-- <property name="preferredTestQuery"  value="select id from test where id=1"/> -->
        <!--用戶修改系統配置參數執行前最多等待300秒。Default: 300 -->
        <!-- <property name="propertyCycle" value="300" /> -->
        <!--因性能消耗大請只在需要的時候使用它。如果設爲true那麼在每個connection提交的 時候都將校驗其有效性。建議使用idleConnectionTestPeriod或automaticTestTable 等方法來提升連接測試的性能。Default: false -->
        <!-- <property name="testConnectionOnCheckout" value="false"/> -->
        <!--如果設爲true那麼在取得連接的同時將校驗連接的有效性。Default: false -->
        <!-- <property name="testConnectionOnCheckin" value="true"/> -->
    </bean>
    <!-- JPA實體管理器工廠 -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceProvider">
            <bean id="persistenceProvider" class="org.hibernate.ejb.HibernatePersistence" />
        </property>
        <property name="packagesToScan" value="com.ninelephas.whale.pojo" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="generateDdl" value="true" />
                <property name="database" value="MYSQL" />
            </bean>
        </property>
        <property name="jpaDialect">
            <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
    </bean>
    <!-- 配置事務管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
        <property name="jpaDialect">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
        </property>
    </bean>
    <!-- 啓用 annotation事務 -->
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
    <!-- 配置Spring Data JPA掃描目錄 -->
    <jpa:repositories base-package="com.ninelephas.whale" />
    <!-- 激活自動代理功能 -->
    <aop:aspectj-autoproxy proxy-target-class="true" />
    <!-- Activiti引擎配置 -->
    <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
        <property name="dataSource" ref="dataSource" />
        <property name="databaseType" value="mysql" />
        <property name="transactionManager" ref="transactionManager" />
        <property name="databaseSchemaUpdate" value="true" />
        <property name="jpaEntityManagerFactory" ref="entityManagerFactory" />
        <property name="jpaHandleTransaction" value="true" />
        <property name="jpaCloseEntityManager" value="true" />
        <property name="jobExecutorActivate" value="false" />
    </bean>
    <!-- Activiti引擎工廠 -->
    <bean id="processEngineFactory" class="org.activiti.spring.ProcessEngineFactoryBean">
        <property name="processEngineConfiguration" ref="processEngineConfiguration" />
    </bean>
    <!-- Activiti的Service -->
    <bean id="repositoryService" factory-bean="processEngineFactory" factory-method="getRepositoryService" />
    <bean id="runtimeService" factory-bean="processEngineFactory" factory-method="getRuntimeService" />
    <bean id="formService" factory-bean="processEngineFactory" factory-method="getFormService" />
    <bean id="identityService" factory-bean="processEngineFactory" factory-method="getIdentityService" />
    <bean id="taskService" factory-bean="processEngineFactory" factory-method="getTaskService" />
    <bean id="historyService" factory-bean="processEngineFactory" factory-method="getHistoryService" />
    <bean id="managementService" factory-bean="processEngineFactory" factory-method="getManagementService" />
</beans>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章