来自 AsyncUncaughtExceptionHandler(spring) 的休眠数据库操作
Posted
技术标签:
【中文标题】来自 AsyncUncaughtExceptionHandler(spring) 的休眠数据库操作【英文标题】:hibernate db operation from AsyncUncaughtExceptionHandler(spring) 【发布时间】:2018-12-28 03:59:57 【问题描述】:当我尝试使用 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]
任何指针?
【问题讨论】:
Caused by 不在您发布的日志中。添加它会很有帮助。 【参考方案1】:我建议按照我的实现。
https://github.com/IronJhon/system_bibliotec/blob/master/src/main/java/com/system/bibliotec/config/CustomAsyncExceptionHandler.java
和
https://github.com/IronJhon/system_bibliotec/blob/master/src/main/java/com/system/bibliotec/config/async/ExceptionHandlingAsyncTaskExecutor.java
和
https://github.com/IronJhon/system_bibliotec/blob/master/src/main/java/com/system/bibliotec/config/AsyncConfigurationBibliotecSystem.java
在 AsyncUncaughtExceptionHandler 的上下文中,无法连接到 spring 管理的 beans/服务。我不知道...
字体:Custom AsyncUncaughtExceptionHandler
【讨论】:
以上是关于来自 AsyncUncaughtExceptionHandler(spring) 的休眠数据库操作的主要内容,如果未能解决你的问题,请参考以下文章
为啥 WCF 服务能够处理来自不同进程的调用而不是来自线程的调用
来自 viewDidAppear 的 Segue 调用有效,但不是来自 viewWillAppear