分布式事务--XA 解决多个数据源操作

个人备忘

事务(官方解释):是由一组sql语句组成的“逻辑处理单元”。

事务具有如下四个属性,通常称为事务的ACID属性 :
1. 原子性(Atomicity): 事务是一个原子操作单元,要么都执行,要么都不执行。
2. 一致性(Consistent):在事务开始和完成时,数据都必须保持一致。
3. 隔离性(Isoation): 数据库系统提供一定的隔离机制,保证事务在不受外部并发操作影响的“独立”环境执行。
4. 持久性(Durabe): 事务完成之后,它对数据的修改是永久性的。

分布式事务 : 分布式事务就是指事务的参与者,支持事务的服务器,资源服务器,以及事务管理器分别位于不同的分布式系统的不同节点之上。

本质上来说,分布式事务就是为了保证“不同数据库的数据一致性” 。

分布式事务产生场景 :

分布式事务--XA 解决多个数据源操作_第1张图片

分布式事务管理器 :

XA 协议 是可以在数据库conmit 之后进行回滚的。

分布式事务--XA 解决多个数据源操作_第2张图片

常见分布式事务解决方案 :

通过日志来记录操作 ,从上到下任何一步有问题,就会rollback , 如下图 :

分布式事务--XA 解决多个数据源操作_第3张图片

XA优缺点:

常见数据库都对XA 协议有支持,成本低。

分布式事务--XA 解决多个数据源操作_第4张图片

XA相关框架:

分布式事务--XA 解决多个数据源操作_第5张图片

Atomikos 代码实现:

基于ssm

//pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.xygroupId>
    <artifactId>springmvc-mybatisartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>warpackaging>
    <properties>
        <spring.version>4.1.1.RELEASEspring.version>
    properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-surefire-pluginartifactId>
                <version>2.15version>
                <configuration>
                    <skip>trueskip>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.1version>
                <configuration>
                    <source>1.7source>
                    <target>1.7target>
                    <debug>truedebug>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-war-pluginartifactId>
                
                
                
            plugin>
            <plugin>
                <groupId>org.mortbay.jettygroupId>
                <artifactId>maven-jetty-pluginartifactId>
                <version>6.1.26version>
                <configuration>
                    <scanIntervalSeconds>10scanIntervalSeconds>
                    <webAppConfig>
                        <contextPath>/maven-appcontextPath>
                    webAppConfig>
                configuration>
            plugin>
        plugins>
    build>
    <dependencies>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-context-supportartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-coreartifactId>
            <version>2.3.0version>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.3.0version>
        dependency>
        <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-classicartifactId>
            <version>1.0.11version>
        dependency>
        <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-coreartifactId>
            <version>1.0.11version>
        dependency>
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
            <version>1.7.6version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>2.5version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-ormartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-txartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>commons-dbcpgroupId>
            <artifactId>commons-dbcpartifactId>
            <version>1.4version>
        dependency>
        <dependency>
            <groupId>commons-fileuploadgroupId>
            <artifactId>commons-fileuploadartifactId>
            <version>1.3.1version>
        dependency>
        <dependency>
            <groupId>com.mchangegroupId>
            <artifactId>c3p0artifactId>
            <version>0.9.2.1version>
        dependency>

        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>3.1.0version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.2.7version>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.0.8version>
        dependency>

        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.2.2version>
        dependency>
        <dependency>
            <groupId>com.atomikosgroupId>
            <artifactId>atomikos-utilartifactId>
            <version>3.7.0version>
        dependency>
        <dependency>
            <groupId>com.atomikosgroupId>
            <artifactId>transactions-jtaartifactId>
            <version>3.7.0version>
        dependency>
        <dependency>
            <groupId>com.atomikosgroupId>
            <artifactId>transactionsartifactId>
            <version>3.7.0version>
        dependency>
        <dependency>
            <groupId>com.atomikosgroupId>
            <artifactId>transactions-apiartifactId>
            <version>3.7.0version>
        dependency>
        <dependency>
            <groupId>com.atomikosgroupId>
            <artifactId>transactions-jdbcartifactId>
            <version>3.7.0version>
        dependency>
        <dependency>
            <groupId>org.codehaus.btmgroupId>
            <artifactId>btmartifactId>
            <version>2.1.4version>
        dependency>

    dependencies>
project>

spring-mybatis.xml


<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xmlns:cache="http://www.springframework.org/schema/cache" xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
       http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
    <context:component-scan base-package="com.xy">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    context:component-scan>
    <context:property-placeholder location="classpath:context/database.properties"/>

    <tx:annotation-driven/>

    <bean id="abstractXADataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init"
          destroy-method="close" abstract="true">
        <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/>
        <property name="poolSize" value="10" />
        <property name="minPoolSize" value="10"/>
        <property name="maxPoolSize" value="30"/>
        <property name="borrowConnectionTimeout" value="60"/>
        <property name="reapTimeout" value="20"/>
        
        <property name="maxIdleTime" value="60"/>
        <property name="maintenanceInterval" value="60"/>
        <property name="loginTimeout" value="60"/>
        <property name="testQuery">
            <value>select 1value>
        property>
    bean>

    <bean id="qadataSource" parent="abstractXADataSource">
        
        <property name="uniqueResourceName" value="mysql/sitestone1" />
        <property name="xaDataSourceClassName"
                  value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
        <property name="xaProperties">
            <props>
                <prop key="URL">${qa.db.url}prop>
                <prop key="user">${qa.db.user}prop>
                <prop key="password">${qa.db.password}prop>
                <prop key="pinGlobalTxToPhysicalConnection">trueprop>
            props>
        property>
    bean>

    <bean id="devdataSource" parent="abstractXADataSource">
        
        <property name="uniqueResourceName" value="mysql/sitestone" />
        <property name="xaDataSourceClassName"
                  value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
        <property name="xaProperties">
            <props>
                <prop key="URL">${dev.db.url}prop>
                <prop key="user">${dev.db.user}prop>
                <prop key="password">${dev.db.password}prop>
                <prop key="pinGlobalTxToPhysicalConnection">trueprop>
            props>
        property>
    bean>

    <bean id="qasqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="qadataSource" />
        <property name="mapperLocations" value="classpath*:com/xy/dao/*.xml" />
    bean>

    <bean id="devsqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="devdataSource" />
        <property name="mapperLocations" value="classpath*:com/xy/daodev/*.xml" />
    bean>

    <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
          init-method="init" destroy-method="close">
        <property name="forceShutdown">
            <value>truevalue>
        property>
    bean>
    <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
        <property name="transactionTimeout" value="300" />
    bean>

    <bean id="transactionManager"
          class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManager">
            <ref bean="atomikosTransactionManager"/>
        property>
        <property name="userTransaction">
            <ref bean="atomikosUserTransaction"/>
        property>
        
        <property name="allowCustomIsolationLevels" value="true"/>

    bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.xy.dao"/>
        <property name="sqlSessionFactoryBeanName" value="qasqlSessionFactory" />
    bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.xy.daodev"/>
        <property name="sqlSessionFactoryBeanName" value="devsqlSessionFactory" />
    bean>
beans>

database.properties

qa.db.url=jdbc:mysql://localhost:3306/qa?useUnicode=true&;characterEncoding=UTF-8
qa.db.user=root
qa.db.password=123456

dev.db.url=jdbc:mysql://localhost:3306/dev?useUnicode=true&;characterEncoding=UTF-8
dev.db.user=root
dev.db.password=123456

jta.properties

com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory
com.atomikos.icatch.console_file_name = tm.out
com.atomikos.icatch.log_base_name = tmlog
com.atomikos.icatch.tm_unique_name = com.atomikos.spring.jdbc.tm
com.atomikos.icatch.console_log_level = INFO
com.atomikos.icatch.output_dir=/hello/atomikos
com.atomikos.icatch.log_base_dir=/hello/atomikos
com.atomikos.icatch.serial_jta_transactions=false

java代码 :

service

package com.xy.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xy.dao.NameQaMapper;
import com.xy.daodev.NameDevMapper;
import com.xy.model.NameDev;
import com.xy.model.NameQa;

@Service
public class NameService {

    @Autowired
    NameQaMapper qaMapper;
    @Autowired
    NameDevMapper devMapper;

    @Transactional(rollbackFor=Exception.class)
    public void addQaAndDev(boolean hasException) throws Exception{

        NameQa nameQa = new NameQa();
        nameQa.setNameQa("hello qa");       
        qaMapper.insert(nameQa);

        NameDev dev = new NameDev();
        dev.setNameDev("hello dev");
        devMapper.insert(dev);

        if (hasException) {
            throw new Exception();
        }
    }
}

controller 代码

package com.xy.controller;

import com.xy.service.NameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Created by helloworld on 2014/11/22.
 */
@Controller
public class mybatisController {

    @Autowired
    NameService nameService;

    @RequestMapping(value = "/addName", method = RequestMethod.GET)
    ModelMap addName(@RequestParam("hasException") boolean hasException) {
        try {
            nameService.addQaAndDev(hasException);
        } catch (Exception e) {
            e.printStackTrace();
            return new ModelMap("false");
        }
        return new ModelMap("true");
    }


}

model 和 mapper 没什么可说的,这里就不粘贴了。

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