asyncuncaughtexceptionhandler不支持实体管理器

klr1opcd  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(273)

我有一个简单的asyncuncaughtexceptionhandler impleStation和asyncconfig,它实现了asyncconfigurer。看起来是这样的:

@Configuration
@EnableAsync
@RequiredArgsConstructor
public class AsyncConfig implements AsyncConfigurer {

    private final AsyncUncaughtExceptionHandler asyncUncaughtExceptionHandler;
    private final ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Override
    public Executor getAsyncExecutor() {
        return threadPoolTaskExecutor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return asyncUncaughtExceptionHandler;
    }
}
public class AsyncExceptionHandler implements AsyncUncaughtExceptionHandler {

    private final WebSocketSender webSocketSender;

    @Override
    public void handleUncaughtException(Throwable throwable, Method method, Object... args) {
        //logs and other logic
        webSocketSender.send(
                    StompErrorMessage.errorBuilder()
                            //building error message
                            .build()
            );
    }
}

websocketsender的send()获取会话用户并向其发送通知,然后通过dao类保存usernotification。websocketsender是一个spring管理的bean。

@RequiredArgsConstructor
public class WebSocketSender {

    private final SimpMessagingTemplate simpMessagingTemplate;

    private final CoreSessionService sessionService;

    private final BaseDAO baseDAO;

    @Transactional
    public void send(StompMessage message) {
        User user = sessionService.getSessionUser();
        simpMessagingTemplate.convertAndSendToUser(user.getLogin(), "/queue/reply", message);
        UserNotification userNotification = new UserNotification();
        userNotification.setUser(user);
        userNotification.setNotification(message);
        baseDAO.save(userNotification);

    }

}

但它不起作用。出现“无支持的实体管理器”错误。

java.lang.IllegalStateException: No transactional EntityManager available
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:283)
    at com.sun.proxy.$Proxy227.unwrap(Unknown Source)
    at com.xx.model.core.dao.impl.JpaDaoImpl.findById(JpaDaoImpl.java:127)
    at com.xx.core.util.session.impl.SessionServiceImpl.getUserFromDetails(SessionServiceImpl.java:231)
    at com.xx.core.util.session.impl.SessionServiceImpl.getSessionUser(SessionServiceImpl.java:91)
    at com.xx.websocket.WebSocketSender.send(WebSocketSender.java:15)
    at com.xx.app.handler.AsyncExceptionHandler.handleUncaughtException(AsyncExceptionHandler.java:38)
    at org.springframework.aop.interceptor.AsyncExecutionAspectSupport.handleError(AsyncExecutionAspectSupport.java:314)
    at org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:124)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.springframework.security.concurrent.DelegatingSecurityContextRunnable.run(DelegatingSecurityContextRunnable.java:84)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

将@transactional注解放入asyncexceptionhandler或websocketsender没有帮助。websocketsender在其他地方工作正常(不是从asyncexceptionhandler发送的通知)。
启用事务管理。在这个项目中,交易在任何地方都可以正常进行。所以问题可能出在asyncconfig或asyncexceptionhandler本身(我的想法)。你有什么想法吗?
先谢谢你。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题