Martijn de Munnik
Martijn de Munnik

Reputation: 974

NHibernate: There is already an open DataReader associated with this Connection which must be closed first

I'm having an issue with NHibernate loading data from a MySQL database. When I run this code (and this is the only time a NHibernate session is created) it throws an exception at the first }. The exception is:

"There is already an open DataReader associated with this Connection which must be closed first."

I don't know why this is happening?

   // ----snip----

        var sessionFactory = NHibernateSessionHelper.CreateSessionFactory();
        using (var session = sessionFactory.OpenSession())
        {
            using (session.BeginTransaction())
            {
                _timeRecords = session
                    .CreateQuery(
                        "select tr from TimeRecord as tr where tr.Billable = true and tr.InvoiceDate is null and tr.CheckedOn is not null")
                    .Enumerable<TimeRecord>();
            } // Exception is thrown here
        }

   // ----snip----

class NHibernateSessionHelper
{
    public static ISessionFactory CreateSessionFactory()
    {
        return Fluently.Configure()
            .Database(
                MySQLConfiguration
                    .Standard.ConnectionString(c => c
                                                        .Server("db01.redknot.nl")
                                                        .Database("todo_youngguns_nl")
                                                        .Username("youngguns.nl")
                                                        .Password(""))
                    .ShowSql()
            )
            .Mappings(m =>
                      m.FluentMappings.AddFromAssemblyOf<UserMap>())
            .BuildSessionFactory();
    }
}

Upvotes: 1

Views: 4479

Answers (1)

Firo
Firo

Reputation: 30813

.Enumerable<>() opens a Datareader and keeps it open while Disposing the Transaction tries to send a Rollback. MySQLs provider cant use the connection while there is the datareader open.

Upvotes: 1

Related Questions