NHibernate + Spring.NET延迟加载失败 - 没有会话 [英] NHibernate + Spring.NET lazy loading failed - no session

查看:144
本文介绍了NHibernate + Spring.NET延迟加载失败 - 没有会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Spring.NET AOP进行NHibernate事务和会话管理。当用户提出几个请求太快 - 懒惰加载失败,异常没有会议或会议被关闭。

我在NHibernate配置中使用SpringSessionContext作为CurrentSessionContext

  public class FluentSessionFactory :LocalSessionFactoryObject 
{
保护覆盖ISessionFactory NewSessionFactory(配置配置)
{
var conf =可以执行
.Configure()
.Database(
MsSqlConfiguration
.MsSql2008
.ConnectionString(c => c.FromConnectionStringWithKey(MyConnection))
// TODO:使用ExposeConfiguration方法
.CurrentSessionContext< SpringSessionContext>()
.Mappings(
m => m.FluentMappings
.AddFromAssembly(this.GetType()。Assembly)

.BuildSessionFactory() ;
返回conf;




$ b $在xml配置中:

 < object id =SessionFactorytype =IndustryTracker.NHibernateRepository.FluentSessionFactory,IndustryTracker.NHibernateRepository> 
< property name =DbProviderref =DbProvider/>
< / object>

和OpenSessionInView模块

 < system.webServer> 
< validation validateIntegratedModeConfiguration =false/>
< modules runAllManagedModulesForAllRequests =true>
< add name =OpenSessionInViewtype =Spring.Data.NHibernate.Support.OpenSessionInViewModule,Spring.Data.NHibernate31/>
< / modules>
< /system.webServer>


$ b $ p
$ b

应用程序实现下一个从db:View - > Controller - > Manager - > Repository获取实体的工作流,与其他方面一样。因此,会话创建每个请求,事务 - 每次调用管理器。

 < object id =TransactionManagertype =Spring .Data.NHibernate.HibernateTransactionManager,Spring.Data.NHibernate31> 
< property name =DbProviderref =DbProvider/>
< property name =SessionFactoryref =SessionFactory/>
< / object>

< tx:advice id =TxAdvicetransaction-manager =TransactionManager>
< tx:attributes>
< tx:method name =*/>
< / tx:属性>
< / tx:advice>

< object id =Pointcuttype =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:config>

这种行为有什么可能的原因,我怎样才能解决这个问题(Not.LazyLoad()和NHibernateUtil.Initialize()在我的上下文中是不可接受的变体)?

解决方案

会话工厂配置为 OpenSessionInViewModule



您可能忘记为会话工厂配置 OpenSessionInViewModule

 < appSettings> 
< add key =Spring.Data.NHibernate.Support.OpenSessionInViewModule.SessionFactoryObjectName
value =SessionFactory/>
< / appSettings>

必须在应用程式设定中完成。

。2。正确的FluentNHibernate为基础的春季会话工厂?



您似乎正在代码中配置您的会话工厂。您是否尝试过配置会话工厂,如文档中所述和BennyM ?你的 NewSessionFactory 方法从直接从Fluent NHibernate返回一个会话工厂,绕过了所有的spring.net支持。



3。您的会话工厂事务是否知晓?

 < object id =SessionFactory
type = IndustryTracker.NHibernateRepository.FluentSessionFactory,IndustryTracker.NHibernateRepository>
< property name =DbProviderref =DbProvider/>
<! - 提供了与Spring的声明式事务管理功能的集成 - >
< property name =ExposeTransactionAwareSessionFactoryvalue =true/>
< / object>

4。你的控制器是否依赖于 scope =application或者没有作用域定义?

一直在这里看错方向。如果您的控制器具有应用程序范围的依赖性,则意味着快速请求可能会受到干扰。默认值是scope =application;所以你想要检查没有范围定义的协作者。 .bringframework.net / doc-latest / reference / html / web.html#web-objectscoperel =nofollow> web范围


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

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;
    }
}

In xml config:

<object id="SessionFactory" type="IndustryTracker.NHibernateRepository.FluentSessionFactory, IndustryTracker.NHibernateRepository">
    <property name="DbProvider" ref="DbProvider" />
</object>

and OpenSessionInView module

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

<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)?

解决方案

1. Session factory configured for OpenSessionInViewModule?

You might have forgotten to configure the session factory for the OpenSessionInViewModule:

<appSettings>
  <add key="Spring.Data.NHibernate.Support.OpenSessionInViewModule.SessionFactoryObjectName" 
       value="SessionFactory"/>
</appSettings>

This has to be done in the app settings.

2. Correct FluentNHibernate based spring session factory?

You seem to be configuring your session factory in code. Have you tried configuring the session factory like described in the docs and on BennyM 's blog? Your NewSessionFactory method returns a session factory from straight from Fluent NHibernate, bypassing all spring.net support.

3. Is your session factory transaction aware?

<object id="SessionFactory" 
        type="IndustryTracker.NHibernateRepository.FluentSessionFactory, IndustryTracker.NHibernateRepository">
  <property name="DbProvider" ref="DbProvider" />
  <!-- provides integation with Spring's declarative transaction management features -->
  <property name="ExposeTransactionAwareSessionFactory" value="true" /> 
</object>

4. Does your controller have dependencies with scope="application" or no scope definition?

I might have been looking in the wrong direction here. If your controller has dependencies of application scope, it would mean that quick requests could interfere. The default is "scope="application"; so you'd want to check collaborators without scope definitions too. See the docs on web scopes.

这篇关于NHibernate + Spring.NET延迟加载失败 - 没有会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆