低调人的gravatar头像
低调人 2017-03-16 20:57:10

spring多数据源配置事务回滚

<?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- 在spring中使用annotation来注册bean-->
    <context:component-scan base-package="com" />
    
    <bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
        <property name="uniqueResourceName" value="dataSource"/>
        <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/>
        <property name="xaProperties">
            <props>
                <prop key="url">jdbc:mysql://localhost:3306/db2</prop>
                <prop key="user">root</prop>
                <prop key="password">root</prop>
            </props>
        </property>
        <property name="minPoolSize" value="5"/>
        <property name="maxPoolSize" value="60"/>
        <property name="borrowConnectionTimeout" value="180"/>
        <property name="testQuery" value="select 1 from dual"/>
        <property name="maintenanceInterval" value="30"/>
    </bean>
    
    <!-- 事务管理 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
    </bean>
    <bean id="transactionTravelManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource">
            <ref bean="dataTravelSource"/>
        </property>
    </bean>
    
    
    <bean id="dataTravelSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
        <property name="uniqueResourceName" value="dataTravelSource"/>
        <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/>
        <property name="xaProperties">
            <props>
                <prop key="url">jdbc:mysql://localhost:3306/db1</prop>
                <prop key="user">root</prop>
                <prop key="password">root</prop>
            </props>
        </property>
                <property name="minPoolSize" value="5"/>
        <property name="maxPoolSize" value="60"/>
        <property name="borrowConnectionTimeout" value="180"/>
        <property name="testQuery" value="select 1 from dual"/>
        <property name="maintenanceInterval" value="30"/>
    </bean>


    <!-- 执行强制执行事务回滚 -->
    <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
        <property name="forceShutdown" value="true" />
    </bean>
    <!-- 事务执行的超时时间 -->
    <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
        <property name="transactionTimeout" value="300" />
    </bean>
    <!-- JTA事务管理器 --> 
    <bean id="springJTATransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManager">
            <ref bean="atomikosTransactionManager"/>
        </property>
        <property name="userTransaction">
            <ref bean="atomikosUserTransaction"/>
        </property>
        <property name="allowCustomIsolationLevels" value="true" />
        <!-- 自定义事务名字 使用的时候加上此名字-->
         <qualifier value="jtaTransaction"/>
    </bean>

 

</beans>

注意使用需要使用自定义的启用jta事务


打赏

文件名:源码jar.zip,文件大小:461.833K 下载
  • /
    • /atomikos-3.9.0.rar
    • /sw.zip
最代码最近下载分享源代码列表最近下载
best2018  LV46 2019年1月14日
goatolia  LV1 2018年1月28日
263482604  LV3 2017年12月8日
13526664251  LV2 2017年8月24日
风铃  LV32 2017年8月2日
wangq  LV1 2017年5月16日
alibaba40dadao  LV1 2017年5月8日
低调人  LV38 2017年4月28日
a649783661  LV5 2017年4月11日
ytiian_hm  LV6 2017年3月20日
最代码最近浏览分享源代码列表最近浏览
胡明杨 2023年4月13日
暂无贡献等级
最代码-宋家辉  LV61 2021年11月12日
别碰我被窝  LV9 2021年10月22日
qazxsw_  LV10 2021年8月19日
fellowfun  LV12 2021年8月12日
小可  LV17 2021年7月9日
Destiny微斯人  LV6 2021年3月19日
939867966  LV1 2021年2月25日
jgsu_edu  LV5 2020年12月30日
ljj123321  LV12 2020年12月29日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友