本文介绍了我应该始终使用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的工作,可以考虑尝试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.

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

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

08-16 03:31
查看更多