英文:
Spring boot Service Layer Tranction not working
问题
以下是翻译好的部分:
"i am trying to achieve spring boot Transaction service layer bit not able to achieve:"
"我正在尝试实现Spring Boot事务服务层,但未能实现:"
"Here is my code:"
"以下是我的代码:"
"i am expecting roll back if any error occuer from database or any code level"
"如果数据库或任何代码级别发生错误,我希望回滚事务。"
英文:
i am trying to achieve spring boot Transaction service layer bit not able to achieve:
Here is my code
@Service
public class EmpService {
@Autowired
EmployeeDao employeeDao;
@Autowired
EmployeeRepo empRepo;
@Autowired
EmployeeHealthInsuranceRepo healthRepo;
@Transactional
public void insertEmployee1(Employee employee,EmployeeHealthInsurance employeeHealthInsurance) {
empRepo.save(employee);
System.out.println(100/0);
healthRepo.save(employeeHealthInsurance);
}
public void insertEmployee(Employee employee,EmployeeHealthInsurance employeeHealthInsurance) {
insertEmployee1(employee,employeeHealthInsurance);
}
}
i am expecting roll back if any error occuer from database or any code level
答案1
得分: 1
问题出现在从insertEmployee(...)调用insertEmployee1(...)时。
@Transactional注解不会扩展到insertEmployee(...)方法。
尝试在insertEmployee(...)上方添加@Transactional以使其成为事务性方法。
希望对你有所帮助。
英文:
The issue happens when you call insertEmployee1(...) from insertEmployee(...)
The @Transactional annotation does not extend to the insertEmployee(...) method.
Try adding @Transactional above insertEmployee(...) for it to be transactional too.
Hope it helps.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论