博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springBoot的事物管理
阅读量:5908 次
发布时间:2019-06-19

本文共 11854 字,大约阅读时间需要 39 分钟。

springBoot的事物管理

1:springBoot 整合单数据源事物:

 Spring Boot 使用事务非常简单,首先使用注解 @EnableTransactionManagement 开启事务支持后,然后在访问数据库的Service方法上添加注解 @Transactional 便可

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

 

标签:@Transactional  

 

 

2:springBoot 整合多数据源分布式事物:

使用springboot+jta+atomikos 分布式事物管理

 

2.1 :新增配置文件信息:

pom.xml

org.springframework.boot
spring-boot-starter-jta-atomikos

 

2.2:新增配置文件信息

application.properties  :

#mysql--01mysql.datasource.test.url = jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=utf-8mysql.datasource.test.username = rootmysql.datasource.test.password = adminmysql.datasource.test.minPoolSize = 3mysql.datasource.test.maxPoolSize = 25mysql.datasource.test.maxLifetime = 20000mysql.datasource.test.borrowConnectionTimeout = 30mysql.datasource.test.loginTimeout = 30mysql.datasource.test.maintenanceInterval = 60mysql.datasource.test.maxIdleTime = 60mysql.datasource.test.testQuery = select 1#mysql--02mysql.datasource.test2.url =jdbc:mysql://localhost:3306/test02?useUnicode=true&characterEncoding=utf-8mysql.datasource.test2.username =rootmysql.datasource.test2.password =adminmysql.datasource.test2.minPoolSize = 3mysql.datasource.test2.maxPoolSize = 25mysql.datasource.test2.maxLifetime = 20000mysql.datasource.test2.borrowConnectionTimeout = 30mysql.datasource.test2.loginTimeout = 30mysql.datasource.test2.maintenanceInterval = 60mysql.datasource.test2.maxIdleTime = 60mysql.datasource.test2.testQuery = select 1

 

2.3 读取配置文件信息:

 

DBConfig1 :

 

 

package com.dengwei.springdemo.dataSource;import org.springframework.boot.context.properties.ConfigurationProperties;@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;    }}

 

 

 

DBConfig2 :
package com.dengwei.springdemo.dataSource;import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix = "mysql.datasource.test2")public class DBConfig2 {    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;    }        }
View Code

 

2.4 创建多数据源

TestMyBatisConfig1  :
package com.dengwei.springdemo.dataSource;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// basePackages 最好分开配置 如果放在同一个文件夹可能会报错@MapperScan(basePackages = "com.dengwei.springdemo.test1", 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;    }    @Primary    @Bean(name = "testSqlSessionFactory")    public SqlSessionFactory testSqlSessionFactory(@Qualifier("testDataSource") DataSource dataSource)            throws Exception {        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();        bean.setDataSource(dataSource);        return bean.getObject();    }    @Primary    @Bean(name = "testSqlSessionTemplate")    public SqlSessionTemplate testSqlSessionTemplate(            @Qualifier("testSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {        return new SqlSessionTemplate(sqlSessionFactory);    }}

 

TestMyBatisConfig2  :
package com.dengwei.springdemo.dataSource;import com.atomikos.jdbc.AtomikosDataSourceBean;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.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;import java.sql.SQLException;// basePackages 最好分开配置 如果放在同一个文件夹可能会报错@Configuration@MapperScan(basePackages = "com.dengwei.springdemo.test2", sqlSessionTemplateRef = "test2SqlSessionTemplate")public class TestMyBatisConfig2 {    // 配置数据源    @Bean(name = "test2DataSource")    public DataSource testDataSource(DBConfig2 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("test2DataSource");        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 = "test2SqlSessionFactory")    public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource)            throws Exception {        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();        bean.setDataSource(dataSource);        return bean.getObject();    }    @Bean(name = "test2SqlSessionTemplate")    public SqlSessionTemplate testSqlSessionTemplate(            @Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {        return new SqlSessionTemplate(sqlSessionFactory);    }}
View Code

 

2.4 :启动加载配置

@EnableConfigurationProperties(value = { DBConfig1.class, DBConfig2.class })

 

 


 

 项目结构如下:

 

转载于:https://www.cnblogs.com/dw3306/p/9612701.html

你可能感兴趣的文章
IPSEC over GRE 同时NAT-T
查看>>
Linux mysql安装
查看>>
【 Makefile 编程基础之一】详细介绍Makefile概念和其机制用途;
查看>>
在Eclipse中启动Tomcat常见警告及开发期修改后台类代码即时生效方法
查看>>
Silverlight4 GDR3与Silverlight5 EAP1变化
查看>>
Mac outlook本地邮件保存在哪
查看>>
【嵌入式】使用minicom连接arm开发板错误
查看>>
MySQL 被ulimit 所困
查看>>
正则表达式 学习笔记1.3
查看>>
Support by Bouncy Castle
查看>>
Windows ThinPC 最终版已 RTM
查看>>
nmon性能监控
查看>>
SpringMVC懒加载导致的问题一则
查看>>
Tips of ACWS Framework
查看>>
HTTP handlers和Module简介
查看>>
新浪微博开放平台开发-android客户端(1)
查看>>
配置 Project Server 2010 与 Microsoft Exchange Server 2010 结合使用
查看>>
服务器架构之性能扩展-第七章(8)
查看>>
J2EE部署项目至Tomcat报错:Unable to read TLD "META-INF/c.tld"
查看>>
《跟阿铭学Linux》第6章 Linux磁盘管理——课后习题与答案
查看>>