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

8
  • 2
    You were already told in your prior question 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 Commented Jul 27, 2016 at 1:21
  • Based on this: stackoverflow.com/questions/7359909/… "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 Commented Jul 27, 2016 at 1: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". Just accept it and make a nice cup of tea Commented Jul 27, 2016 at 1:39
  • so your telling me its a lost cause. NHibernate cannot do two sessions at the same time? Commented Jul 27, 2016 at 1:49
  • 2
    This is a duplicate of this stackoverflow.com/questions/37887107/… Commented Jul 27, 2016 at 2:47

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.