NHibernate ISession Flush:何时何地使用它,为什么?
让我彻底困惑的事情之一是使用 session.Flush
与 session.Commit
和 session.Close
结合使用。
有时 session.Close
有效,例如,它提交我需要的所有更改。 我知道当我有一个事务或一个具有多个创建/更新/删除的工作单元时,我需要使用提交,以便在发生错误时我可以选择回滚。
但有时我真的会被 session.Flush
背后的逻辑所困扰。 我见过一些例子,其中有一个 session.SaveOrUpdate()
后跟一个刷新,但是当我删除 Flush 时它仍然可以正常工作。 有时,我在 Flush 语句上遇到错误,指出会话超时,删除它可以确保我没有遇到该错误。
有人对何时何地使用冲水有好的指导吗? 我已经查看了 NHibernate 文档,但仍然找不到简单的答案。
One of the things that get me thoroughly confused is the use of session.Flush
,in conjunction with session.Commit
, and session.Close
.
Sometimes session.Close
works, e.g., it commits all the changes that I need. I know I need to use commit when I have a transaction, or a unit of work with several creates/updates/deletes, so that I can choose to rollback if an error occurs.
But sometimes I really get stymied by the logic behind session.Flush
. I have seen examples where you have a session.SaveOrUpdate()
followed by a flush, but when I remove Flush it works fine anyway. Sometimes I run into errors on the Flush statement saying that the session timed out, and removing it made sure that I didn't run into that error.
Does anyone have a good guideline as to where or when to use a Flush? I've checked out the NHibernate documentation for this, but I still can't find a straightforward answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下是我的代码的两个示例,如果没有 session.Flush(),它将失败:
http://www.lucidcoding.blogspot.co.uk/2012/05/changing-type-of-entity-persistence.html
在此结束时,您可以看到一段代码,我在其中设置身份插入,保存实体然后刷新,然后设置身份插入关闭。 如果没有这种刷新,它似乎会设置身份插入打开和关闭,然后保存实体。
Flush() 的使用让我能够更好地控制正在发生的事情。
这是另一个示例:
在 TransactionScope 内发送 NServiceBus 消息
我不完全明白为什么这个,但是 Flush() 阻止了我的错误发生。
Here are two examples of my code where it would fail without session.Flush():
http://www.lucidcoding.blogspot.co.uk/2012/05/changing-type-of-entity-persistence.html
at the end of this, you can see a section of code where I set identity insert on, save the entity then flush, then set identity insert off. Without this flush it seemed to be setting identity insert on and off then saving the entity.
The use of Flush() gave me more control over what was going on.
Here is another example:
Sending NServiceBus message inside TransactionScope
I don't fully understand why on this one, but Flush() prevented my error from happening.
从 NHibernate 2.0 开始,数据库操作需要事务。 因此,
ITransaction.Commit()
调用将处理任何必要的刷新。 如果由于某种原因您没有使用 NHibernate 事务,那么将不会自动刷新会话。Starting in NHibernate 2.0, transactions are required for DB operations. Therefore, the
ITransaction.Commit()
call will handle any necessary flushing. If for some reason you aren't using NHibernate transactions, then there will be no auto-flushing of the session.ISession 有时会执行将 ADO.NET 连接的状态与内存中保存的对象的状态同步所需的 SQL 语句。
并且始终在提交更改后使用
,而不是将此更改保存到数据库中,我们使用 transaction.Commit();
From time to time the ISession will execute the SQL statements needed to synchronize the ADO.NET connection's state with the state of objects held in memory.
And always use
after the changes are committed than this changes to save into database we use transaction.Commit();
简而言之:
Close()
,而是将调用包装在using
语句或 manage 内的ISession
上你的 ISession 的生命周期在其他地方。来自文档:
...
另请参阅本节:
Briefly:
Close()
, instead wrap your calls on anISession
inside ausing
statement or manage the lifecycle of your ISession somewhere else.From the documentation:
...
Also refer to this section: