分布式事物控制

Spring管理多个数据源概念

首先要做分布式数据处理是需要配置多个数据源的,以下是没有事物控制的多个数据源

<!--数据源-->
    <bean name="dataSource1" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 数据连接信息 -->
        <property name="jdbcUrl" value="${jdbc.url1}"></property>
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <bean name="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 数据连接信息 -->
        <property name="jdbcUrl" value="${jdbc.url2}"></property>
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!--JDBC-->
    <bean name="JDBCTemplate1" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource1"></property>
    </bean>
    <bean name="JDBCTemplate2" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource2"></property>
    </bean>

2.通过atomikos控制分布式事物

  1. 添加依赖

    <dependency>
       <groupId>javax.transaction</groupId>
       <artifactId>jta</artifactId>
       <version>1.1</version>
    </dependency>
    <dependency>
      <groupId>org.atteo.moonshine</groupId>
      <artifactId>atomikos</artifactId>
      <version>1.2</version>
    </dependency>
  2. 添加数据源

    <!--数据源-->
    <bean id="dataSource1" class="com.atomikos.jdbc.AtomikosDataSourceBean" destroy-method="close">
        <property name="uniqueResourceName" value="dataSource1"></property>
        <!--驱动-->
        <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/>
        <property name="xaProperties">
            <props>
                <prop key="user">${jdbc.username}</prop>
                <prop key="password">${jdbc.password}</prop>
                <prop key="URL">${jdbc.url1}</prop>
            </props>
        </property>
    </bean>
    <bean id="dataSource2" class="com.atomikos.jdbc.AtomikosDataSourceBean" destroy-method="close">
        <property name="uniqueResourceName" value="dataSource2"></property>
        <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/>
        <property name="xaProperties">
            <props>
                <prop key="user">${jdbc.username}</prop>
                <prop key="password">${jdbc.password}</prop>
                <prop key="URL">${jdbc.url2}</prop>
            </props>
        </property>
    </bean>
  3. 配置atomikos事物管理器

    <!--配置atomikos事物管理器-->
        <bean id="UserTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"/>
        <bean id="UserTransactionManagerImp" class="com.atomikos.icatch.jta.UserTransactionImp"/>
  4. 配置spring事物管理器

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

整体配置文件

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

    <context:annotation-config/>
    <!--扫描包-->
    <context:component-scan base-package="xin.liuyang.dao,xin.liuyang.biz"/>
    <!--扫描配置文件-->
    <context:property-placeholder location="classpath:properties/*.properties"/>
    <!--数据源-->
    <bean id="dataSource1" class="com.atomikos.jdbc.AtomikosDataSourceBean" destroy-method="close">
        <property name="uniqueResourceName" value="dataSource1"></property>
        <!--驱动-->
        <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/>
        <property name="xaProperties">
            <props>
                <prop key="user">${jdbc.username}</prop>
                <prop key="password">${jdbc.password}</prop>
                <prop key="URL">${jdbc.url1}</prop>
            </props>
        </property>
    </bean>
    <bean id="dataSource2" class="com.atomikos.jdbc.AtomikosDataSourceBean" destroy-method="close">
        <property name="uniqueResourceName" value="dataSource2"></property>
        <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/>
        <property name="xaProperties">
            <props>
                <prop key="user">${jdbc.username}</prop>
                <prop key="password">${jdbc.password}</prop>
                <prop key="URL">${jdbc.url2}</prop>
            </props>
        </property>
    </bean>
    <!--配置atomikos事物管理器-->
    <bean id="userTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"/>
    <bean id="userTransactionManagerImp" class="com.atomikos.icatch.jta.UserTransactionImp"/>
    <!--Spring jta事务管理器-->
    <bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManager" ref="userTransactionManager"/>
        <property name="userTransaction" ref="userTransactionManagerImp"/>
    </bean>
    <!--事物管理驱动-->
    <tx:annotation-driven transaction-manager="jtaTransactionManager"/>
    <!--JDBC-->
    <bean name="JDBCTemplate1" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource1"></property>
    </bean>
    <bean name="JDBCTemplate2" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource2"></property>
    </bean>
</beans>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值