开发者

Spring/Hibernate: Keep session open for thread, or open a new one?

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 http response has already been sent.

If I am working with lazy collections in my thread I get the following error:

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开发者_StackOverflow manually keep the session open or to open a new one for the thread?

Thanks for your help!


You can use Spring to inject the SessionFactory into your Runnable, open a Session from it, and manage the lifetime of the Session and Transaction using vanilla Hibernate APIs without relying on the session management magic of Spring.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜