We are using Spring.NET for Dependency Injection and NHibernate as ORM. We use Session Per Request Model and the session is created using Spring.NET. In one of the page, There is a webservice call. When the web service call throws EndPointNotFoundException exception, the NH Session gets closed and we get 'Lazy Initialization exception : 'failed to lazily initialize a collection of role: , no session or session was closed(We have a Person objects, that gets loaded in the page load, but the Person objects has set of lazily loaded collections like addresses etc. the error is thrown when one of the lazily loaded collection is accessed). If the exception is not thrown,everything works correctly. Is there a quick solution?
1 Answers
More information is required but i'll try to make a calculated guess:
The exception thrown is not handled, or if handled is re-thrown. The exception propagates to the page error handler.
Either way somewhere along the exception bubble pipeline an exception handler calls ISession.Close()
and then consumes the exception (and thus program execution continues via the rendering of the page)
This is not uncommon and usually when doing a rollback on the transaction it is advised to also close the ISession
.
Note, that a rollback+close may be performed on a failed commit, in which case the failed webservice call somehow pollutes your object state and thus the commit fails. Again the exception is handled and consumed.
The nitty gritty detail is that some code-path that contains proxied nhibernate entities still gets to execute and tries to reference the closed ISession