从零开始学springboot-jpa-atomikos多数据源
前言
前章我们已经能够流畅的写出一个基于springboot2.1.3的多数据源的案例了,而且我们选择很多,可以通过jpa搭建,也可以通过jdbc。有了多数据源,必然会碰到多数据源事务处理的问题,也就是分布式事务,所以,这节,我们就通过jpa多数据源+atomikos的方式来实现分布式事务的处理案例。
Atomikos介绍
Atomikos 是一个为Java平台提供增值服务的并且开源类事务管理器。我们通过它来管理事务。springboot本身对其有很好的支持,依赖为spring-boot-starter-jta-atomikos。
创建空项目
1.png添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-atomikos</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
2.png
添加配置
application.yml:
spring:
datasource:
master:
username: root
password: 123456
url: jdbc:mysql://192.168.145.131:3306/test
driver-class-name: com.mysql.cj.jdbc.Driver
slave:
username: root
password: 123456
url: jdbc:mysql://192.168.145.131:3306/test2
driver-class-name: com.mysql.cj.jdbc.Driver
jta:
atomikos:
datasource:
max-pool-size: 20
borrow-connection-timeout: 60
connectionfactory:
max-pool-size: 20
borrow-connection-timeout: 60
jpa:
hibernate:
ddl-auto: update
show-sql: true
hibernate.properties(和application.yml同目录):
##坑,springboot2.1.3已经不支持网上的那些控制jpa建innodb表的配置了,必须创建这个文件配置。
hibernate.dialect.storage_engine=innodb
建库
创建test、test2库即可,本次我们通过jpa自动建表,运行代码时会动创建student和teacher表
也可手动创建
test:
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`age` int(11) NOT NULL,
`grade` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
test2:
CREATE TABLE `teacher` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`age` int(11) NOT NULL,
`course` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
完善
目录结构
3.png
根据目录结构,请自行创建package和class。
config/AtomikosJtaPlatform
package com.mrcoder.sbjpamultidbatomikos.config;
import org.hibernate.engine.transaction.jta.platform.internal.AbstractJtaPlatform;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
public class AtomikosJtaPlatform extends AbstractJtaPlatform {
private static final long serialVersionUID = 1L;
static TransactionManager transactionManager;
static UserTransaction transaction;
@Override
protected TransactionManager locateTransactionManager() {
return transactionManager;
}
@Override
protected UserTransaction locateUserTransaction() {
return transaction;
}
}
config/DataSourceConfig
package com.mrcoder.sbjpamultidbatomikos.config;
import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.icatch.jta.UserTransactionManager;
import org.hibernate.dialect.MySQL5Dialect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.jta.JtaTransactionManager;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
@Configuration
@ComponentScan
@EnableTransactionManagement
public class DataSourceConfig {
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
//设置JPA特性
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
//显示sql
hibernateJpaVendorAdapter.setShowSql(true);
//自动生成/更新表
hibernateJpaVendorAdapter.setGenerateDdl(true);
//设置数据库类型
hibernateJpaVendorAdapter.setDatabase(Database.MYSQL);
return hibernateJpaVendorAdapter;
}
@Bean(name = "userTransaction")
public UserTransaction userTransaction() throws Throwable {
UserTransactionImp userTransactionImp = new UserTransactionImp();
userTransactionImp.setTransactionTimeout(10000);
return userTransactionImp;
}
@Bean(name = "atomikosTransactionManager", initMethod = "init", destroyMethod = "close")
public TransactionManager atomikosTransactionManager() throws Throwable {
UserTransactionManager userTransactionManager = new UserTransactionManager();
userTransactionManager.setForceShutdown(false);
AtomikosJtaPlatform.transactionManager = userTransactionManager;
return userTransactionManager;
}
@Bean(name = "transactionManager")
@DependsOn({"userTransaction", "atomikosTransactionManager"})
public PlatformTransactionManager transactionManager() throws Throwable {
UserTransaction userTransaction = userTransaction();
AtomikosJtaPlatform.transaction = userTransaction;
TransactionManager atomikosTransactionManager = atomikosTransactionManager();
return new JtaTransactionManager(userTransaction, atomikosTransactionManager);
}
}
config/MasterConfig
package com.mrcoder.sbjpamultidbatomikos.config;
import com.mysql.cj.jdbc.MysqlXADataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
@Configuration
@DependsOn("transactionManager")
@EnableJpaRepositories(basePackages = "com.mrcoder.sbjpamultidbatomikos.entity.master", entityManagerFactoryRef = "masterEntityManager", transactionManagerRef = "transactionManager")
public class MasterConfig {
@Autowired
private JpaVendorAdapter jpaVendorAdapter;
//master库
@Primary
@Bean(name = "masterDataSourceProperties")
@Qualifier("masterDataSourceProperties")
@ConfigurationProperties(prefix = "spring.datasource.master")
public DataSourceProperties masterDataSourceProperties() {
return new DataSourceProperties();
}
@Primary
@Bean(name = "masterDataSource", initMethod = "init", destroyMethod = "close")
@ConfigurationProperties(prefix = "spring.datasource.master")
public DataSource masterDataSource() throws SQLException {
MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
mysqlXaDataSource.setUrl(masterDataSourceProperties().getUrl());
mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
mysqlXaDataSource.setPassword(masterDataSourceProperties().getPassword());
mysqlXaDataSource.setUser(masterDataSourceProperties().getUsername());
AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
xaDataSource.setXaDataSource(mysqlXaDataSource);
xaDataSource.setUniqueResourceName("xads1");
xaDataSource.setBorrowConnectionTimeout(60);
xaDataSource.setMaxPoolSize(20);
return xaDataSource;
}
@Primary
@Bean(name = "masterEntityManager")
@DependsOn("transactionManager")
public LocalContainerEntityManagerFactoryBean masterEntityManager() throws Throwable {
HashMap<String, Object> properties = new HashMap<String, Object>();
properties.put("hibernate.transaction.jta.platform", AtomikosJtaPlatform.class.getName());
properties.put("javax.persistence.transactionType", "JTA");
LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
entityManager.setJtaDataSource(masterDataSource());
entityManager.setJpaVendorAdapter(jpaVendorAdapter);
entityManager.setPackagesToScan("com.mrcoder.sbjpamultidbatomikos.entity.master");
entityManager.setPersistenceUnitName("masterPersistenceUnit");
entityManager.setJpaPropertyMap(properties);
return entityManager;
}
}
config/SlaveConfig
package com.mrcoder.sbjpamultidbatomikos.config;
import com.mysql.cj.jdbc.MysqlXADataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
@Configuration
@DependsOn("transactionManager")
@EnableJpaRepositories(basePackages = "com.mrcoder.sbjpamultidbatomikos.entity.slave", entityManagerFactoryRef = "slaveEntityManager", transactionManagerRef = "transactionManager")
public class SlaveConfig {
@Autowired
private JpaVendorAdapter jpaVendorAdapter;
@Bean(name = "slaveDataSourceProperties")
@Qualifier("slaveDataSourceProperties")
@ConfigurationProperties(prefix = "spring.datasource.slave")
public DataSourceProperties slaveDataSourceProperties() {
return new DataSourceProperties();
}
@Bean(name = "slaveDataSource", initMethod = "init", destroyMethod = "close")
@ConfigurationProperties(prefix = "spring.datasource.slave")
public DataSource slaveDataSource() throws SQLException {
MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
mysqlXaDataSource.setUrl(slaveDataSourceProperties().getUrl());
mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
mysqlXaDataSource.setPassword(slaveDataSourceProperties().getPassword());
mysqlXaDataSource.setUser(slaveDataSourceProperties().getUsername());
AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
xaDataSource.setXaDataSource(mysqlXaDataSource);
xaDataSource.setUniqueResourceName("xads2");
xaDataSource.setBorrowConnectionTimeout(60);
xaDataSource.setMaxPoolSize(20);
return xaDataSource;
}
@Bean(name = "slaveEntityManager")
@DependsOn("transactionManager")
public LocalContainerEntityManagerFactoryBean slaveEntityManager() throws Throwable {
HashMap<String, Object> properties = new HashMap<String, Object>();
properties.put("hibernate.transaction.jta.platform", AtomikosJtaPlatform.class.getName());
properties.put("javax.persistence.transactionType", "JTA");
LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
entityManager.setJtaDataSource(slaveDataSource());
entityManager.setJpaVendorAdapter(jpaVendorAdapter);
entityManager.setPackagesToScan("com.mrcoder.sbjpamultidbatomikos.entity.slave");
entityManager.setPersistenceUnitName("slavePersistenceUnit");
entityManager.setJpaPropertyMap(properties);
return entityManager;
}
}
entity/master/Student
package com.mrcoder.sbjpamultidbatomikos.entity.master;
import javax.persistence.*;
@Entity(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private int id;
private String name;
private int age;
private int grade;
public Student() {
}
public Student(String name, int age, int grade) {
this.name = name;
this.age = age;
this.grade = grade;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", grade=" + grade +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
entity/master/StudentDao
package com.mrcoder.sbjpamultidbatomikos.entity.master;
import org.springframework.data.jpa.repository.JpaRepository;
public interface StudentDao extends JpaRepository<Student, Integer> {
}
entity/slave/Teacher
package com.mrcoder.sbjpamultidbatomikos.entity.slave;
import javax.persistence.*;
@Entity(name = "teacher")
public class Teacher {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private int id;
private String name;
private int age;
private int course;
public Teacher() {
}
public Teacher(String name, int age, int course) {
this.name = name;
this.age = age;
this.course = course;
}
@Override
public String toString() {
return "Teacher{" +
"id=" + id +
", name='" + name + '\'' +
", age='" + age + '\'' +
", course='" + course + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getCourse() {
return course;
}
public void setCourse(int course) {
this.course = course;
}
}
entity/slave/TeacherDao
package com.mrcoder.sbjpamultidbatomikos.entity.slave;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TeacherDao extends JpaRepository<Teacher,Integer> {
}
service/CurdService
package com.mrcoder.sbjpamultidbatomikos.service;
import com.mrcoder.sbjpamultidbatomikos.entity.master.Student;
import com.mrcoder.sbjpamultidbatomikos.entity.master.StudentDao;
import com.mrcoder.sbjpamultidbatomikos.entity.slave.Teacher;
import com.mrcoder.sbjpamultidbatomikos.entity.slave.TeacherDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class CurdService {
@Autowired
private StudentDao studentDao;
@Autowired
private TeacherDao teacherDao;
@Transactional
public void add(int code) {
Student s1 = new Student();
s1.setAge(10);
s1.setGrade(10);
s1.setName("s1");
studentDao.save(s1);
Teacher t1 = new Teacher();
t1.setAge(10);
t1.setName("t1");
t1.setCourse(10);
teacherDao.save(t1);
int result = 1/code;
}
}
controller/JpaAtomikosController
package com.mrcoder.sbjpamultidbatomikos.controller;
import com.mrcoder.sbjpamultidbatomikos.entity.master.StudentDao;
import com.mrcoder.sbjpamultidbatomikos.service.CurdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class JpaAtomikosController {
@Autowired
private CurdService curdService;
@Autowired
private StudentDao studentDao;
@RequestMapping("/add")
public void add() {
curdService.add(1);
}
@RequestMapping("/test")
public void test() {
curdService.add(0);
}
@RequestMapping("/list")
public void list() {
System.out.println(studentDao.findAll());
}
}
运行
http://localhost:8080/add 会在test库的student和test2库的teacher表中各新增一条记录
http://localhost:8080/test 人为的制造1/0的异常,异常触发事务,会发现两张表都不会新增记录。
项目地址
https://github.com/MrCoderStack/SpringBootDemo/tree/master/sb-jpa-multidb-atomikos
https://gitee.com/MrCoderStack/SpringBootDemo/tree/master/sb-jpa-multidb-atomikos
注意点
请一定注意,两张表为innodb引擎,若出现分布式事务无法触发,请优先查看表引擎。