简体   繁体   English

NHibernate会话意外关闭

[英]NHibernate session closing unexpectedly

I am using a MVC4, C#, Castle Windsor, fluentnhibernate stack for my web application, which generally works very well. 我为我的Web应用程序使用了MVC4,C#,Castle Windsor,fluentnhibernate堆栈,通常效果很好。

Its only occasionally that I get an error related to nHibernate something along the lines of: 偶尔我会收到与nHibernate相关的错误,类似于:

Invalid attempt to call Read when reader is closed. 关闭阅读器后,尝试调用Read的尝试无效。

or 要么

Internal connection fatal error. 内部连接致命错误。

This usually rears its ugly head when I do multiple calls, in very close succession. 当我连续打多个电话时,这通常会抬起头来。

Currently I am replicating it while doing multiple ajax gets, from JQuery. 目前,我正在从JQuery执行多个ajax获取时复制它。

What I suspect the problem is, is with my NHibernate session management. 我怀疑问题出在我的NHibernate会话管理中。

The only thing I can think of is that the calls are someone using the same session, the first one completes and closes it, then the following call is unable to complete. 我唯一能想到的是,这些呼叫是使用同一会话的某人,第一个呼叫完成并关闭它,然后下一个呼叫无法完成。

This shouldn't be possible due to the way my sessions are handled like this: 由于我的会话是这样处理的,因此这不可能:

Kernel.Register(
                Component.For<ISessionFactory>()
                                .UsingFactoryMethod(_ => config.BuildSessionFactory()),
                Component.For<ISession>()
                                .UsingFactoryMethod(k => k.Resolve<ISessionFactory>().OpenSession())
                                .LifestylePerWebRequest());

Should be one session per request right? 每个请求应该一次会话吗?

How else, or where else can I look for the problem? 还有什么方法,或者在其他地方可以找到问题? I am quite stuck. 我很困。

Of course solved this a few minutes later, but the answer was an eye opener for me. 几分钟后当然解决了这个问题,但答案让我大开眼界。

This answer led me down the right path. 这个答案使我走上了正确的道路。

While all of my Repositories, Manager and other layers were installed with the correct lifestyle using Castle Windsor, there was one that wasn't. 虽然我所有的存储库,管理器和其他层都使用Castle Windsor进行了正确的生活方式安装,但其中没有一个。

I was doing a Repository call from an ActionFilter, all of my action filters are invoked through an ActionInvoker class which was registered incorrectly as Singleton, which resulted in my errors. 我正在从ActionFilter进行存储库调用,我所有的动作过滤器都是通过ActionInvoker类调用的,该类被错误地注册为Singleton,这导致了我的错误。

container.Register(Component.For<IActionInvoker>()
                                    .ImplementedBy<WindsorActionInvoker>()
                                    .LifeStyle.***Singleton***);

Should of course be 当然应该

container.Register(Component.For<IActionInvoker>()
                                    .ImplementedBy<WindsorActionInvoker>()
                                    .LifeStyle.Transient);

Just another reminder to pay closer attention to those Lifestyles. 另一个提醒是要更加注意那些生活方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM