分布式事物控制

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>
    
    <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.transactiongroupId>
       <artifactId>jtaartifactId>
       <version>1.1version>
    dependency>
    <dependency>
      <groupId>org.atteo.moonshinegroupId>
      <artifactId>atomikosartifactId>
      <version>1.2version>
    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事物管理器

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

    
    <bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
         <property name="transactionManager" ref="userTransactionManager"/>
         <property name="userTransaction" ref="userTransactionManagerImp"/>
    bean>

整体配置文件


<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>
    
    <bean id="userTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"/>
    <bean id="userTransactionManagerImp" class="com.atomikos.icatch.jta.UserTransactionImp"/>
    
    <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"/>
    
    <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>

你可能感兴趣的:(分布式方案)