Message "Session has already been garbage collected"

707 views
Skip to first unread message

David_L

unread,
Mar 14, 2018, 11:20:35 AM3/14/18
to nhusers
I upgraded my Project to NH5 last week. Now I have a question about a Message I have never seen before: "Session has already been garbage collected"

My code was Querying through lazy linked (many-to-one/one-to-many) Entities via LINQ. Everything worked without Exception in the past (with NH4). A customer reported me this message. I personally have never seen it...

StackTrace of my customer report:

Message:An error has occurred.,ExceptionMessage:Session has already been garbage collected,ExceptionType:System.InvalidOperationException,StackTrace: 
 bei NHibernate.Linq.DefaultQueryProvider.get_Session()
 bei NHibernate.Linq.DefaultQueryProvider.PrepareQuery(Expression expression, IQuery query)
 bei NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression)
 bei NHibernate.Linq.DefaultQueryProvider.Execute[TResult](Expression expression)
 bei Remotion.Linq.QueryableBase`1.GetEnumerator()
 bei System.Linq.Enumerable.<SelectManyIterator>d__16`2.MoveNext()
 bei System.Linq.Enumerable.<DistinctIterator>d__63`1.MoveNext()
 bei System.Linq.Buffer`1..ctor(IEnumerable`1 source)
 bei System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
 bei MediaServices.Services.Implementation.ServiceBaseWebApi.FillModelExtras(Asset[] assets)
 bei MediaServices.Services.Implementation.OfferService.OfferGalery(Int64 offerProgramId)

My code in OfferGalery loads some entiteis from database with a Session.Query<xxx>().Where... Statement. Then I Fill my Model (FillModelExtras) navigating through the loaded entities and lazy-sub-entities.

Under which conditions does this Message come up? And what can I do to avoid it?

frederic...@free.fr

unread,
Mar 17, 2018, 4:10:34 PM3/17/18
to nhusers
The NHibernate LINQ provider has a weak reference on the session, allowing the session to get garbaged collected if nothing else than the query provider references it.

If a Linq query is used after having let the session it is bound to go un-referenced, then this message may occur. This is a change in NHibernate 5.0: previously, the weak reference was "resurrecting" the session instead, but in an unpredictable state.
Reply all
Reply to author
Forward
0 new messages