3

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?


nrk

nRk
  • 1,251
  • 7
  • 24
  • 50
  • Yes.. I am opening the connection. Actually i am doing a file import application, in that I am doing record insertion batch-wise (200) with session and commiting the session and those records are available in DB also,, but after 2 or 3 batches it's giving this exception. I am not able to figure it out. – nRk Mar 31 '10 at 12:28

3 Answers3

6

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
  • 12,471
  • 22
  • 98
  • 182
2

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
  • 31,444
  • 34
  • 152
  • 221
  • looks similar like that, but making cascading= none fixed the issue. – nRk May 13 '10 at 05:09
  • 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 May 13 '10 at 05:28
  • 1
    @vapcguy See above link – UpTheCreek Sep 16 '14 at 22:23
  • @UpTheCreek - Thanks -- in other words, ensure you're logging them yourself, using log4NET, or other. – vapcguy Sep 19 '14 at 21:50
0

We had this same issue. Changing the App Pool Managed Pipeline to Classic solved it for us.

vapcguy
  • 7,097
  • 1
  • 56
  • 52