本文介绍基于spring+mybatis+atomikos+jta实现分布式事务,由程序动态切换数据源,通过atomikos可实现分布式事务一致性。

版本:spring-3.2.9.RELEASE、mybatis-3.4.4、atomikos-4.0.5、jdk1.8

1,maven配置文件pom.xml如下:

    
    
        junit
        junit
        4.12
        test
        
            
                hamcrest-core
                org.hamcrest
            
        
    
    
        org.hamcrest
        hamcrest-all
        1.3
        test
    
    
        org.mockito
        mockito-core
        1.9.5
        test
        
            
                hamcrest-core
                org.hamcrest
            
        
    
    
        com.jayway.jsonpath
        json-path
        0.8.1
        test
    
    
    
        org.springframework
        spring-aop
        ${spring.version}
    
    
        org.springframework
        spring-beans
        ${spring.version}
    
    
        org.springframework
        spring-context
        ${spring.version}
    
    
        org.springframework
        spring-core
        ${spring.version}
    
    
        org.springframework
        spring-expression
        ${spring.version}
    
    
        org.springframework
        spring-web
        ${spring.version}
    
    
        org.springframework
        spring-webmvc
        ${spring.version}
    
    
        org.springframework
        spring-webmvc-portlet
        ${spring.version}
    
    
        org.springframework
        spring-tx
        ${spring.version}
    
    
        org.springframework
        spring-jdbc
        ${spring.version}
    
    
        org.springframework
        spring-test
        3.2.18.RELEASE
    
    
        org.springframework
        spring-struts
        ${spring.version}
    
    
    
        org.mybatis
        mybatis
        3.4.4
    
    
        org.mybatis
        mybatis-spring
        1.3.1
    

    
    
        mysql
        mysql-connector-java
        5.1.27
    

    
    
        com.atomikos
        transactions-jdbc
        4.0.5
    
    

    
    
        javax.transaction
        jta
        1.1
    
    
    
    
        org.codehaus.jackson
        jackson-mapper-asl
        1.9.13
    
    
        org.codehaus.jackson
        jackson-core-asl
        1.9.13
    
    
    
        org.aspectj
        aspectjweaver
        1.8.13
    

    
        log4j
        log4j
        1.2.16
    
    
        javax.servlet
        servlet-api
        2.5
        provided
    

2,数据库连接配置文件:jdbc.properties

jdbc.xaDataSourceClassName=com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
validationQuery=select 1
ds1.jdbc.url=jdbc:mysql://xx.xx.xx.xx:3306/test1?characterEncoding=UTF-8
ds1.jdbc.username=xxxx
ds1.jdbc.password=xxxx

ds2.jdbc.url=jdbc:mysql://xx.xx.xx.xx:3306/test2?characterEncoding=UTF-8
ds2.jdbc.username=xxxx
ds2.jdbc.password=xxxx

3,atomikos配置文件:jta.properties

com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory
com.atomikos.icatch.console_file_name=tm.release.out
com.atomikos.icatch.log_base_name=tm.releaselog
com.atomikos.icatch.tm_unique_name=com.atomikos.spring.jdbc.tm.release
com.atomikos.icatch.console_log_level=INFO

4,spring-datasource-jta.xml




  
     
         
            classpath*:jdbc.properties
        
    


    
        
            
        
    
    
        
            
        
    



    
    
    
    
    
    
    
    
    


    
    
    
        
            ${ds1.jdbc.url}
            ${ds1.jdbc.username}
            ${ds1.jdbc.password}
        
    


    
    
    
        
            ${ds2.jdbc.url}
            ${ds2.jdbc.username}
            ${ds2.jdbc.password}
            true
        
    
    
    
    
    
  

5,spring-mybatis-dynamic.xml









    
    
        
            classpath:mybatis/model/*.xml
        
    


    
    
        
            classpath:mybatis/model/*.xml
        
    
 

 
    
    
        
            
            
        
    
 

 
    
    
 

 
    
     
         
             
         
     
  

6,编写mybatis.model代码,HelloDO.java、 HelloMapper.java、HelloMapper.xml

HelloDO.java

package mybatis.model;

public class HelloDO {
    private Long id;
    private String name;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

HelloMapper.java
package mybatis.model;

import org.springframework.stereotype.Repository;

@Repository(value="helloMapper")
public interface HelloMapper {
    public void insert(HelloDO hello);
}

HelloMapper.xml



    
        
        
    

    
        insert into
        HELLO(ID,NAME)
        values(#{id},#{name})
    

7,编写service类:mybatis.dynamic.service.HelloWorldServiceDynamic.java

package mybatis.dynamic.service;

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

import mybatis.model.HelloDO;
import mybatis.model.HelloMapper;

@Service("helloWorldServiceDynamic")
public class HelloWorldServiceDynamic {
    @Autowired
    private HelloMapper helloMapper;

    public void addHello(HelloDO helloDO) {
        // TODO Auto-generated method stub
        helloMapper.insert(helloDO);
    }

}

8,编写controller类:mybatis.dynamic.controler.HelloWorldController.java

package mybatis.dynamic.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import mybatis.dynamic.service.HelloWorldServiceDynamic;
import mybatis.model.HelloDO;
import spring.jta.util.DataSourceContextHolder;

@Controller(value = "helloWorldController")
@RequestMapping(value="/hello")
public class HelloWorldControler {
    @Autowired
    private HelloWorldServiceDynamic helloWorldServiceDynamic;

    @Transactional
    @RequestMapping(value="create_dynamic",method=RequestMethod.POST)
    public @ResponseBody long createHelloDynamic(@RequestBody HelloDO helloDO) {
        DataSourceContextHolder.setDBType("dataSource");
        helloWorldServiceDynamic.addHello(helloDO);
        DataSourceContextHolder.setDBType("dataSource2");
        helloWorldServiceDynamic.addHello(helloDO);
        return helloDO.getId();
    }
}

9,编写测试类:HelloWorldControlerDynamicTest.java

package mybatis.controller;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectWriter;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import mybatis.model.HelloDO;

@RunWith(SpringJUnit4Cla***unner.class)
@WebAppConfiguration
@ContextConfiguration({ "classpath:spring-datasource-jta.xml", "classpath:spring-mybatis-dynamic.xml" })
public class HelloWorldControlerDynamicTest {
    @Autowired
    private WebApplicationContext context;
    private MockMvc mocMvc;

    @Before
    public void setUp() throws Exception {
        mocMvc = MockMvcBuilders.webAppContextSetup(context).build();
    }

    @Test
    public void test() throws Exception {
        String data = "{\"id\":1,\"name\":\"abc\"}";
        HelloDO hello = new HelloDO();
        hello.setId(1L);
        hello.setName("abc");
        ObjectMapper mapper = new ObjectMapper();
    ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
    java.lang.String requestJson = ow.writeValueAsString(hello);
        mocMvc.perform(post("/hello/create_dynamic").contentType(MediaType.APPLICATION_JSON).content(data)).andExpect(status().isOk());
    }
}

10,编写数据源切换类 DataSourceContextHolder.java

package spring.jta.util;

public class DataSourceContextHolder {
    private static final ThreadLocal contextHolder = new ThreadLocal();

    public static void setDBType(String dbType) {
        contextHolder.set(dbType);
    }

    public static String getDBType() {
        return ((String) contextHolder.get());
    }

    public static void clearDBType() {
        contextHolder.remove();
    }
}

11,编写CustomSqlSessionTemplate.java,该类扩展SqlSessionTemplate,结合DataSourceContextHolder实现sqlSession的切换

package spring.jta.util;

import static java.lang.reflect.Proxy.newProxyInstance;
import static org.apache.ibatis.reflection.ExceptionUtil.unwrapThrowable;
import static org.mybatis.spring.SqlSessionUtils.closeSqlSession;
import static org.mybatis.spring.SqlSessionUtils.getSqlSession;
import static org.mybatis.spring.SqlSessionUtils.isSqlSessionTransactional;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.executor.BatchResult;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.MyBatisExceptionTranslator;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.util.Assert;

/**
 * function: 继承SqlSessionTemplate 重写相关方法
 * @author hoojo
 * @createDate 2013-10-18 下午03:07:46
 * @file CustomSqlSessionTemplate.java
 * @package com.hoo.framework.mybatis.support
 * @project SHMB
 * @blog http://blog.csdn.net/IBM_hoojo
 * @email [email protected]
 * @version 1.0
 */
public class CustomSqlSessionTemplate extends SqlSessionTemplate {

    private final SqlSessionFactory sqlSessionFactory;
    private final ExecutorType executorType;
    private final SqlSession sqlSessionProxy;
    private final PersistenceExceptionTranslator exceptionTranslator;

    private Map targetSqlSessionFactorys;
    private SqlSessionFactory defaultTargetSqlSessionFactory;

    public void setTargetSqlSessionFactorys(Map targetSqlSessionFactorys) {
        this.targetSqlSessionFactorys = targetSqlSessionFactorys;
    }

    public void setDefaultTargetSqlSessionFactory(SqlSessionFactory defaultTargetSqlSessionFactory) {
        this.defaultTargetSqlSessionFactory = defaultTargetSqlSessionFactory;
    }

    public CustomSqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        this(sqlSessionFactory, sqlSessionFactory.getConfiguration().getDefaultExecutorType());
    }

    public CustomSqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType) {
        this(sqlSessionFactory, executorType, new MyBatisExceptionTranslator(sqlSessionFactory.getConfiguration()
                .getEnvironment().getDataSource(), true));
    }

    public CustomSqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,
            PersistenceExceptionTranslator exceptionTranslator) {

        super(sqlSessionFactory, executorType, exceptionTranslator);

        this.sqlSessionFactory = sqlSessionFactory;
        this.executorType = executorType;
        this.exceptionTranslator = exceptionTranslator;

        this.sqlSessionProxy = (SqlSession) newProxyInstance(
                SqlSessionFactory.class.getClassLoader(),
                new Class[] { SqlSession.class }, 
                new SqlSessionInterceptor());

        this.defaultTargetSqlSessionFactory = sqlSessionFactory;
    }

    @Override
    public SqlSessionFactory getSqlSessionFactory() {

        SqlSessionFactory targetSqlSessionFactory = targetSqlSessionFactorys.get(DataSourceContextHolder.getDBType());
        if (targetSqlSessionFactory != null) {
            return targetSqlSessionFactory;
        } else if (defaultTargetSqlSessionFactory != null) {
            return defaultTargetSqlSessionFactory;
        } else {
            Assert.notNull(targetSqlSessionFactorys, "Property 'targetSqlSessionFactorys' or 'defaultTargetSqlSessionFactory' are required");
            Assert.notNull(defaultTargetSqlSessionFactory, "Property 'defaultTargetSqlSessionFactory' or 'targetSqlSessionFactorys' are required");
        }
        return this.sqlSessionFactory;
    }

    public List getAllConfigurations() {
        List list = new ArrayList();
        for(Map.Entry entry : targetSqlSessionFactorys.entrySet()) {
            SqlSessionFactory sqlSessionFactory = entry.getValue();
            list.add(sqlSessionFactory.getConfiguration());
        }
        return list;
    }

    @Override
    public Configuration getConfiguration() {
        return this.getSqlSessionFactory().getConfiguration();
    }

    public ExecutorType getExecutorType() {
        return this.executorType;
    }

    public PersistenceExceptionTranslator getPersistenceExceptionTranslator() {
        return this.exceptionTranslator;
    }

    /**
     * {@inheritDoc}
     */
    public  T selectOne(String statement) {
        return this.sqlSessionProxy. selectOne(statement);
    }

    /**
     * {@inheritDoc}
     */
    public  T selectOne(String statement, Object parameter) {
        return this.sqlSessionProxy. selectOne(statement, parameter);
    }

    /**
     * {@inheritDoc}
     */
    public  Map selectMap(String statement, String mapKey) {
        return this.sqlSessionProxy. selectMap(statement, mapKey);
    }

    /**
     * {@inheritDoc}
     */
    public  Map selectMap(String statement, Object parameter, String mapKey) {
        return this.sqlSessionProxy. selectMap(statement, parameter, mapKey);
    }

    /**
     * {@inheritDoc}
     */
    public  Map selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) {
        return this.sqlSessionProxy. selectMap(statement, parameter, mapKey, rowBounds);
    }

    /**
     * {@inheritDoc}
     */
    public  List selectList(String statement) {
        return this.sqlSessionProxy. selectList(statement);
    }

    /**
     * {@inheritDoc}
     */
    public  List selectList(String statement, Object parameter) {
        return this.sqlSessionProxy. selectList(statement, parameter);
    }

    /**
     * {@inheritDoc}
     */
    public  List selectList(String statement, Object parameter, RowBounds rowBounds) {
        return this.sqlSessionProxy. selectList(statement, parameter, rowBounds);
    }

    /**
     * {@inheritDoc}
     */
    public void select(String statement, ResultHandler handler) {
        this.sqlSessionProxy.select(statement, handler);
    }

    /**
     * {@inheritDoc}
     */
    public void select(String statement, Object parameter, ResultHandler handler) {
        this.sqlSessionProxy.select(statement, parameter, handler);
    }

    /**
     * {@inheritDoc}
     */
    public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) {
        this.sqlSessionProxy.select(statement, parameter, rowBounds, handler);
    }

    /**
     * {@inheritDoc}
     */
    public int insert(String statement) {
        return this.sqlSessionProxy.insert(statement);
    }

    /**
     * {@inheritDoc}
     */
    public int insert(String statement, Object parameter) {
        return this.sqlSessionProxy.insert(statement, parameter);
    }

    /**
     * {@inheritDoc}
     */
    public int update(String statement) {
        return this.sqlSessionProxy.update(statement);
    }

    /**
     * {@inheritDoc}
     */
    public int update(String statement, Object parameter) {
        return this.sqlSessionProxy.update(statement, parameter);
    }

    /**
     * {@inheritDoc}
     */
    public int delete(String statement) {
        return this.sqlSessionProxy.delete(statement);
    }

    /**
     * {@inheritDoc}
     */
    public int delete(String statement, Object parameter) {
        return this.sqlSessionProxy.delete(statement, parameter);
    }

    /**
     * {@inheritDoc}
     */
    public  T getMapper(Class type) {
        return getConfiguration().getMapper(type, this);
    }

    /**
     * {@inheritDoc}
     */
    public void commit() {
        throw new UnsupportedOperationException("Manual commit is not allowed over a Spring managed SqlSession");
    }

    /**
     * {@inheritDoc}
     */
    public void commit(boolean force) {
        throw new UnsupportedOperationException("Manual commit is not allowed over a Spring managed SqlSession");
    }

    /**
     * {@inheritDoc}
     */
    public void rollback() {
        throw new UnsupportedOperationException("Manual rollback is not allowed over a Spring managed SqlSession");
    }

    /**
     * {@inheritDoc}
     */
    public void rollback(boolean force) {
        throw new UnsupportedOperationException("Manual rollback is not allowed over a Spring managed SqlSession");
    }

    /**
     * {@inheritDoc}
     */
    public void close() {
        throw new UnsupportedOperationException("Manual close is not allowed over a Spring managed SqlSession");
    }

    /**
     * {@inheritDoc}
     */
    public void clearCache() {
        this.sqlSessionProxy.clearCache();
    }

    /**
     * {@inheritDoc}
     */
    public Connection getConnection() {
        return this.sqlSessionProxy.getConnection();
    }

    /**
     * {@inheritDoc}
     * @since 1.0.2
     */
    public List flushStatements() {
        return this.sqlSessionProxy.flushStatements();
    }

    /**
     * Proxy needed to route MyBatis method calls to the proper SqlSession got from Spring's Transaction Manager It also
     * unwraps exceptions thrown by {@code Method#invoke(Object, Object...)} to pass a {@code PersistenceException} to
     * the {@code PersistenceExceptionTranslator}.
     */
    private class SqlSessionInterceptor implements InvocationHandler {
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            final SqlSession sqlSession = getSqlSession(
                    CustomSqlSessionTemplate.this.getSqlSessionFactory(),
                    CustomSqlSessionTemplate.this.executorType, 
                    CustomSqlSessionTemplate.this.exceptionTranslator);
            try {
                Object result = method.invoke(sqlSession, args);
                if (!isSqlSessionTransactional(sqlSession, CustomSqlSessionTemplate.this.getSqlSessionFactory())) {
                    // force commit even on non-dirty sessions because some databases require
                    // a commit/rollback before calling close()
                    sqlSession.commit(true);
                }
                return result;
            } catch (Throwable t) {
                Throwable unwrapped = unwrapThrowable(t);
                if (CustomSqlSessionTemplate.this.exceptionTranslator != null && unwrapped instanceof PersistenceException) {
                    Throwable translated = CustomSqlSessionTemplate.this.exceptionTranslator
                        .translateExceptionIfPossible((PersistenceException) unwrapped);
                    if (translated != null) {
                        unwrapped = translated;
                    }
                }
                throw unwrapped;
            } finally {
                closeSqlSession(sqlSession, CustomSqlSessionTemplate.this.getSqlSessionFactory());
            }
        }
    }
}

12,至此,可由程序动态切换数据源,并实现分布式事务一致性已经实现。