Rob Stevenson-Leggett
Rob Stevenson-Leggett

Reputation: 35679

MySql, Ninject and NHibernate - There is already an open DataReader associated with this Connection which must be closed first

I'm using a MySQL database which I am accessing with Fluent NHibernate and have wired up Session-Per-Request with Ninject like so:

        Kernel.Bind<ISession>()
            .ToMethod(context => Kernel.Get<ISessionFactory>().OpenSession())
            .InRequestScope();

I did have OnDeactivation(x=>x.Dispose()); but have been told it is not necessary as Ninject will call Dispose for you.

Everything works great except for a few times when I double click on a link that causes a read I get "There is already an open DataReader associated with this Connection which must be closed first."

I have read that I need to enable Multiple Active Record Sets (MARS) however I beleive this is not supported by MySQL.

Is there anything I can do to get rid of this error?

Upvotes: 2

Views: 1386

Answers (1)

Rob Stevenson-Leggett
Rob Stevenson-Leggett

Reputation: 35679

This was due to using NHibernate in multiple threads via a Parrell.ForEach.

So don't use a single session in multiple threads, basically!

Upvotes: 2

Related Questions