Spring Boot服务层事务未生效。

huangapple go评论74阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年2月27日 16:01:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定