NHibernate Linq 使用隐式事务?
我正在使用 Ayende 的 NHibernate Linq 版本 2.1.2,可在 此处 获取,当我使用NProf 检查使用此方法的查询:
public IQueryable<T> GetAll()
{
return Session.Linq<T>();
}
它向我发出警告,表明我正在使用隐式事务。问题是,我在存储库中使用它来抽象数据库会话,但我仍然希望返回 IQueryable 的灵活性,以便我可以运行我想要的任何 Linq 查询。有没有办法将 Session.Linq
更多背景知识。我正在使用这样的方法:
var repo = new Repository();
var animals = repo.GetAll<Animal>().Where(x => x.Size > 100);
NoahsArk.LargeAnimals.AddRange(animals);
I'm using Ayende's NHibernate Linq version 2.1.2, available here, and when I use NHProf to inspect queries that use this method:
public IQueryable<T> GetAll()
{
return Session.Linq<T>();
}
It gives me the warning that I'm using an implicit transaction. Problem is, I'm using this in a repository to abstract out the database session, but I still want the flexibility of returning an IQueryable so I can run any Linq query I want. Is there a way to explicitly wrap the Session.Linq<T>()
in a transaction without exposing it, or should I just ignore the warning in this case?
A little more background. I'm using the method like so:
var repo = new Repository();
var animals = repo.GetAll<Animal>().Where(x => x.Size > 100);
NoahsArk.LargeAnimals.AddRange(animals);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NProf 的消息实际上与您的存储库实现无关。它只是指出您正在事务之外运行查询,这可能是问题的根源。
Ayende 在博客文章中解释了这一点:NH 教授警报:不鼓励使用隐式事务
您应该在应用程序中从更高级别管理您的事务。使用存储库时有多种方法可以实现此目的,请查看 unhaddins
NHProf's message is actually not related to your repository implementation. It's just pointing out that you are running your query outside a transaction, which can be a source of problems.
Ayende explains this in a blog post: NH Prof Alerts: Use of implicit transactions is discouraged
You should manage your transactions from a higher level in your application. There are several ways to do this while using repositories, have a look at unhaddins
我有一个非常相似的问题,我很简单地解决了。
我在我的存储库中创建了一个由 Linq 方法返回的 LinqClass,
然后我可以将 linq 语句包装在 using 块中
,即使从其中返回数据,仍然会调用事务提交。不再有隐式交易。显然这个例子是针对 NHibernate 的,但它对于其他东西也应该类似地工作。
I had a very similar problem which I solved quite simply.
I created a LinqClass within my repository returned by my Linq method
I could then wrap my linq statements up in a using block
and even if returning data from the middle of it, the transaction commit is still called. No more implicit transactions. Obviously this example is for NHibernate, but it should work similarly for other things.
我很确定您可以忽略此警告。
你能在NHProf中看到交易吗?
http://groups.google.com/group/nhprof/browse_thread/thread/ fbc97d3286ad783b
I'm pretty sure you can ingnore this warning.
Can you see the transaction in NHProf?
http://groups.google.com/group/nhprof/browse_thread/thread/fbc97d3286ad783b