|
|
Spring/Hibernate: Keep session open for thread, or open a new one?
Hi,
I am using Spring 3 together with Hibernate. I have a user request that starts a java thread in the spring controller and then a response is sent, so the thread might continue to work while the user from response has already been sent.
If I am working with lazy collections in my thread I get the following error:Code:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: my.model.MyClass.lazyCollection, no session or session was closed
I read I could use Hibernate.initialize(entity) to fully fetch the lazy collections but I cannot use this as I am filling the collection just in the thread. Furthermore I tried to use merge() but that still does not solve the problem.
Is there a way to manually keep the session open or to open a new one for the thread?
Thanks for your help!
Hello
I read I could use Hibernate.initialize(entity) to fully fetch the lazy collections
You should use that (initialize)
but I cannot use this as I am filling the collection just in the thread.
Can you post your DAO method?
I already solved the problem by autowiring the session factory, opening a session on my own and closing it on the end of the thread.
Thanks for your help, anyway! |
|