@Autowired
private DataSourceTransactionManager dataSourceTransactionManager;
@Autowired
private TransactionDefinition transactionDefinition;
public void testSubmit(List<User> users) throws ExecutionException, InterruptedException {
//手动开启事务!
TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
try {
for (User user : users) {
if(user.getId() == 3){
throw new RuntimeException();
}
userMapper.insert(user);
}
// 手动提交
dataSourceTransactionManager.commit(transactionStatus);
}catch (Exception e){
System.err.println("error");
// 手动回滚
dataSourceTransactionManager.rollback(transactionStatus);
}
}