开发者

Sudden "failed to lazily initialize a collection of role...:no session or session was closed" - Exception

in an application that works for quite some time without any change I got the Exception

failed to lazily initialize a collection of role: ... , no session or session was closed

What could be the possible cause, all of a sudden? The method where this happens is tested and used successfully for the pas开发者_运维知识库t 3 years.

Thanks for the help, al

P.S. Environment: JSP, Spring, Hibernate, MySQL


If you pass an entity around and eventually leave the transaction boundary where it was read, you can't access any not already initialized association (I guess yours is something like Collection<Role> roles). If you then access roles Hibernate might try and intialize it, but since the transaction (and the Hibernate session with it) is closed already, you get that exception.

The method where this happens is tested and used successfully for the past 3 years.

This just might mean that the case of the collection not being initialized when that method is called is rare and did not happen in the last 3 years.


Try using Open Session In View setting in web.xml. It will help if you are trying to send hibernate objects to the spring MVC view and bringing it back.

Two options.

  • Load all lazy collections before you close the hibernate session or
  • Use OpenSession In View setting in web.xml
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜