小编Chr*_*che的帖子

Spring Boot 在关闭时关闭休眠会话 - 在 @Async 方法完成之前

@Async我有一个 Spring Boot 应用程序的问题,该应用程序在任务(使用 EntityManager)完成之前关闭 EntityManager/会话。

有 2 个类与此问题相关:

调度程序

计划方法保留有限数量的作业,并调用执行实际工作的@Async方法。XYJobProcessor

@Component
public class XYJobProcessingTimer {

    private final XYJobService      xyJobService;
    private final XYJobProcessor    xyJobProcessor;

    //constructor skipped

    @Scheduled(initialDelayString = "${initial_delay}", fixedDelayString = "${delay}")
    public void performXYJobProcessing() {
        final String ticket = UUID.randomUUID().toString();
        final int reservedJobs = xyJobService.findAndReserveReadyXYJobs(ticket);

        if (reservedJobs > 0) {
            final Collection<XYJob> xyJobs = xyJobService.readReservedJobs(ticket);
            xyJobProcessor.process(xyJobs);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

异步处理器

@Async 注解的方法调用访问 EntityManager 的服务。

@Service
public class XYJobProcessor {

    private final XYJobService  xyJobService;

    // constructor …
Run Code Online (Sandbox Code Playgroud)

java spring asynchronous spring-data-jpa spring-boot

5
推荐指数
1
解决办法
2651
查看次数