简体   繁体   中英

Is it necessary to wrap nHibernate futures in a transaction?

Is it necessary to wrap nHibernate futures in a transaction?

Such as:

using (var s = sf.OpenSession())
using (var tx = s.BeginTransaction())
{
    var blogs = s.CreateCriteria<Blog>()
        .SetMaxResults(30)
        .List<Blog>();
    var countOfBlogs = s.CreateCriteria<Blog>()
        .SetProjection(Projections.Count(Projections.Id()))
        .UniqueResult<int>();

    Console.WriteLine("Number of blogs: {0}", countOfBlogs);
    foreach (var blog in blogs)
    {
        Console.WriteLine(blog.Title);
    }

    tx.Commit();
}

From here:

https://ayende.com/blog/3979/nhibernate-futures

I can't see a reason to do this. It's a select rather than an insert, or update etc.

When an NHibernate query is not explicitly wrapped in a transaction it is called an "implicit transaction" which is discouraged, and has various implications including nullifying the second level cache. This is all explained here:

https://hibernatingrhinos.com/Products/nhprof/learn#DoNotUseImplicitTransactions

An excerpt:

Even if we are only reading data, we should use a transaction, because using transactions ensures that we get consistent results from the database. NHibernate assumes that all access to the database is done under a transaction, and strongly discourages any use of the session without a transaction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM