提问人:user3479176 提问时间:11/9/2023 更新时间:11/9/2023 访问量:31
Spring Boot 异常处理程序不适用于 EventHandler TransactionPhase.AFTER_COMMIT
Spring Boot Exception Handler not working with EventHandler TransactionPhase.AFTER_COMMIT
问:
在Spring Boot中,当事件处理程序中抛出异常时,我的异常处理程序无法正确处理它 仅当事件处理程序被注释时才会发生这种情况
@TransactionPhase.AFTER_COMMIT
我的代码:
控制器
@PostMapping("/endpoint")
public ResponseEntity<?> doStuff() {
service.doStuff();
}
服务
@Transactional
public void doStuff() {
eventPublisher.publishEvent(new MyAwesomeEvent());
}
事件处理程序
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void handleAwesomeEvent(MyAwesomeEvent event) {
// Wont be caught !
throw new RuntimeException();
}
异常处理程序
@ExceptionHandler({Exception.class})
public ResponseEntity<Object> fallbackHandler(Exception ex, HttpServletRequest request) {
//Won't be called
}
有趣的是,当我将注释从
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
自
@TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT)
异常处理程序有效。
答: 暂无答案
评论
@Transactional(noRollbackFor = { EmailException.class })