Well it may be because I'm copying my object to a dictionary and then
later accessing one of the lazy propertys.  Would this cause it?

Here is the code:

Dictionary<int, ItemBankCoreNH.BusinessObjects.Form> de = new
Dictionary<int, ItemBankCoreNH.BusinessObjects.Form>();

The above line just declares my business object dictionary which will
contain "Form" entitys that I get from nHibernate.

ItemBankCoreNH.BusinessObjects.Form frm =
formManager.GetById(Convert.ToInt32(listItem.Value));

The above line is where I get the actual form object.

I now have what I need (but have not accessed any of the lazy
propertys of the frm (Form) object yet).

I now add it to the dictionary.

de.Add(frm.Id, frm);

Now a lot of other code processes, but later on in the code (about 50
lines later), I access the property and I get the
LazyInitializationException.

Does copying it into the dictionary somehow disconnect it from the
session?

For now I've created a basic function as follows and am initializing
all Lazy propertys ahead of time (before adding to the dictionary) and
I think it might solve it (but haven't tested it yet).

Here is the code: formManager.InitializeLazyPropertys(frm);

Here is the interface:

void InitializeLazyPropertys(params T[] propertys); //Added a simple
mechanism for initializing lazy propertys at runtime

And the implementation:

        public void InitializeLazyPropertys(params T[] propertys)
        {
            foreach (T prop in propertys)
                NHibernateUtil.Initialize(prop);
        }

On Sep 28, 9:24 am, Oskar Berggren <[email protected]> wrote:
> The following applies since at least NH 2.0:
> The fact that the ISession releases the connection is not the cause of
> your problem.
> If NHibernate is managing the database connections by itself, the
> ISession will release the underlying ado.net connection whenever there
> is no active transaction. This is to avoid holding connections that
> isn't used. A connection will be retrieved again when needed.
>
> Your real problem is hidden in your code somewhere.
>
> /Oskar
>
> 2010/9/28 MattO <[email protected]>:
>
>
>
> > I use session per request, therefore the session should stay open for
> > the duration of my ASP .NET request, but it does not seem to be doing
> > that.
>
> > Instead nHibernate seems to be closing the connection and I'm not sure
> > why.
>
> > Here is the important parts of the log:
>
> > 2010-09-28 08:47:20,536 [12] INFO  NHibernate.Loader.Loader [(null)] -
> > SELECT COUNT(*) AS Count FROM FormDetail INNER JOIN Items ON
> > FormDetail.RecordID = Items.RecordID WHERE (Items.QTypeID = @p0) and
> > form...@p1
> > 2010-09-28 08:47:20,551 [12] DEBUG
> > NHibernate.Connection.DriverConnectionProvider [(null)] - Obtaining
> > IDbConnection from Driver
> > 2010-09-28 08:47:20,551 [12] DEBUG NHibernate.AdoNet.AbstractBatcher
> > [(null)] - ExecuteReader took 3 ms
> > 2010-09-28 08:47:20,551 [12] DEBUG NHibernate.AdoNet.AbstractBatcher
> > [(null)] - Opened IDataReader, open IDataReaders: 1
> > 2010-09-28 08:47:20,551 [12] DEBUG NHibernate.Loader.Loader [(null)] -
> > processing result set
> > 2010-09-28 08:47:20,551 [12] DEBUG NHibernate.Loader.Loader [(null)] -
> > result set row: 0
> > 2010-09-28 08:47:20,551 [12] DEBUG NHibernate.Loader.Loader [(null)] -
> > result row:
> > 2010-09-28 08:47:20,551 [12] DEBUG NHibernate.Type.Int32Type [(null)]
> > - returning '0' as column: Count
> > 2010-09-28 08:47:20,551 [12] DEBUG NHibernate.Loader.Loader [(null)] -
> > done processing result set (1 rows)
> > 2010-09-28 08:47:20,551 [12] DEBUG NHibernate.Driver.NHybridDataReader
> > [(null)] - running NHybridDataReader.Dispose()
> > 2010-09-28 08:47:20,551 [12] DEBUG NHibernate.AdoNet.AbstractBatcher
> > [(null)] - Closed IDataReader, open IDataReaders :0
> > 2010-09-28 08:47:20,551 [12] DEBUG NHibernate.AdoNet.AbstractBatcher
> > [(null)] - DataReader was closed after 3 ms
> > 2010-09-28 08:47:20,551 [12] DEBUG NHibernate.AdoNet.AbstractBatcher
> > [(null)] - Closed IDbCommand, open IDbCommands: 0
> > 2010-09-28 08:47:20,551 [12] DEBUG NHibernate.AdoNet.ConnectionManager
> > [(null)] - aggressively releasing database connection
> > 2010-09-28 08:47:20,551 [12] DEBUG
> > NHibernate.Connection.ConnectionProvider [(null)] - Closing connection
> > 2010-09-28 08:47:20,551 [12] DEBUG
> > NHibernate.Engine.StatefulPersistenceContext [(null)] - initializing
> > non-lazy collections
> > 2010-09-28 08:47:20,551 [12] DEBUG NHibernate.AdoNet.ConnectionManager
> > [(null)] - after autocommit
> > 2010-09-28 08:47:20,551 [12] DEBUG NHibernate.Impl.SessionImpl
> > [(null)] - transaction completion
> > 2010-09-28 08:47:20,551 [12] DEBUG NHibernate.AdoNet.ConnectionManager
> > [(null)] - aggressively releasing database connection
> > 2010-09-28 08:47:22,829 [12] ERROR
> > NHibernate.LazyInitializationException [(null)] -
> > Initializing[ItemBankCoreNH.BusinessObjects.Form#BS]-failed to lazily
> > initialize a collection of role:
> > ItemBankCoreNH.BusinessObjects.Form.ExamTypes, no session or session
> > was closed
> > NHibernate.LazyInitializationException:
> > Initializing[ItemBankCoreNH.BusinessObjects.Form#BS]-failed to lazily
> > initialize a collection of role:
> > ItemBankCoreNH.BusinessObjects.Form.ExamTypes, no session or session
> > was closed
>
> > --
> > 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 
> > athttp://groups.google.com/group/nhusers?hl=en.- Hide quoted text -
>
> - Show quoted text -

-- 
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.

Reply via email to