The db connection and the ISession are two different things. The db connection will be released back to the pool when there is no active transaction on the ISession. It will be reacquired when needed.
The ISession on the other hand stays around until you get rid of it. This is what matters for lazy initialization. So how are you handling your sessions? You don't say explicitly, but it seems that you first load three items in one session, close that, then later load the form in another session, then in your code "link" the items to the form(details) loaded in the second session. For this to work I can think of at least three solutions: you can either change to use one session, or you need to Attach the objects to the second session, or reload them in the second session. The first-level cache is owned by a single ISession, and is destroyed when that session is destroyed. A later session will not get object instances from another sessions level-1 cache. /Oskar 2010/10/18 MattO <[email protected]>: > I currently have an application running with nHibernate however I am > experiencing problems with lazily loaded collections. > > The issue I have is as follows: > > 1. I have an entity called "Item". I load several item objects in > from the database, it has a lazily loaded property called "ExamTypes" > on an item and it is a one to many associate (one item to multiple > exam types). > > 2. I have an entity called "Form", which has a one to many property > to "FormDetails" which then has a one to many associated to "Items". > All propertys are lazily loaded. > > The problem is in one procedure I load say 3 Item objects. This seems > to get stored in first level cache (I believe), I do not touch the > lazily loaded ExamTypes property. > > Then later on I load a "Form" object, which is associated to 3 > "FormDetail" objects and each one of these "FormDetail" objects is > associated to the same 3 "Item" objects I loaded previously. > > Now if I access my "Items" object like this: > Form.FormDetail.Item.ExamType I will get a > LazyInitializationException. This seems to be due to the fact that it > is not actually going to the database for these 3 "Item" entitys and > are just using the cached versions from the first time I loaded them > in a completely seperate procedure. > > I've solved this for now by Initializing the 3 item records ExamTypes > property the first time they are loaded. > > This increases program complexity though, because I'll have to always > remember when a particular entity is loaded first and ensure all > propertys are loaded by initializing the lazy propertys. So not only > do I have to know what all the lazy propertys are I have to remember > to initialize them. An alternative is to Evict them from the cache, > but then I loose the nice caching feature. > > How do people handle this? Why can't the session stay around with the > original 3 items records I loaded previously so that when I do > Form.FormDetail.Item.ExamType it won't throw the > LazyInitializationException?? > > Here is a snippet of the log: > > 2010-10-18 08:15:32,969 [4] DEBUG NHibernate.AdoNet.ConnectionManager > [(null)] - aggressively releasing database connection > 2010-10-18 08:15:32,969 [4] DEBUG > NHibernate.Connection.ConnectionProvider [(null)] - Closing connection > 2010-10-18 08:15:32,969 [4] DEBUG NHibernate.Loader.Loader [(null)] - > total objects hydrated: 0 > 2010-10-18 08:15:32,969 [4] DEBUG > NHibernate.Engine.StatefulPersistenceContext [(null)] - initializing > non-lazy collections > 2010-10-18 08:15:32,969 [4] DEBUG NHibernate.AdoNet.ConnectionManager > [(null)] - after autocommit > 2010-10-18 08:15:32,969 [4] DEBUG NHibernate.Impl.SessionImpl [(null)] > - transaction completion > 2010-10-18 08:15:32,969 [4] DEBUG NHibernate.AdoNet.ConnectionManager > [(null)] - aggressively releasing database connection > 2010-10-18 08:15:32,985 [4] ERROR > NHibernate.LazyInitializationException [(null)] - > Initializing[ItemBankCoreNH.BusinessObjects.Item#RAD]-failed to lazily > initialize a collection of role: > ItemBankCoreNH.BusinessObjects.Item.ExamTypes, no session or session > was closed > NHibernate.LazyInitializationException: > Initializing[ItemBankCoreNH.BusinessObjects.Item#RAD]-failed to lazily > initialize a collection of role: > ItemBankCoreNH.BusinessObjects.Item.ExamTypes, no session or session > was closed > 2010-10-18 08:15:33,250 [2] DEBUG NHibernate.Impl.SessionImpl [(null)] > - closing session > > -- > You received this message because you are subscribed to the Google Groups > "nhusers" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/nhusers?hl=en. > > -- You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
