NHibernate Linq 使用隐式事务?

发布于 2024-08-15 10:00:22 字数 623 浏览 5 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮倦 2024-08-22 10:00:22

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

沧桑㈠ 2024-08-22 10:00:22

我有一个非常相似的问题,我很简单地解决了。

我在我的存储库中创建了一个由 Linq 方法返回的 LinqClass,

public virtual LinqClass Linq()
{
    return new LinqClass(Session, LinqSource());
}

public class LinqClass : IDisposable
{
    public LinqClass(ISession session, IQueryable<T> linqSource)
    {
        _linq = linqSource;
        _transaction = session.BeginTransaction();
    }
    private readonly IQueryable<T> _linq;
    private readonly ITransaction _transaction;

    public IQueryable<T> Linq
    {
        get { return _linq; }
    }

    public void  Dispose()
    {
         _transaction.Commit();
    }
}

然后我可以将 linq 语句包装在 using 块中

    using (var linq = Linq())
    {
        var versions = from t in linq.Linq
                       where t.BaseName == BaseName
                       orderby t.Version descending
                       select t.Version;

        return versions.Take(1).SingleOrDefault();
    }

,即使从其中返回数据,仍然会调用事务提交。不再有隐式交易。显然这个例子是针对 NHibernate 的,但它对于其他东西也应该类似地工作。

I had a very similar problem which I solved quite simply.

I created a LinqClass within my repository returned by my Linq method

public virtual LinqClass Linq()
{
    return new LinqClass(Session, LinqSource());
}

public class LinqClass : IDisposable
{
    public LinqClass(ISession session, IQueryable<T> linqSource)
    {
        _linq = linqSource;
        _transaction = session.BeginTransaction();
    }
    private readonly IQueryable<T> _linq;
    private readonly ITransaction _transaction;

    public IQueryable<T> Linq
    {
        get { return _linq; }
    }

    public void  Dispose()
    {
         _transaction.Commit();
    }
}

I could then wrap my linq statements up in a using block

    using (var linq = Linq())
    {
        var versions = from t in linq.Linq
                       where t.BaseName == BaseName
                       orderby t.Version descending
                       select t.Version;

        return versions.Take(1).SingleOrDefault();
    }

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.

可是我不能没有你 2024-08-22 10:00:22

我很确定您可以忽略此警告。

你能在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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文