TaskUtils $ LoggingErrorHandler-计划任务发生意外错误。 java.lang.IllegalStateException:会话/实体管理器已关闭

时间:2019-01-31 08:43:47

标签: java spring

我正在编写一个由Spring Boot调度的功能,以某个固定的时间间隔运行它。

因为我正在调用一个sql过程来获取一些数据。但是我正在获取java.lang.IllegalStateException:Session / EntityManager已关闭

当我打印entityManager.isOpen()时,它正在打印真实值。

@PersistenceContext
private EntityManager entityManager;
@Scheduled(fixedRate = 5000)
public void syncAutofixNGAOverviewCache() {
    System.out.println("entityManager.isOpen() "+entityManager.isOpen());
    logger.info("syncing ignite cache with overview data");
    System.out.println("syncing ignite cache with overview data");
    StoredProcedureQuery query = entityManager.createStoredProcedureQuery("TODAY_OPEN_STATS")
            .registerStoredProcedureParameter(1, Class.class,ParameterMode.REF_CURSOR);
    query.execute();

        List<Object[]> postComments = query.getResultList();
        for (Object[] objects : postComments) {
            System.out.println(objects);
        }

}

我遇到的异常是

entityManager.isOpen() true
syncing ignite cache with overview data
query org.hibernate.procedure.internal.ProcedureCallImpl@32b64be5
Hibernate: {call TODAY_OPEN_STATS(?)}
[ERROR] 2019-01-31 13:43:45.080 [scheduling-1] TaskUtils$LoggingErrorHandler - Unexpected error occurred in scheduled task.
java.lang.IllegalStateException: Session/EntityManager is closed
    at org.hibernate.internal.AbstractSharedSessionContract.checkOpen(AbstractSharedSessionContract.java:357) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.engine.spi.SharedSessionContractImplementor.checkOpen(SharedSessionContractImplementor.java:138) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.query.internal.AbstractProducedQuery.getMaxResults(AbstractProducedQuery.java:892) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.procedure.internal.ProcedureCallImpl.getResultList(ProcedureCallImpl.java:716) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_192]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_192]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_192]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_192]
    at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:402) ~[spring-orm-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at com.sun.proxy.$Proxy114.getResultList(Unknown Source) ~[?:?]
    at com.bt.platform.dashboard.jobs.SyncAutofixNGAOverviewCacheJob.syncAutofixNGAOverviewCache(SyncAutofixNGAOverviewCacheJob.java:32) ~[classes/:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_192]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_192]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_192]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_192]
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) [spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_192]
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) [?:1.8.0_192]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [?:1.8.0_192]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [?:1.8.0_192]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_192]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_192]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_192]

0 个答案:

没有答案