Spring JTA——使用Atomikos实现分布式事务

问题

多个数据源连接多个数据库,如何保证事务

配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="top.yuyufeng.learn.jta"></context:component-scan>

    <!--数据源1-->
    <bean id="dataSource1" class="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean" init-method="init"  destroy-method="close">
        <property name="uniqueResourceName" value="XA1DBMS" />

        <property name="url" value="jdbc:mysql://127.0.0.1:3306/learn1?useUnicode=true&amp;characterEncoding=utf-8" />
        <property name="user" value="root" />
        <property name="password" value="12345" />
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />

        <property name="poolSize" value="3" />
        <property name="minPoolSize" value="3" />
        <property name="maxPoolSize" value="5" />
    </bean>

    <!--数据源2-->
  <bean id="dataSource2" class="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean" init-method="init"  destroy-method="close">
        <property name="uniqueResourceName" value="XA2DBMS" />

        <property name="url" value="jdbc:mysql://127.0.0.1:3306/learn2?useUnicode=true&amp;characterEncoding=utf-8" />
        <property name="user" value="root" />
        <property name="password" value="12345" />
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />

        <property name="poolSize" value="3" />
        <property name="minPoolSize" value="3" />
        <property name="maxPoolSize" value="5" />
    </bean>


    <bean id="jdbcTemplate1" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource1"></property>
    </bean>

    <bean id="jdbcTemplate2" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource2"></property>
    </bean>

    <!--面向用户-->
    <bean id="userTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
        <property name="transactionTimeout" value="300" />
    </bean>

    <!--事务管理器-->
    <bean id="springTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="userTransaction" ref="userTransaction" />
    </bean>

    <tx:annotation-driven transaction-manager="springTransactionManager"/>

</beans>

猜你喜欢

转载自blog.csdn.net/qq_18860653/article/details/80062344