我应该始终使用NHibernate的(即使是简单的读取和写入)的交易? [英] Should I always use transactions in nhibernate (even for simple reads and writes)?

查看:119
本文介绍了我应该始终使用NHibernate的(即使是简单的读取和写入)的交易?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道的多部分写道,我应该用交易NHibernate的。但是怎么样简单的读取和写入(1份)......我读过,这是很好的做法,始终使用事务。这是必需的?

我应该做到以下几点进行简单的读?或者我可以只下降了transcaction一部分的所有togather?

 公开的PrinterJob RetrievePrinterJobById(GUID ID)
{
    使用(ISession的会话= sessionFactory.OpenSession())
    {
        使用(ITransaction事务= session.BeginTransaction())
        {
            VAR printerJob2 =(的PrinterJob)session.Get(typeof运算(的PrinterJob),身份证);
            器transaction.commit();

            返回printerJob2;
        }
    }
}
 

 公开的PrinterJob RetrievePrinterJobById(GUID ID)
{
    使用(ISession的会话= sessionFactory.OpenSession())
    {
        返程(的PrinterJob)session.Get(typeof运算(的PrinterJob),身份证);
    }
}
 

那么简单写?

 公共无效AddPrintJob(的PrinterJob的PrinterJob)
{
    使用(ISession的会话= sessionFactory.OpenSession())
    {
        使用(ITransaction事务= session.BeginTransaction())
        {
            的session.save(的PrinterJob);
            器transaction.commit();
        }
    }
}
 

解决方案

最好的建议是始终使用事务。 这从NHProf 的文档链接,最好的解释了为什么。

  

当我们没有定义我们自己   交易,这又落入   隐性事务模式,在每一个   语句到数据库中运行其   自己的事务,从而导致较大的   性能成本(数据库时间   建立和拆卸交易),以及   减少的一致性。

     

即使我们只是读取数据,   应该使用一个事务,因为   使用事务确保我们得到   一致的结果从数据库。   NHibernate的假设所有访问   数据库被下一个进行   交易,并强烈反对   任何使用会话无   事务。

(顺便说一句,如果你是做严肃的NHibernate的工作,可以考虑尝试NHProf)。

I know that for multi part writes, I should be using transactions in nhibernate. However what about for simple read and writes (1 part) ... I've read that it's good practice to always use transactions. Is this required?

Should I do the following for a simple read ?? or can I just drop the transcaction part all togather ?

public PrinterJob RetrievePrinterJobById(Guid id)
{
    using (ISession session = sessionFactory.OpenSession())
    {
        using (ITransaction transaction = session.BeginTransaction())
        {
            var printerJob2 = (PrinterJob) session.Get(typeof (PrinterJob), id);
            transaction.Commit();

            return printerJob2;
        }
    }  
}

or

public PrinterJob RetrievePrinterJobById(Guid id)
{
    using (ISession session = sessionFactory.OpenSession())
    {
        return (PrinterJob) session.Get(typeof (PrinterJob), id);              
    }
}

What about for simple writes?

public void AddPrintJob(PrinterJob printerJob)
{
    using (ISession session = sessionFactory.OpenSession())
    {
        using (ITransaction transaction = session.BeginTransaction())
        {
            session.Save(printerJob);
            transaction.Commit();
        }
    }
}

解决方案

Best recommendation would be to always use a transaction. This link from the NHProf documentation, best explains why.

When we don't define our own transactions, it falls back into implicit transaction mode, where every statement to the database runs in its own transaction, resulting in a large performance cost (database time to build and tear down transactions), and reduced consistency.

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.

(BTW, if you are doing serious NHibernate work, consider trying out NHProf).

这篇关于我应该始终使用NHibernate的(即使是简单的读取和写入)的交易?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆