1. @Autowired
    2. private DataSourceTransactionManager dataSourceTransactionManager;
    3. @Autowired
    4. private TransactionDefinition transactionDefinition;
    5. public void testSubmit(List<User> users) throws ExecutionException, InterruptedException {
    6. //手动开启事务!
    7. TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
    8. try {
    9. for (User user : users) {
    10. if(user.getId() == 3){
    11. throw new RuntimeException();
    12. }
    13. userMapper.insert(user);
    14. }
    15. // 手动提交
    16. dataSourceTransactionManager.commit(transactionStatus);
    17. }catch (Exception e){
    18. System.err.println("error");
    19. // 手动回滚
    20. dataSourceTransactionManager.rollback(transactionStatus);
    21. }
    22. }