c# - 是否有必要在交易中包装 nHibernate future ?

标签 c# .net nhibernate

是否有必要在事务中包装 nHibernate future ?

如:

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();
}

从这里开始:

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

我看不出这样做的理由。它是选择而不是插入或更新等。

最佳答案

当 NHibernate 查询未显式包装在事务中时,它被称为“隐式事务”,这是不鼓励的,并且具有各种含义,包括使二级缓存无效。这一切都在这里解释:

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

节选:

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.

关于c# - 是否有必要在交易中包装 nHibernate future ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43097763/

相关文章:

.net - 检测笔记本电脑盖子的关闭和打开

asp.net - 在单个内容页面中覆盖 ASP 母版页 html?

c# - 使用通配符解析规范路径

c# - 日期时间 + 计时器打勾?

c# - unity UxmlAttributeDescription 不设置值(重置值)

nhibernate - FluentNHibernate 多对一引用,其中外键不是主键,列名不同

ASP.NET MVC 图像上传存储位置(数据库与文件系统)

c# - .net framework 是否提供图像处理库?

C# - 将 Nullable<T> 公开给 COM 的好方法

.net - 避免序列化 REST 服务中的某些属性