1 代码实现
1.1 正常情况
我们采用手动开启事务的方式
public void add(CountDownLatch countDownLatch) {
executor.submit(() -> {
TransactionStatus transaction = dataSourceTransactionManager.getTransaction(transactionDefinition);
try {
ThreadTest obj = new ThreadTest();
obj.setColumnName("threadTest");
log.info("开始插入");
threadTestMapper.insert(obj);
countDownLatch.countDown();
// throw new RuntimeException("故意抛出错误测试是否会进行回滚");
} catch (Exception e) {
log.info("出现异常了开始回滚");
dataSourceTransactionManager.rollback(transaction);
} finally {
dataSourceTransactionManager.commit(transaction);
}
});
}
insert方法中插入100条测试数据到数据库,通过手动开启事务的方式在每个子线程中开启各自的事务。注意注释的位置,首先测试没有错误的情况。
完整代码
@Service
@RequiredArgsConstructor
@Slf4j
public class ThreadTestServiceImplTwo extends ServiceImpl<ThreadTestMapper, ThreadTest>
implements ThreadTestService{
private final DataSourceTransactionManager dataSourceTransactionManager;
private final TransactionDefinition transactionDefinition;
private final ThreadTestMapper threadTestMapper;
private
static final ThreadPoolTaskExecutor executor;
static {
executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(15);
executor.setQueueCapacity(100);
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.setKeepAliveSeconds(60);
executor.initialize();
}
public void add(CountDownLatch countDownLatch) {
executor.submit(() -> {
TransactionStatus transaction = dataSourceTransactionManager.getTransaction(transactionDefinition);
try {
ThreadTest obj = new ThreadTest();
obj.setColumnName("threadTest");
log.info("开始插入");
threadTestMapper.insert(obj);
countDownLatch.countDown();
// throw new RuntimeException("故意抛出错误测试是否会进行回滚");
} catch (Exception e) {
log.info("出现异常了开始回滚");
dataSourceTransactionManager.rollback(transaction);
} finally {
dataSourceTransactionManager.commit(transaction);
}
});
}
public void insert() {
CountDownLatch countDownLatch = new CountDownLatch(100);
for (int i = 0; i < 100; i++) {
add(countDownLatch);
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
测试类中测试insert方法
@Test
void testInsert() {
threadTestService.insert();
}
结果
正常插入100条数据
1.2 异常情况
把1.1中的方法 // throw new RuntimeException("故意抛出错误测试是否会进行回滚");
取消注释即可,测试手动错误数据插入情况
public void insert() {
CountDownLatch countDownLatch = new CountDownLatch(100);
for (int i = 0; i < 100; i++) {
add(countDownLatch);
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
public void add(CountDownLatch countDownLatch) {
executor.submit(() -> {
TransactionStatus transaction = dataSourceTransactionManager.getTransaction(transactionDefinition);
try {
ThreadTest obj = new ThreadTest();
obj.setColumnName("threadTest");
log.info("开始插入");
threadTestMapper.insert(obj);
countDownLatch.countDown();
// 手动错误
throw new RuntimeException("故意抛出错误测试是否会进行回滚");
} catch (Exception e) {
log.info("出现异常了开始回滚");
dataSourceTransactionManager.rollback(transaction);
} finally {
dataSourceTransactionManager.commit(transaction);
}
});
}
结果
可以看到线程发生错误(截图为部分线程)开始回滚数据
数据库理所当然没有数据,完成了事务的控制
总结
难道我们真的控制了多线程的事务吗?
上面测试的仅仅是所有的线程各自发生了错误,并且各自线程完成了自己线程事务的回滚,假设某一个线程没有错误,某个线程有错误,如何回滚所有线程事务呢?是不是和分布式事务有点类似了呢?所以通过上述方法是没有办法处理部分错误部分正确的情况!