I have coded with java, hibernate and spring. This error comes up when trying to add the @Scheduled
annotation. Without Annotate using @Scheduled
it works fine. what is the reason caused for this error?
Error as follows,
xyzLib: could not initialize proxy [ABC#8] - no Session
at xyzService.customMethod(xyzService.java:1392) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_291]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_291]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_291]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_291]
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.3.2.jar:5.3.2]
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.3.2.jar:5.3.2]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_291]
at java.util.concurrent.FutureTask.runAndReset$$$capture(FutureTask.java:308) [na:1.8.0_291]
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java) [na:1.8.0_291]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_291]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [na:1.8.0_291]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_291]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_291]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_291]
Hope someone can help me to understand what is happening.
CodePudding user response:
Without knowing the details you may need to add something like this above the method
@Transactional(propagation = Propagation.REQUIRES_NEW)
CodePudding user response:
This error means that we try to fetch a lazy-loaded object from the database by using a proxy object, but the Hibernate session is already closed.
I have fixed this error using FetchType.EAGER
strategy.
I use this strategy along with a @OneToMany annotation, for example :
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id")
private Set<Role> roles;
This is a sort of compromised answer for a precise utilization when we want to fetch the related series for most of our use cases.
So it is a lot simpler to declare the EAGER fetch kind as an alternative to explicitly fetching the series for most of the special enterprise flows.
more about this please refer to this link.