springboot分布式数据源(Mysql)

本文环境接上文多数据源配置的环境。

如果采用不同的数据源,当同时对不同的数据源进行操作时,事务无法正确的回滚,此时需要使用MysqlXADataSource来代理数据源。

MybatisDBD1Config.java:

package com.bxw.configuration;

import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * 分布式事务管理
 */
@Configuration
@MapperScan(basePackages = "com.bxw.mapperTA", sqlSessionTemplateRef = "sqlSessionTemplateT1")
public class MybatisDBD1Config {
    @Primary
    @Bean(name = "datasourceT1")
    public DataSource dataSource(DBConfig1 dbConfig1)throws SQLException{
        MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
        mysqlXaDataSource.setUrl(dbConfig1.getUrl());
        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
        mysqlXaDataSource.setPassword(dbConfig1.getPassword());
        mysqlXaDataSource.setUser(dbConfig1.getUsername());
        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);

        AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
        xaDataSource.setXaDataSource(mysqlXaDataSource);
        xaDataSource.setUniqueResourceName("dbb1");

//        xaDataSource.setMinPoolSize(dbConfig1.getMinPoolSize());
//        xaDataSource.setMaxPoolSize(dbConfig1.getMaxPoolSize());
//        xaDataSource.setMaxLifetime(dbConfig1.getMaxLifetime());
//        xaDataSource.setBorrowConnectionTimeout(dbConfig1.getBorrowConnectionTimeout());
//        xaDataSource.setLoginTimeout(dbConfig1.getLoginTimeout());
//        xaDataSource.setMaintenanceInterval(dbConfig1.getMaintenanceInterval());
//        xaDataSource.setMaxIdleTime(dbConfig1.getMaxIdleTime());
//        xaDataSource.setTestQuery(dbConfig1.getTestQuery());
        return xaDataSource;
    }

    @Bean(name = "sqlSessionFactoryT1")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("datasourceT1") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource); // 使用db1数据源, 连接hibernate库

        return factoryBean.getObject();

    }

    @Bean(name="sqlSessionTemplateT1")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactoryT1") SqlSessionFactory sqlSessionFactory) throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory); // 使用上面配置的Factory
        return template;
    }
}
MybatisDBD2Config.java:
package com.bxw.configuration;

import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * 分布式事务管理
 */
@Configuration
@MapperScan(basePackages = "com.bxw.mapperTB", sqlSessionTemplateRef = "sqlSessionTemplateT2")
public class MybatisDBD2Config {
    @Bean(name = "datasourceT2")
    public DataSource dataSource(DBConfig2 dbConfig2)throws SQLException{
        MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
        mysqlXaDataSource.setUrl(dbConfig2.getUrl());
        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
        mysqlXaDataSource.setPassword(dbConfig2.getPassword());
        mysqlXaDataSource.setUser(dbConfig2.getUsername());
        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);

        AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
        xaDataSource.setXaDataSource(mysqlXaDataSource);
        xaDataSource.setUniqueResourceName("dbb2");

//        xaDataSource.setMinPoolSize(dbConfig1.getMinPoolSize());
//        xaDataSource.setMaxPoolSize(dbConfig1.getMaxPoolSize());
//        xaDataSource.setMaxLifetime(dbConfig1.getMaxLifetime());
//        xaDataSource.setBorrowConnectionTimeout(dbConfig1.getBorrowConnectionTimeout());
//        xaDataSource.setLoginTimeout(dbConfig1.getLoginTimeout());
//        xaDataSource.setMaintenanceInterval(dbConfig1.getMaintenanceInterval());
//        xaDataSource.setMaxIdleTime(dbConfig1.getMaxIdleTime());
//        xaDataSource.setTestQuery(dbConfig1.getTestQuery());
        return xaDataSource;
    }

    @Bean(name = "sqlSessionFactoryT2")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("datasourceT2") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource); // 使用db1数据源, 连接hibernate库

        return factoryBean.getObject();

    }

    @Bean(name="sqlSessionTemplateT2")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactoryT2") SqlSessionFactory sqlSessionFactory) throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory); // 使用上面配置的Factory
        return template;
    }
}

创建sqlSessionFactory,SqlSessionTemplate

 

StudentService.java:

package com.bxw.service;

import com.bxw.annotation.DB;
import com.bxw.entity.Student;
import com.bxw.mapperTA.StudentMapperTA;
import com.bxw.mapperTB.StudentMapperTB;
import com.bxw.mapperDynamic.StudentMapperC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Service
public class StudentService {

    @Autowired
    private StudentMapperTA studentMapperTA;
    @Autowired
    private StudentMapperTB studentMapperTB;
/* 分布式事务
*/ @Transactional public boolean saveStudent(Student s){ studentMapperTA.addStudent(s); studentMapperTB.addStudent(s); int i = 1/0; return true; } }

 

posted on 2018-05-13 15:33 popcorn丫 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/popcornya/p/9032287.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值