今天写代码的时候有一个service需要用到事务,故使用@Transactional注解
@Transactional
Map<String, Object> joinTeam(Long teamId, Long userId) throws Exception;
这里抛出自己定义的异常来实现事务回滚
接口实现类方法如下
public Map<String, Object> joinTeam(Long teamId, Long userId) throws Exception {
Map<String, Object> result = new HashMap<>();
Team team = teamService.getTeamById(teamId);
//添加组队成员
TeamMember member = new TeamMember();
member.setUserId(userId);
member.setTeamId(teamId);
teamMemberDao.saveTeamMember(member);
//更新组队人数
team.setCurrentSignup(team.getCurrentSignup() + 1);
Long count = teamService.updateTeamCurrentSignup(team);
int i = 0;
while (count == 0) {
if (i >= 3) {
throw new BaseException(BaseException.OPTIMISTIC_LOCK);
}
team = teamService.getTeamById(teamId);
team.setCurrentSignup(team.getCurrentSignup() + 1);
count = teamService.updateTeamCurrentSignup(team);
i++;
}
result.put("success", true);
result.put("message", "加入成功!");
throw new Exception(BaseException.OPTIMISTIC_LOCK);
}
teamMemberDao.saveTeamMember(member) 与 count = teamService.updateTeamCurrentSignup(team) 、两个修改库操作,需要 teamService.updateTeamCurrentSignup(team) 抛异常来控制 teamMemberDao.saveTeamMember(member) 的数据回滚
但是结尾抛异常数据并不回滚,很是糟心。
于是查看Spring的Transactional的API文档,发现下面这段:
If no rules are relevant to the exception, it will be treated like DefaultTransactionAttribute (rolling back on runtime exceptions).
所以Transactional默认异常回滚是runtimeexcetion才回滚
excetion是所有异常的总称。
而runtimeexcetion是具体的某一个异常。
所以得将Transactional设置回滚异常为excetion
故将接口修改如下,这次再抛自定义异常就会回滚了
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
Map<String, Object> joinTeam(Long teamId, Long userId) throws Exception;