springBoot的事物管理

时间:2021-04-30 10:54:11

springBoot的事物管理

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

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

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

标签:@Transactional

springBoot的事物管理

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

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

2.1 :新增配置文件信息:

pom.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-atomikos</artifactId>
</dependency>

2.2:新增配置文件信息

application.properties  :

#mysql--01
mysql.datasource.test.url = jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=utf-8
mysql.datasource.test.username = root
mysql.datasource.test.password = admin 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--02
mysql.datasource.test2.url =jdbc:mysql://localhost:3306/test02?useUnicode=true&characterEncoding=utf-8
mysql.datasource.test2.username =root
mysql.datasource.test2.password =admin 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

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;
} }

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);
}
}

2.4 :启动加载配置

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

springBoot的事物管理


项目结构如下:

springBoot的事物管理