-2

I have implemented NHibernate using NInject MVC5 in my MVC C# application. All works well but i realized that when i double click on my links i always get the error(s): {"There is already an open DataReader associated with this Connection which must be closed first."}

or

{"Session is closed!\r\nObject name: 'ISession'."}

Now i know this is because im binding my IContentService interface to ISession. I also know that ISession is not thread-safe and can only be used once. So my question is... then how am i supposed to create a session that can be used simultaniously InRequestScope across multiple threads. Because i know users will double click or users will click a link at the same time another user is clicking the same link and errors that stop multiple users is not really ideal.

I have this in my injections:

 Bind<IContentService>().To<ContentService>().InRequestScope();

            Bind<ISession>() //I'm sure the problem is right here, but not sure.
               .ToMethod(
                   context =>
                       context.Kernel.Get<IMasterSessionSource>().GetSession())
               .WhenInjectedInto<IContentService>()
               .InRequestScope();

My ContentService

public interface IContentService
    {
        IQueryable<Question> Questions{ get; }
    }


 public class ContentService : IContentService
    {
        private readonly ISession _session; //I think the problem might be here too...

        public ContentService(ISession session)
        {
            _session = session;
        }

        public IQueryable<Question> Questions
        {
            get { return _session.Query<Question>(); }
        }
    }

UPDATE

I read how i should use ISessionFactory for multithreading but i was unable to bind it like i can ISession.

Oskar Berggren
  • 5,583
  • 1
  • 19
  • 36
NeoSketo
  • 525
  • 1
  • 7
  • 26
  • 2
    You were already told in [your prior question](http://stackoverflow.com/questions/38596986/how-to-have-multiple-instances-of-a-shared-session-in-nhibernate-containing-diff) that NHibernate sessions are not thread-safe (which you admitted to knowing here and back then too). Asking the same question over and over is not going to change the Universe –  Jul 27 '16 at 01:21
  • Based on this: http://stackoverflow.com/questions/7359909/nhibernate-sessionfactory-thread-safe-issue "The session factory is threadsafe, the session is not." But the question is different than the last being that im asking HOW to make a mult-thread session – NeoSketo Jul 27 '16 at 01:27
  • Golly, just how is that link relevant? Do you think asking the question differently will result in a different outcome? _"[Sessions are not thread safe in NHibernate by design](http://stackoverflow.com/a/242983/585968)"_. Just accept it and make a nice cup of tea –  Jul 27 '16 at 01:39
  • so your telling me its a lost cause. NHibernate cannot do two sessions at the same time? – NeoSketo Jul 27 '16 at 01:49
  • NHibernate can handle _many_ sessions at the same time. However each session can only be accessed by a single thread. That however has nothing to do with your question above –  Jul 27 '16 at 02:03
  • im not sure how to ask the question then... Just trying to figure out how to do many sessions at the same time is all. – NeoSketo Jul 27 '16 at 02:19
  • 2
    This is a duplicate of this http://stackoverflow.com/questions/37887107/how-to-implement-session-per-request-pattern-in-asp-net-mvc-with-nhibernate/37887173#37887173 – Fran Jul 27 '16 at 02:47
  • You keep phrasing your question in contradictory terms - "how to multi-thread a session but I know it can't be done" makes no sense. It seems you know the rules so I'm assuming it's just confusing language. I think the question you want to ask is "How to correctly configure NInject MVC to handle NHibernate sessions per request?" Which makes it clear that you should apply the ninject tags to your question to get more relevant exposure. Alternatively use NHibernate's own contextual session support, but for that there are plenty of information on google - or ask about specific problems with that. – Oskar Berggren Jul 27 '16 at 22:59

1 Answers1

0

Turns out i was multi-threading correctly with sessions per request. the problem was NHibernate has a major issue with MySQL closing the session too soon. I appreciate the response. this definitely was a difficult issue to explain.

UPDATE

figured out the answer here: Session is closed Object name: 'ISession'. at NHibernate.Impl.AbstractSessionImpl.ErrorIfClosed() - How to stop the session from closing prematurely

Community
  • 1
  • 1
NeoSketo
  • 525
  • 1
  • 7
  • 26
  • unfortunately this is something i found on my own. In the logs i saw that it was creating multiple sessions. so i ended up having to cache the queries and make a make shift queue. Im just suprised i am the only one having this issue with MySQL and NHibernate. – NeoSketo Jul 28 '16 at 13:33
  • There is of course the possibility of bugs - are you using the latest version of the mysql connector? On the other hand, if it seems no one else is having the problem, that's an indication it really could be a problem somewhere in your code. I don't get exactly what you mean with "caching the queries and queue" etc, but it sounds to me like a huge workaround that you shouldn't really need to do. – Oskar Berggren Jul 28 '16 at 14:35
  • you are correct. They underlying issue is still there and i have to solve it :( It feels like I've tried everything and nothing is working. Do you know of a resource i can pay to help me with this? – NeoSketo Aug 08 '16 at 16:10