@Slf4j @Service public class MemberService { @Autowired private MemberMapper memberMapper; @Transactional public Integer insert(MemberEntity memberEntity) { Integer insertResult = 0; try { insertResult = memberMapper.save(memberEntity); log.info("insertResult:{}", insertResult); int result = 1 / memberEntity.getAge(); } catch (Exception e) { log.error("errorMsg:{}", e.getMessage()); //回滚 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); } return insertResult; } }
如上标红代码,如果不写的话,事务是不会回滚的。因为事务被 catch 捕获到了,所以只能手动的进行事务回滚。