views:

195

answers:

2

Hi,

I am getting the folloinwg error from NHibernate:

System.ObjectDisposedException: Session is closed! Object name: 'ISession'.
   at NHibernate.Impl.AbstractSessionImpl.ErrorIfClosed()
   at NHibernate.Impl.AbstractSessionImpl.CheckAndUpdateSessionStatus()
   at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
   at NHibernate.Impl.SessionImpl.Save(Object obj)

I am using NHibernate in .net windows service.
I am not able to trace the excact problem for the exception. This exception occurs very often.
Any one can help me on this to fix this exception?

Thanks
nrk

A: 

As the error says - it looks like you are trying to save an object when your ISession is closed. Are you sure you are opening it? Or perhaps it is being closed prematurely?

Update: Have you checked the NHibernate Logs?

UpTheCreek
looks similar like that, but making cascading= none fixed the issue.
nRk
Glad you got it working. If I were you though, I'd be sure to take a look at the nhibernate log4net logs, to maybe get some more info in whats going on, see: http://stackoverflow.com/questions/743323/nhibernate-enabling-log4net
UpTheCreek
+1  A: 

I am guessing you are wrapping your session in a using construct more than once, something like below. Can you post some of your session usage code?

HTH,
Berryl

Wrong - the session is closed after the first using construct:

using(var session = _sessionFactory.GetCurrentSession()
using(var tx = _session.BeginTransaction(){
    ... do something
    tx.Commit();
}


using(var session = _sessionFactory.GetCurrentSession()
using(var tx = _session.BeginTransaction(){
    ... do something else
}

Better- the session is closed after it's work is done

var session = _sessionFactory.GetCurrentSession();

using(var tx = _session.BeginTransaction(){
    ... do something
    tx.Commit();
}


using(var tx = _session.BeginTransaction(){
    ... do something else
    tx.Commit()
}
session.Close()
Berryl