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

查看:94
本文介绍了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进行大量工作(多个Get,Loads,Saves,Update等)

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天全站免登陆