Search code examples
c#nhibernateobjectdisposedexception

NHibernate Session ObjectDisposedException


Is there anyway to verify if a Session has been disposed of by NHibernate?

I have a wrapper class on Session that has it's own Finalizer and IDispoable implementation however if the Session gets disposed before I handle it myself in my class I end up receiving an ObjectDisposedException.

I really don't wish to wrap my clean up code with

try {
...
}
catch (ObjectDisposedException) { }

But I'm not really sure of any other way. The Session.IsOpen and Session.IsActive properties do not seem to offer any reliable information for me to acknowledge the session has been disposed of.

For full source you can view it on Assembla.


Solution

  • Ok, just taked a peek at your code. I don't know if this is exactly the issue, but you are calling End() from the conversation dispose method, which in turn tries to reconnect and disposes the session.. if you have eplicitly called End() before this you will get what you get, avoid that call. I think you shouldn't worry about rolling back the transaction before the session dispose as this is implicitly done. Just taken a quick look, but i think i really like your implementation.