spring JTA多數據源事務管理詳細教程

<context:annotation-config />
<!-- 使用註解的包路徑 -->
<context:component-scan
    base-package="com.rongli.service,com.rongli.dao,com.rongli.controller" />
<!-- 支持 @Transactional 標記 -->
<tx:annotation-driven transaction-manager="springJTATransactionManager"
    proxy-target-class="true" />
<!-- 導入cxf配置文件 -->
<import resource="ws.cxf.xml" />

<!-- 加載properties配置文件 -->
<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:jta.jdbc.properties</value>
        </list>
    </property>
</bean>

<!--公有數據庫鏈接池 -->
<bean id="abstractXADataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
    init-method="init" destroy-method="close" abstract="true">
    <property name="xaDataSourceClassName"
        value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
    <property name="poolSize" value="10" />
    <property name="minPoolSize" value="10" />
    <property name="maxPoolSize" value="30" />
    <!--獲取鏈接失敗從新獲等待最大時間,在這個時間內若是有可用鏈接,將返回 -->
    <property name="borrowConnectionTimeout" value="60" />
    <!--最大獲取數據時間,若是不設置這個值,Atomikos使用默認的5分鐘, 那麼在處理大批量數據讀取的時候,一旦超過5分鐘,就會拋出相似 Resultset 
        is close 的錯誤. -->
    <property name="reapTimeout" value="20000" />
    <!-- 最大空閒時間 -->
    <property name="maxIdleTime" value="60" />
    <!--鏈接回收時間 -->
    <property name="maintenanceInterval" value="60" />
    <!--java數據庫鏈接池,最大可等待獲取datasouce的時間 -->
    <property name="loginTimeout" value="60" />
    <property name="testQuery">
        <value>select 1</value>
    </property>
</bean>

<!-- 客戶數據庫 -->
<bean id="rlc_cus" parent="abstractXADataSource">
    <property name="uniqueResourceName" value="mysql/rlc_cus" />
    <property name="xaProperties">
        <props>
            <prop key="URL">${jdbc.rlc_cus.properties}</prop>
            <prop key="user">${jdbc.rlc.user}</prop>
            <prop key="password">${jdbc.rlc.password}</prop>
            <prop key="pinGlobalTxToPhysicalConnection">true</prop>
        </props>
    </property>
</bean>

<!-- 系統數據庫 -->
<bean id="rlc_sys" parent="abstractXADataSource">
    <property name="uniqueResourceName" value="mysql/rlc_sys" />
    <property name="xaProperties">
        <props>
            <prop key="URL">${jdbc.rlc_sys.properties}</prop>
            <prop key="user">${jdbc.rlc.user}</prop>
            <prop key="password">${jdbc.rlc.password}</prop>
            <prop key="pinGlobalTxToPhysicalConnection">true</prop>
        </props>
    </property>
</bean>

<!-- 公有SessionFactory配置 -->
<bean id="baseSessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
    abstract="true">
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.format_sql}</prop>
            <prop key="hibernate.format_sql">${hibernate.show_sql}</prop>
            <prop key="javax.persistence.validation.mode">none</prop>
        </props>
    </property>
    <!-- 自動掃描註解方式配置的hibernate類文件 -->
    <property name="packagesToScan" value="com.rongli.bean"></property>
</bean>

<!-- sessionFactory配置 -->
<!-- customer sessionFactory -->
<bean id="cusSessionFactory" parent="baseSessionFactory">
    <property name="dataSource" ref="rlc_cus" />
</bean>
<!-- system sessionFactory -->
<bean id="sysSessionFactory" parent="baseSessionFactory">
    <property name="dataSource" ref="rlc_sys" />
</bean>

<!-- atomikos事務管理器 -->
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
    init-method="init" destroy-method="close">
    <!-- 調用終止時,強制關閉 -->
    <property name="forceShutdown">
        <value>true</value>
    </property>
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
    <property name="transactionTimeout">
        <value>2000</value>
    </property>
</bean>

<!-- spring 事務管理器 -->
<bean id="springJTATransactionManager"
    class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManager">
        <ref bean="atomikosTransactionManager" />
    </property>
    <property name="userTransaction">
        <ref bean="atomikosUserTransaction" />
    </property>
</bean>

<!-- 用於測試,發佈到服務器上時刪除 -->
<bean id="systemServiceImpl" class="com.rongli.service.impl.SystemServiceImpl">
</bean>
相關文章
相關標籤/搜索