多线程入库事务回滚实现
本文基于CyclicBarrier
和 CompletableFuture
实现springboot
框架下的多线程事务管理。
内容阐述均基于springboot
框架
一、问题点
一般我们入库时都会基于 @Transactional
注解管理我们的事务,但是在多线程入库的场景下该注解不能生效,此时就需要我们手动的去处理事务了。
二、具体实现
1.全局线程池配置
#自定义线程池 (配置在application.yml中)
thread-pool:
# 核心线程数
core-size: 16
# 最大线程数
max-size: 32
# 队列容量
queue-capacity: 1000
# 线程池名称
name: v_eco_thread_pool
/**
* 多线程配置类 配置在 config 下
* 启动异步方法注解
* @author daniel
*/
@Data
@Configuration
@ConfigurationProperties(prefix = "thread-pool")
public class ThreadPoolConfig {
private Integer coreSize;
private Integer maxSize;
private Integer queueCapacity;
private String name;
@Bean(name = "myThreadPool")
public Executor myThreadPool() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(coreSize);
executor.setMaxPoolSize(maxSize)