我在从MySQL数据库加载数据时遇到了一个NHibernate问题。当我运行这段代码时(这是唯一一次创建NHibernate会话),它在第一次执行时抛出异常。例外情况是:
“已经有一个打开的DataReader与此连接相关联,必须先将其关闭。”
我不知道为什么会发生这种事?
// ----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();
}
}
发布于 2011-11-11 15:05:46
.Enumerable<>()
打开一个数据读取器,并使其保持打开状态,同时处理事务,尝试发送回滚。当datareader处于打开状态时,MySQL提供程序无法使用连接。
https://stackoverflow.com/questions/8093149
复制相似问题