-2

I have a strange behaviour which I can't find any solution for days.. The behaviour I experience is the classic LazyInitializationException with no session or session was closed which is usual when there's no contextual session and trying to access lazy loaded properties/collections, but this in not my case because I have:

  • Session-per-request session bounded with Spring.NET OpenSessionInViewModule
  • A session opened and bounded to the context (web request)
  • Entities with collection as "list" (mapping list collection) with lazy="extra" or with "set" (mapping set collection) (The issue is caused mainly with lazy=extra..)
  • It seems that the Count statement for extra cound be done, but accessing the whole collection throws the exception

After investigating the usual issues:

  • no session opened

  • the object is not bounded to any session

  • a call to Session.Clear or Session.Evict was made (so any lazy for the evicted instance will work)

  • the Spring.NET WebSupportModule is not registered

    I found no explicit reasons of why this happen.. A strange behaviour is if I profile in NHprof, I see the exception in a new session, in other words, the contextual session works correctly, but when the exception is thrown, that exception is visualized in a secondary session.. but NO code open a session, and a single, contextual session is opened..

Anyone knows what other reasons could cause a situation like that? Thanks in advance

Community
  • 1
  • 1
Hoghweed
  • 1,938
  • 1
  • 16
  • 35
  • 1
    It would help if you posted some code samples of how this is all set up. – Trevor Pilley Dec 17 '12 at 10:21
  • A code sample is insignificant as I said in the question, the important is the description of the context. Every single part of the context/configuration is made as each product documentation best practice – Hoghweed Dec 17 '12 at 10:31
  • I am not 100% sure what your question is, however it may be worth knowing that `lazy="extra"` and `.count()` needs an open session as it performs a `select count(*)` rather than `select *` and an in memory count. – Rippo Dec 17 '12 at 10:54
  • Rippo thanks, but as I said in the "Context", I have a contextual session-per-request session already opened.. in any case thanks for the comment.. please, tell me what can I do to make my question clear.. What is missing? Thanks – Hoghweed Dec 17 '12 at 11:17
  • @Hoghweed you say you have a "contextual" session, whatever that means, but we don't have any code to analyze. Rippo is right; the session that was used to retrieve the entities is not open at a time it needs to, that's for sure. – Diego Mijelshon Dec 17 '12 at 12:16
  • @DiegoMijelshon you are wrong. First, the concept of "contextual" session is a base concept of NHibernate programming .. and you can see about it here http://nhforge.org/wikis/reference2-0en/context-sessions.aspx or in the official documentation (the link above is an extract of that documentation) second, I said the session is still opened, other nhibernate operations works correctly on that session bound to the "request context" as it name "session-per-request".. For that kind of issue a piece of code is ingnificant, I said that this happen on lazy="extra" collections.. – Hoghweed Dec 17 '12 at 12:25
  • 1
    So you are saying you 1) open a session, 2) do some work, 3) call Count (Exception happens in some strange second session) 4) close session. Are you sure 4 DOES not happen before 3? Please post a screen shot from NHProf showing this, ta – Rippo Dec 17 '12 at 13:28
  • I'm not explicitly doing nothing about session management because, as I said in question, all the session lifetime stuff is managed by the Spring.NET module OpenSessionInViewModule, which basically at request start open a session --> bind to the context class --> at the request close unbind the session from the context and close that session.. the NHprof screenshot is unusefull, because graphically seems that the issue is spawned on a second session, but all happen in the contextual session... any way thanks for the try – Hoghweed Dec 17 '12 at 14:01
  • Good luck, I wish you well. The problem is indeed a closed session, that is when you want to call `.Count` the session is closed. I am sorry you cannot see this. I take it the `.Count` is being used in the code behind and not ON the `view/presentation` screen itself. – Rippo Dec 17 '12 at 14:07
  • @Rippo the problem is not that, many times issues in nhibernate which seems to be a "classic" issues are not.. and notice, sometime this issue is caused because an object is take off (evicted) from a session, and even if the session is still opened, if you call some "lazies" you'll get the exception, but the session is opened! so the resolution is to reattach the object.. so, I'm sorry you cannot see that is not so simple to say "this is the problem and you cannot see this".. Here I ask if anyone knows other reasons, except the session closed, which may cause this issue! thanks – Hoghweed Dec 17 '12 at 14:23
  • 1
    I have been using NHibernate for 4+ years and answered over 300 questions here on S.O. and have to disagree, in my experience many times the issues ARE in fact classic ones. I wish you luck, it appears you are going to need it :) – Rippo Dec 17 '12 at 15:00
  • 1
    @Hoghweed I think I know what NH contextual sessions are, but I'm not sure *you* understand their implications. Furthermore, the fact that you're *"not explicitly doing nothing about session management"* doesn't mean you are free from understanding what is happening. Like Rippo, I've anwered not 300 but **over 1000** NH questions, and I'm 100% sure your session is being closed ahead of time. Blame Spring.Net if you want (or the way it's configured, actually), but the error *is* that. – Diego Mijelshon Dec 17 '12 at 16:10

1 Answers1

0

It's difficult to diagnose without seeing some code, but the problem sounds very similar to this. In the linked case OpenSessionInView was being used in addition to setting the session context in another location (similar to your first two bullet points) and WebSupportModule was not registered (as in your last point).

On the other hand, I know nothing about Spring.NET.

Jamie Ide
  • 48,427
  • 16
  • 81
  • 117