Results 1 to 4 of 4

Thread: NHibernate + Spring.NET lazy loading failed - no session

  1. #1
    Join Date
    Jun 2012
    Posts
    2

    Default NHibernate + Spring.NET lazy loading failed - no session

    I use Spring.NET AOP for transaction and session management with NHibernate. When user makes several requests too quick - lazy loading is failed with exception "no session or session was closed".

    I use SpringSessionContext as CurrentSessionContext in NHibernate configuration
    Code:
    public class FluentSessionFactory : LocalSessionFactoryObject
    {
        protected override ISessionFactory NewSessionFactory(Configuration config)
        {
            var conf = Fluently
                .Configure()
                .Database(
                    MsSqlConfiguration
                        .MsSql2008
                        .ConnectionString(c => c.FromConnectionStringWithKey("MyConnection"))
                        // TODO: use ExposeConfiguration method
                        .CurrentSessionContext<SpringSessionContext>()
                    )
                .Mappings(
                    m => m.FluentMappings
                        .AddFromAssembly(this.GetType().Assembly)
                )
                .BuildSessionFactory();
            return conf;
        }
    }
    and OpenSessionInView module
    Code:
    <system.webServer>
        <validation validateIntegratedModeConfiguration="false"/>
        <modules runAllManagedModulesForAllRequests="true">
            <add name="OpenSessionInView"  type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate31"/>
        </modules>
    </system.webServer>
    Application implements next workflow for getting entities from db: View -> Controller -> Manager -> Repository and same to other side. So session is created per request, transaction - per call to manager.
    Code:
    <object id="TransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate31">
        <property name="DbProvider" ref="DbProvider"/>
        <property name="SessionFactory" ref="SessionFactory"/>
    </object>
    
    <tx:advice id="TxAdvice" transaction-manager="TransactionManager">
        <tx:attributes>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
    
    <object id="Pointcut" type="Spring.Aop.Support.SdkRegularExpressionMethodPointcut, Spring.Aop">
        <property name="patterns">
            <list>
                <value>MyAppication.Managers.AccountManager</value>
                <value>MyAppication.Managers.CompanyManager</value>
            </list>
        </property>
    </object>
    
    <aop:config>
        <aop:advisor advice-ref="TxAdvice" pointcut-ref="Pointcut"/>
    </aop:config>
    What are possible reasons of such behaviour and how can I solve this problem(Not.LazyLoad() and NHibernateUtil.Initialize() are not acceptable variants in my context)?

  2. #2
    Join Date
    Apr 2011
    Posts
    55

    Default

    Quote Originally Posted by lapagost View Post
    So session is created per request, transaction - per call to manager.
    What do you mean by this statement? You are using the OpenSessionInView module, which means that one hibernate session will be created for each http request.

    Why are you setting SpringSessionContext in addition to using the OpenSessionViewViewModule? I'm pretty sure this is not necessary. I've certainly never had to do it.

    Also, normally you would register the Spring.Context.Support.WebSupportModule http module to take care of setting the correct SessionContext for use with OpenSessionInView.

    HTH,
    Jordan.

  3. #3
    Join Date
    Jun 2012
    Posts
    2

    Default

    Thanks, it helped me a lot
    Register Spring.Context.Support.WebSupportModule in modules, are solved my problem in a moment
    Quote Originally Posted by gusgorman View Post
    What do you mean by this statement? You are using the OpenSessionInView module, which means that one hibernate session will be created for each http request.
    Yes session is created on the http request, and declarative transaction is when call manager

  4. #4
    Join Date
    Apr 2011
    Posts
    55

    Default

    Quote Originally Posted by lapagost View Post
    Thanks, it helped me a lot
    Register Spring.Context.Support.WebSupportModule in modules, are solved my problem in a moment
    cool

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •