我正在使用 Fluent NHibernate 作为我的 ORM。为此,我尝试使用 NHibernate LINQ 语法通过 LINQ 的强大功能来获取一组数据。我的代码可以正常工作并执行,但如果运行时间超过大约 30 秒,则会引发超时。我的问题是如何通过 NHibernate 延长 LINQ 语句的默认 30 秒超时?
我已经看过帖子了here https://stackoverflow.com/questions/12406717/linq-timeout-timeout-expired, here https://stackoverflow.com/questions/11821236/sql-timeout-expired, and here https://stackoverflow.com/questions/2519754/setting-linq-to-nhibernate-ado-command-timeout但是前两个是指设置 DataContext 的 Timeout 属性,这在这里不适用,第三个是指在 XML 中设置超时,这也不适用,因为我正在使用 Fluent NHibernate 动态生成 XML。不仅如此,该帖子已有 2 年历史,Fluent NHibernate 自此发生了变化。
使用 ICriteria 对象甚至 HQL,我可以指定超时,但这不是这里的目标。我想知道如何设置相同的超时并使用 LINQ。
示例代码:
using (var session = SessionFactory.OpenSession())
using (var transaction = session.BeginTransaction())
{
var query = (from mem in session.Query<Member>()
select mem);
query = query.Where({where statement});
int start = (currentPage - 1) * max);
if (start > 0)
query = query.Skip(start).Take(max);
else
query = query.Take(max);
var list = query.ToList();
transaction.Commit();
return list;
}
此代码(其中语句无关紧要)适用于除发生超时之外的所有用途。
任何帮助表示赞赏。提前致谢!
我最终为 Fluent NHibernate 配置设置了命令超时。这样做的缺点是它为我的所有数据访问调用设置了超时,而不仅仅是一个。
示例代码:
.ExposeConfiguration(c => c.SetProperty("command_timeout", (TimeSpan.FromMinutes(10).TotalSeconds).ToString()))
我发现这个建议来自this https://groups.google.com/forum/?fromgroups=#!topic/fluent-nhibernate/gi5O00uFYzE网站。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)