简体   繁体   中英

NHibernate session closing unexpectedly

I am using a MVC4, C#, Castle Windsor, fluentnhibernate stack for my web application, which generally works very well.

Its only occasionally that I get an error related to nHibernate something along the lines of:

Invalid attempt to call Read when reader is closed.

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.

What I suspect the problem is, is with my NHibernate session management.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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