Community blog feed

Castle ActiveRecord - Lazy Loading and session scopes in the web

Website
Blog
The Darkside
Posted
09 Oct 2008 at 22:00

Summary

This is the third post I'm doing related to a Castle ActiveRecord/NHibernate/lazy loading/sessionscope error that occurs. The first two are here and here. I received a mail the other day about an ActiveRecord/NHibernate error begin thrown in an application (web application, actually), which subseque

Post extract

This is the third post I'm doing related to a Castle ActiveRecord/NHibernate/lazy loading/sessionscope error that occurs. The first two are here and here.

I received a mail the other day about an ActiveRecord/NHibernate error begin thrown in an application (web application, actually), which subsequently turned out to be another instance of the "failed to lazily initialize a collection, no session or session was closed" error. While browsing through the ActiveRecord source code when trying to solve the previous sessionscope problems I'd had, I came across an HttpModule called "SessionScopeWebModule" in the Castle.ActiveRecord.Framework namespace, and so the lightbulb switched on. I did a quick search for more info on this module at castleproject.org and found this article

You simply add the module in as you'd do for any HttpModule, as follows:

<httpModules>
  <add name="ar.sessionscope" type="Castle.ActiveRecord.Framework.SessionScopeWebModule, Castle.ActiveRecord" />
</httpModules>

 When you initialise the ActiveRecord framework, however, you still get an error related to the fact that you don't have the "isWeb" attribute set to true. That's simply remedied by modifying the configuration section for ActiveRecord as follows:

<activerecord isWeb="true">
  <config>
    <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
    <add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect" />
    <add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
    <add key="connection.connection_string" value="Server=(local);initial catalog=myDatabase;Integrated Security=SSPI" />
    <add key="show_sql" value="false" />
  </config>
</activerecord>

Want to stay in touch with what's going on? Follow us on twitter!