调用本类中的方法,事务回滚失败的问题。
错误调用方式:
@Service
public class BaseServiceImpl implements BaseService {
@Override
@Transactional
public void saveBaseInfo() {
BaseRequestBean baseRequestBean = new BaseRequestBean();
saveA(baseRequestBean);
saveB();
}
private void saveA(BaseRequestBean baseRequestBean){
baseDao.insert(baseRequestBean);
}
private void saveB(){
base222Dao.insert();
}
}
错误原因:
调用方法saveA 和 saveB 是通过 实例对象调用的,而非Spring代理的Bean。
正确调用方法:
@Service
public class BaseServiceImpl implements BaseService {
@Autowired
BaseServiceImpl baseService;
@Override
@Transactional
public void saveBaseInfo() {
BaseRequestBean baseRequestBean = new BaseRequestBean();
baseService.saveA(baseRequestBean);
baseService.saveB();
}
private void saveA(BaseRequestBean baseRequestBean){
baseDao.insert(baseRequestBean);
}
private void saveB(){
base222Dao.insert();
}
}
原因:
通过注入Bean调用,即可。