NHibernate:Session.Save 和 Transaction.Commit [英] NHibernate: Session.Save and Transaction.Commit

查看:12
本文介绍了NHibernate:Session.Save 和 Transaction.Commit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Session.SaveTransaction.Commit 有区别吗?

我什么时候应该使用哪个?

When I should use which?

似乎有时Session.Save必须与Transaction.Commit结合使用,有时不需要.谁能告诉为什么会这样?

It seems that sometimes Session.Save must use in conjunction with Transaction.Commit, sometimes no. Can anyone tell why this is so?

推荐答案

他们是不同的——Session.Save 保存一个对象,Transaction.Commit 提交一堆工作(多次获取、加载、保存、更新等).

They're differenrt-- Session.Save saves an object and Transaction.Commit commits a bunch of work (multiple Gets, Loads, Saves, Updates, etc).

您会想要同时使用两者.这是一个带有链接的快速解释以供更多阅读.NHibernate 文档说明如下:

You'll want to use both. Here's a quick explanation with a link for more reading. The NHibernate documentation says the following:

ISession中,每个数据库操作都发生在一个事务中隔离数据库操作(甚至只读操作).

In an ISession, every database operation occurs inside a transaction that isolates the database operations (even read-only operations).

如果您没有明确定义您的事务,则每次您从数据库读取或写入数据库时​​都会隐式创建一个.效率不高.因此,即使您只是在阅读,您也会希望将所有内容放入一个事务中,并在完成后提交该事务.Ayende Rahien 在 这篇博文.

If you don't explicitly define your transaction, one will be created implicitly every time you read from or write to the database. Not very efficient. So even if you're just reading, you'll want to put everything inside a transaction and commit the transaction when you're done. Ayende Rahien explains further in this blog post.

当您查看一些代码示例时,似乎人们并未使用事务,但他们可能只是在您正在查看的代码之外开始/提交事务.例如,在我的 ASP.Net MVC 应用程序中,我使用操作过滤器 (TransactionAttribute) 来处理控制器操作之外的事务.

When you look at some code samples out there, it may seem like people aren't using transactions but they may just be beginning/committing the transaction outside of the code you're looking at. In my ASP.Net MVC app, for example, I use an action filter (TransactionAttribute) to handle the transaction outside of my Controller Actions.

这篇关于NHibernate:Session.Save 和 Transaction.Commit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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