it3xl
it3xl

Reputation: 2672

NHibernate.LazyInitializationException: Initializing[]-Could not initialize proxy - no Session

ASP.NET site.
Constantly got the exception
LazyInitializationException: ... -Could not initialize proxy - no Session.

NHibernate.LazyInitializationException: Initializing[MyProj.MyClass#XXX]-Could not initialize proxy - no Session.   
   at NHibernate.Proxy.AbstractLazyInitializer.Initialize()
   at NHibernate.Proxy.DefaultLazyInitializer.Intercept(InvocationInfo info)
   at MyClassProxy.get_MyProjerty()

Upvotes: 0

Views: 874

Answers (1)

it3xl
it3xl

Reputation: 2672

  • Caching
  • Plus lazy loading
  • Plus session closing for every request

We've cached NHibernate entities but this is a dangerous idea to cache ORM objects.

We could do the following

  • Convert an ORM entity object to a custom limited POCO (Plain Old CLR Object) and use it for caching.
  • The following is dangerous for huge entity objects with Many-To-Many or One-To-Many relations.
    • Disable lazy loading for a single request (if possible).
    • Use some deep cloning to create a deep copy.
  • Detach the ORM entity object from the context. But lazy loading properties can stay having the null.

Keep in mind that somebody my enable or disable lazy loading and you'll get in troubles for a long time. Or someone may manage the session lifetime in an unexpected way. Or the session could become corrupted if a DB connection became interrupted.

I don't know yet what guys will choose to fix this issue.

Upvotes: 1

Related Questions