我在控制器中自动装配服务。在服务中,我有一个需要抛出异常和数据库更改的场景。所以,我尝试了@Async
@Transactional
public void verifyOtp(OtpDto otpDto)
...
if(xyz){
deactivateOtp(emp.getId());
throw new ServException("Mobile no requested is already assigned", "error-code");
}
}
@Async
@Transactional //with or without
public void deactivateOtp(Integer id){
otpRepo.deactivateOtp(id);
}
public interface OtpRepository extends JpaRepository<Otp, Integer> {
@Modifying
@Query("UPDATE Otp SET isActive = 0 WHERE id = :id")
public void deactiveOtp(@Param("id") Integer id);
这不是创建新线程。但是,如果我在回购中给出,它会起作用
public void deactivateOtp(Integer id){
otpRepo.deactivateOtp(id);
}
public interface OtpRepository extends JpaRepository<Otp, Integer> {
@Async
@Transactional
@Modifying
@Query("UPDATE Otp SET isActive = 0 WHERE id = :id")
public void deactiveOtp(@Param("id") Integer id);