springboot保证事务有效方法

1、首先肯定是springboot允许事务,在应用入口加上@EnableTransactionManagement;

2、@Transactional 注解只能应用到 public 的方法上;

3、不要在事务中显示捕获异常;

4、注解加上 rollback,@Transactional(rollbackFor = Exception.class),保证所有Exception异常都回滚;

5、不要直接调用同一个接口内的其他事务方法,否则失效。

@Service
public class AccountOperationManageServiceImpl implements AccountOperationManageService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    RoleUserMapper roleUserMapper;

    @Autowired
    AccountOperationManageService accountOperationManageService;

    @Override
    public void test1() throws Exception {
        this.test2();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void test2() throws Exception {
        RoleUser roleUser = new RoleUser();
        roleUser.setRole_id(100L);
        roleUser.setUid(97330202L);
        roleUser.setStatus(1);
        roleUserMapper.insert(roleUser);
        User user = new User();
        user.setMerchant_id(1000L);
        user.setUid(97330202L);
        user.setUsername("测试事务");
        user.setIs_super(1);
        userMapper.insert(user);
    }
}

具体可见: http://hbnnforever.cn/article/springboottransction.html

--------EOF---------
本文微信分享/扫码阅读