当我尝试使用Hibernate从AsyncUncaughtExceptionHandler(如果@Async方法中有任何异常将被触发)执行数据库操作时,我面临以下问题。
代码段:
CustomAsyncUncaughtExceptionHandler:
public class CustomAsyncUncaughtExceptionHandler implements AsyncUncaughtExceptionHandler {
private final Log logger = LogFactory.getLog(CustomAsyncUncaughtExceptionHandler.class);
@Autowired
private AuditService auditService;
@Override
public void handleUncaughtException(final Throwable ex, final Method method, final Object... params) {
if (params[0] instanceof HttpServletRequest) {
final HttpServletRequest request = (HttpServletRequest) params[0];
this.auditService.auditAsyncException(ex.getMessage(), ex instanceof Exception
? (Exception) ex
: null, request.getRemoteUser(), request.getRequestURI(), this.getMapOfParameters(request));
}
else {
this.logger.error(String.format("Unexpected error occurred invoking async " + "method '%s'.", method), ex);
}
}
}
auditAsyncException:
@Service
public class AuditServiceImpl implements AuditService {
private static final Logger LOG =
LoggerFactory.getLogger(AuditServiceImpl.class);
@Autowired
private AuditDao auditDao;
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public ExceptionAudit auditAsyncException(final String message, final Exception ex, final String userid,
final String requestUri, final Map<String, String> parameters) {
LOG.debug("auditException");
ExceptionSeverity severtiy = ExceptionSeverity.HIGH;
final ExceptionAudit exceptionAudit =
new ExceptionAudit(message, ex, userid, requestUri, severtiy, parameters, null);
this.auditDao.saveAuditException(exceptionAudit);
LOG.error(message, ex);
return exceptionAudit;
}
}
错误消息:
ERROR [executor-1] o.s.s.a.AnnotationAsyncExecutionInterceptor.handleError(AsyncExecutionAspectSupport.java:300)| Exception handler for
async method 'public sendSMSOrScheduleDelivery(javax.servlet.http.HttpServletRequest,java.lang.Long,java.lang.String,java.lang.Long,java.util.List<java.lang.String>,java.lang.String)' threw unexpected exception itself
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
at org.springframework.orm.hibernate5.SpringSessionContext.currentSession(SpringSessionContext.java:133) ~[spring-orm-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:688) ~[hibernate-core-5.1.2.Final.jar:5.1.2.Final]
at .GeneralDaoImpl.getSession(GeneralDaoImpl.java:48) ~[classes/:na]
at .AuditDaoImpl.saveAuditException(AuditDaoImpl.java:43) ~[classes/:na]
at AuditServiceImpl.auditAsyncException(AuditServiceImpl.java:187) ~[classes/:na]
at CustomAsyncUncaughtExceptionHandler.handleUncaughtException(CustomAsyncUncaughtExceptionHandler.java:38) ~[classes/:na]
at org.springframework.aop.interceptor.AsyncExecutionAspectSupport.handleError(AsyncExecutionAspectSupport.java:297) ~[spring-aop-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:124) [spring-aop-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at controller.MappedDiagnosticContextThreadPoolTaskExecutor$1.call(MappedDiagnosticContextThreadPoolTaskExecutor.java:111) [classes/:na]
at java.util.concurrent.FutureTask.run(FutureTask.java:262) [na:1.7.0_80]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_80]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_80]
at java.lang.Thread.run(Thread.java:745) [na:1.7.0_80]
有指针吗?
答案 0 :(得分:0)
我建议遵循我的实施方式。
和
和
在AsyncUncaughtExceptionHandler的上下文中,无法连接到spring管理的bean /服务。不是我知道...