Spring3.X 分布式事务

1.为什么要组建多数据源?

   一个系统中存在多个数据库读写操作,如Oralce+MySql......等

2.为什么要组建分布式事务?

   在同一个系统,同一个事务中对不同数据库进行写操作时,如若一个失败,那么应该对多个数据库进行回滚操作,否则应该全部提交.

3.分布式事务有哪些?

   在Spring 2.5中可以采用jta,在Spring3.X中则已经不对其提供支持,但可以使用atomikos.

4.需要提供哪些jar包支持?

  6个必须的jar包,可以通过maven下载 
atomikos-util-3.9.3.jar 
jta-1.1.jar
transactions-3.9.3.jar
transactions-api-3.9.3.jar 
transactions-jdbc-3.9.3.jar 
transactions-jta-3.9.3.jar

5.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:aop= "http://www.springframework.org/schema/aop"

     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.2.xsd

       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd

       http://www.springframework.org/schema/mvc

       http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

 

     <context:annotation-config />

     <context:component-scan base-package ="com.cn" />

     

     

     

     <bean id ="oracleDS" class= "com.atomikos.jdbc.AtomikosDataSourceBean" init-method= "init" destroy-method ="close">

        <description >oracle xa datasource description >

        <property name ="uniqueResourceName">

            <value >oracle_ds value >

        property >

        <property name ="xaDataSourceClassName">

            <value> oracle.jdbc.xa.client.OracleXADataSourcevalue >

        property >

        <property name ="xaProperties">

            <props >

                <prop key ="user">dongruan prop>

                <prop key ="password">dongruan prop>

                <prop key= "URL">jdbc:oracle:thin:@100.100.100.30:1521: orcl prop>

            props >

        property >

        <property name ="testQuery">

        <value >select 1 from dual value >

        property >

    bean >

 

   

    <bean id ="mysqlDS" class= "com.atomikos.jdbc.AtomikosDataSourceBean" init-method= "init" destroy-method ="close">

        <description >mysql xa datasource description>

        <property name ="uniqueResourceName">

            <value >mysql_ds value >

        property >

        <property name ="xaDataSourceClassName">

            <value> com.mysql.jdbc.jdbc2.optional.MysqlXADataSourcevalue >

        property >

        <property name ="xaProperties">

            <props >

                <prop key ="user">datapre prop>

                <prop key ="password">flow2012 prop>

                <prop key= "URL">jdbc:mysql://100.100.100.48:3306/ dataprepare prop>

            props >

        property >

        <property name ="testQuery">

        <value >select 1 value >

        property >

    bean >

     

     

    <bean id ="oracleSqlSession" class= "org.mybatis.spring.SqlSessionFactoryBean" >

            <property name ="dataSource" ref="oracleDS" />

            <property name ="mapperLocations" value= "classpath:com/cn/oraclemapper/*Mapper.xml" />

            <property name ="typeAliasesPackage" value= "com.cn.oraclepo" />

     bean >

     <bean id ="oracleMapperScanner" class= "org.mybatis.spring.mapper.MapperScannerConfigurer" >

            <property name ="sqlSessionFactoryBeanName" value= "oracleSqlSession" />

            <property name ="basePackage" value= "com.cn.oraclemapper" />

     bean >

 

     

     <bean id ="mysqlSqlSessionFactory" class= "org.mybatis.spring.SqlSessionFactoryBean" >

            <property name ="dataSource" ref="mysqlDS" />

            <property name ="mapperLocations" value= "classpath:com/cn/mapper/*Mapper.xml" />

            <property name ="typeAliasesPackage" value= "com.cn.po" />

     bean >

     <bean id ="mysqlMapperScanner" class= "org.mybatis.spring.mapper.MapperScannerConfigurer" >

            <property name ="sqlSessionFactoryBeanName" value= "mysqlSqlSessionFactory" > property>

            <property name ="basePackage" value="com.cn.mapper" />

     bean >

     

     

     <bean id ="atomikosUserTransaction" class= "com.atomikos.icatch.jta.UserTransactionImp" >

            <description >UserTransactionImp description >

            <property name ="transactionTimeout" value= "300" />

     bean >

     <bean id ="atomikosTransactionManager" class= "com.atomikos.icatch.jta.UserTransactionManager" init-method= "init" destroy-method ="close">

            <description >UserTransactionManager description >

            <property name ="forceShutdown">

                 <value >true value >

            property >

     bean >

     <bean id ="springTransactionManager" class= "org.springframework.transaction.jta.JtaTransactionManager" >

            <description >JtaTransactionManager description >

            <property name ="transactionManager">

                 <ref bean ="atomikosTransactionManager" />

            property >

            <property name ="userTransaction">

                 <ref bean ="atomikosUserTransaction" />

            property >

            <property name ="allowCustomIsolationLevels" value= "true" />

       

     bean >

     <bean id ="atomikosTransactionProxy" class= "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract ="true">

          <property name ="transactionManager">

               <ref bean ="springTransactionManager" />

          property >

          <property name ="transactionAttributes">

               <props >

                <prop key= "*">PROPAGATION_REQUIRED,-Exception prop >

               props >

          property >

     bean >

    

    

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

beans> 

6.测试代码如下:

package com.cn.main;

import java.io.UnsupportedEncodingException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cn.testService;
 

public class main {

     public static void main(String[] args) throws UnsupportedEncodingException {

           ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("DataSource.xml" );

          

           testService service = context.getBean(testService.class );

           service.test();

     }

}

7.Service类:

package com.cn;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import com.cn.mapper.SendPreMapper;

import com.cn.oraclemapper.AirwayPointFlowMapper;

import com.cn.oraclepo.AirwayPointFlow;

import com.cn.po.SendPreWithBLOBs;

@Service

public class testService {

     @Autowired

     public SendPreMapper mysqlMapper;

     @Autowired

     public AirwayPointFlowMapper oracleMapper;

 

     @Transactional

     public void test() {

          SendPreWithBLOBs pre = new SendPreWithBLOBs();

          pre.setId(123457);

           mysqlMapper.insert(pre);

 

          AirwayPointFlow air = new AirwayPointFlow();

          air.setId( "11111111112");

           oracleMapper.insert(air);

     }

}

你可能感兴趣的:(技术贴)