Springboot入门之分布式事务管理

Posted 牧野流冰87

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springboot入门之分布式事务管理相关的知识,希望对你有一定的参考价值。

springboot默认集成事务,只主要在方法上加上@Transactional即可。

分布式事务一种情况是针对多数据源,解决方案这里使用springboot+jta+atomikos来解决

一、pom文件

<groupId>cn.iponkan</groupId>
    <artifactId>springboot-jsp</artifactId>
    <version>1.0-SNAPSHOT</version>
 
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>
    <dependencies>
        <!-- SpringBoot 核心组件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jta-atomikos</artifactId>
        </dependency>
    </dependencies>
二、application.properties

# Mysql 1
mysql.datasource.test.url = jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=utf-8
mysql.datasource.test.username = root
mysql.datasource.test.password =
 
mysql.datasource.test.minPoolSize = 3
mysql.datasource.test.maxPoolSize = 25
mysql.datasource.test.maxLifetime = 20000
mysql.datasource.test.borrowConnectionTimeout = 30
mysql.datasource.test.loginTimeout = 30
mysql.datasource.test.maintenanceInterval = 60
mysql.datasource.test.maxIdleTime = 60
mysql.datasource.test.testQuery = select 1
 
 
# Mysql 2
mysql.datasource.test2.url =jdbc:mysql://localhost:3306/test02?useUnicode=true&characterEncoding=utf-8
mysql.datasource.test2.username =root
mysql.datasource.test2.password =
 
mysql.datasource.test2.minPoolSize = 3
mysql.datasource.test2.maxPoolSize = 25
mysql.datasource.test2.maxLifetime = 20000
mysql.datasource.test2.borrowConnectionTimeout = 30
mysql.datasource.test2.loginTimeout = 30
mysql.datasource.test2.maintenanceInterval = 60
mysql.datasource.test2.maxIdleTime = 60
mysql.datasource.test2.testQuery = select 1
三、读取配置文件,使用类(DBConfig2类似)

@ConfigurationProperties(prefix = "mysql.datasource.test")
public class DBConfig1 {
    private String url;
    private String username;
    private String password;
 
    private int minPoolSize;
 
    private int maxPoolSize;
 
    private int maxLifetime;
 
    private int borrowConnectionTimeout;
 
    private int loginTimeout;
 
    private int maintenanceInterval;
 
    private int maxIdleTime;
 
    private String testQuery;
 
    public String getUrl() {
        return url;
    }
 
    public void setUrl(String url) {
        this.url = url;
    }
 
    public String getUsername() {
        return username;
    }
 
    public void setUsername(String username) {
        this.username = username;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
 
    public int getMinPoolSize() {
        return minPoolSize;
    }
 
    public void setMinPoolSize(int minPoolSize) {
        this.minPoolSize = minPoolSize;
    }
 
    public int getMaxPoolSize() {
        return maxPoolSize;
    }
 
    public void setMaxPoolSize(int maxPoolSize) {
        this.maxPoolSize = maxPoolSize;
    }
 
    public int getMaxLifetime() {
        return maxLifetime;
    }
 
    public void setMaxLifetime(int maxLifetime) {
        this.maxLifetime = maxLifetime;
    }
 
    public int getBorrowConnectionTimeout() {
        return borrowConnectionTimeout;
    }
 
    public void setBorrowConnectionTimeout(int borrowConnectionTimeout) {
        this.borrowConnectionTimeout = borrowConnectionTimeout;
    }
 
    public int getLoginTimeout() {
        return loginTimeout;
    }
 
    public void setLoginTimeout(int loginTimeout) {
        this.loginTimeout = loginTimeout;
    }
 
    public int getMaintenanceInterval() {
        return maintenanceInterval;
    }
 
    public void setMaintenanceInterval(int maintenanceInterval) {
        this.maintenanceInterval = maintenanceInterval;
    }
 
    public int getMaxIdleTime() {
        return maxIdleTime;
    }
 
    public void setMaxIdleTime(int maxIdleTime) {
        this.maxIdleTime = maxIdleTime;
    }
 
    public String getTestQuery() {
        return testQuery;
    }
 
    public void setTestQuery(String testQuery) {
        this.testQuery = testQuery;
    }
}
四、创建多数据源(同理创建2)

@Configuration
@MapperScan(basePackages = "cn.iponkan.test01", sqlSessionTemplateRef = "testSqlSessionTemplate")
public class TestMyBatisConfig1 {
    // 配置数据源
    @Primary
    @Bean(name = "testDataSource")
    public DataSource testDataSource(DBConfig1 testConfig) throws SQLException {
        MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
        mysqlXaDataSource.setUrl(testConfig.getUrl());
        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
        mysqlXaDataSource.setPassword(testConfig.getPassword());
        mysqlXaDataSource.setUser(testConfig.getUsername());
        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
 
        AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
        xaDataSource.setXaDataSource(mysqlXaDataSource);
        xaDataSource.setUniqueResourceName("testDataSource");
 
        xaDataSource.setMinPoolSize(testConfig.getMinPoolSize());
        xaDataSource.setMaxPoolSize(testConfig.getMaxPoolSize());
        xaDataSource.setMaxLifetime(testConfig.getMaxLifetime());
        xaDataSource.setBorrowConnectionTimeout(testConfig.getBorrowConnectionTimeout());
        xaDataSource.setLoginTimeout(testConfig.getLoginTimeout());
        xaDataSource.setMaintenanceInterval(testConfig.getMaintenanceInterval());
        xaDataSource.setMaxIdleTime(testConfig.getMaxIdleTime());
        xaDataSource.setTestQuery(testConfig.getTestQuery());
        return xaDataSource;
    }
 
    @Bean(name = "testSqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("testDataSource") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }
 
    @Bean(name = "testSqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(
            @Qualifier("testSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
五、Controller类和启动类

@RestController
public class Indexcontroller {
    @Autowired
    private User1Mapper user1Mapper;
    @Autowired
    private User2Mapper user2Mapper;
 
 
    @RequestMapping("/add")
    @Transactional
    public String add() {
        user2Mapper.addUser("Clare", 22);
        int i = 1/0;
        user1Mapper.addUser("Tung", 22);
        return "成功!";
    }
}













































































































































































































































以上是关于Springboot入门之分布式事务管理的主要内容,如果未能解决你的问题,请参考以下文章

15《Spring Boot 入门教程》多数据源与分布式事务

Spring Cloud Alibaba | 微服务分布式事务之Seata

企业分布式微服务云SpringCloud SpringBoot mybatis (十七)Spring Boot中的事务管理

分布式事务数据最终一致性之serviceComb-Saga使用方法

springboot(整合事务和分布式事务)

java的框架spring如何配置分布式事务?