分佈式事物控制

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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章