Получение IllegalStateException: Session / EntityManager закрывается для session.evict () - PullRequest
2 голосов
/ 03 октября 2019

Я получаю следующее исключение:

java.lang.IllegalStateException: Session/EntityManager is closed
    at org.hibernate.internal.AbstractSharedSessionContract.checkOpen(AbstractSharedSessionContract.java:357)
    at org.hibernate.engine.spi.SharedSessionContractImplementor.checkOpen(SharedSessionContractImplementor.java:138)
    at org.hibernate.internal.SessionImpl.fireEvict(SessionImpl.java:1395)
    at org.hibernate.internal.SessionImpl.evict(SessionImpl.java:1391)
    at com.hotstar.payment.service.PGPaymentService.deleteAndSave(PGPaymentService.java:259)
    at com.hotstar.payment.service.PGPaymentService.reconTransaction(PGPaymentService.java:224)
    at com.hotstar.payment.service.impl.PhonePeService.reconTransaction(PhonePeService.java:88)
    at com.hotstar.payment.service.impl.PhonePeService$$FastClassBySpringCGLIB$$312adb03.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
    at com.hotstar.payment.service.impl.PhonePeService$$EnhancerBySpringCGLIB$$f6a3598f.reconTransaction(<generated>)
    at com.hotstar.payment.schedulers.tasks.ReconTask.reconPendingTransactions(ReconTask.java:124)
    at com.hotstar.payment.schedulers.tasks.ReconTask$$FastClassBySpringCGLIB$$d6e2cee2.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88)
    at org.springframework.cloud.sleuth.instrument.scheduling.TraceSchedulingAspect.traceBackgroundThread(TraceSchedulingAspect.java:68)
    at sun.reflect.GeneratedMethodAccessor228.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633)
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
    at com.hotstar.payment.schedulers.tasks.ReconTask$$EnhancerBySpringCGLIB$$10269f04.reconPendingTransactions(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)
2019-10-03 12:32:00.741 ERROR [-,76fca589eb38a4c2,76fca589eb38a4c2,false] 1 --- [taskScheduler-4] c.h.payment.service.PGPaymentService     : STACKTRACE
java.lang.IllegalStateException: Session/EntityManager is closed
    at org.hibernate.internal.AbstractSharedSessionContract.checkOpen(AbstractSharedSessionContract.java:357)
    at org.hibernate.engine.spi.SharedSessionContractImplementor.checkOpen(SharedSessionContractImplementor.java:138)
    at org.hibernate.internal.SessionImpl.fireEvict(SessionImpl.java:1395)
    at org.hibernate.internal.SessionImpl.evict(SessionImpl.java:1391)
    at com.hotstar.payment.service.PGPaymentService.deleteAndSave(PGPaymentService.java:259)
    at com.hotstar.payment.service.PGPaymentService.reconTransaction(PGPaymentService.java:224)
    at com.hotstar.payment.service.impl.PhonePeService.reconTransaction(PhonePeService.java:88)
    at com.hotstar.payment.service.impl.PhonePeService$$FastClassBySpringCGLIB$$312adb03.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
    at com.hotstar.payment.service.impl.PhonePeService$$EnhancerBySpringCGLIB$$f6a3598f.reconTransaction(<generated>)
    at com.hotstar.payment.schedulers.tasks.ReconTask.reconPendingTransactions(ReconTask.java:124)
    at com.hotstar.payment.schedulers.tasks.ReconTask$$FastClassBySpringCGLIB$$d6e2cee2.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88)
    at org.springframework.cloud.sleuth.instrument.scheduling.TraceSchedulingAspect.traceBackgroundThread(TraceSchedulingAspect.java:68)
    at sun.reflect.GeneratedMethodAccessor228.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633)
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
    at com.hotstar.payment.schedulers.tasks.ReconTask$$EnhancerBySpringCGLIB$$10269f04.reconPendingTransactions(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)

Для следующего кода:

try {
    Session session = (Session) entityManager.getDelegate();
    session.evict(userSubscriptions);
    UserSubscriptions deleteObject = userSubscriptionsDao.findByHidAndCommercialPack(userSubscriptions.getHid(), userSubscriptions.getCommercialPack());

    if (deleteObject != null) {
        userSubscriptionsDao.delete(deleteObject);
        userSubscriptionsDao.flush();
    }

    userSubscriptionsDao.saveAndFlush(userSubscriptions.duplicate());
    return userSubscriptions;
} catch (Exception e) {
    ...
}

В основном я пытаюсь удалить объект перед его сохранением. Может быть несколько потоков, которые будут выполнять один и тот же код, но не в состоянии выяснить, что вызывает исключение IllegalStateException. Может кто-нибудь, пожалуйста, помогите мне с тем, что не так?

@Repository
public interface UserSubscriptionsDAO extends JpaRepository<UserSubscriptions, Long> {

1 Ответ

1 голос
/ 03 октября 2019

Проблема с

userSubscriptionsDao.saveAndFlush(userSubscriptions.duplicate());

Вы сохраняете отсоединенную сущность.

Вместо этого объединяет объект сущности UserSubscription. Это будет работать так, как вы ожидали.

...