使用ITransactions时是否必须设置FlushMode.Commit?
Is it nessecary to set FlushMode.Commit when using ITransactions?
我已经为使用 Web 和桌面应用程序构建了一个 NHibernate 基础结构。
但是,我想知道是否有必要在使用事务时使用FlushMode.Commit。
如果我在获取或创建 ISession 时使用默认 FlushMode.Auto 会有问题吗?
我已阅读以下内容:"Set the session's flush mode to Commit to avoid unnecessary trips to the database when using transactions."。
但是,我希望有人向我保证,就功能而言,FlushMode.Auto 也能胜任。
这只是避免不必要的数据库访问的性能问题吗?
我可能会忍受它。
请给我你的光!
TL;DR
不,没有必要改变刷新模式。如果不确定,请将其保留为 Auto
。您将避免错误。
完整解释
默认冲洗模式 Auto
的工作方式与描述的一样 here:
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. This process, flush, occurs by default at the
following points
- from some invocations of Find() or Enumerable()
- from NHibernate.ITransaction.Commit()
- from ISession.Flush()
此逻辑是为了确保查询数据库不会 return 陈旧数据,关于对当前事务所做的事情。
Except when you explicity Flush(), there are absolutely no guarantees
about when the Session executes the ADO.NET calls, only the order in
which they are executed. However, NHibernate does guarantee that the
ISession.CreateQuery(..) methods will never return stale data; nor
will they return the wrong data.
更改默认的刷新模式可能会导致您在事务中查询 return 数据而不考虑之前在同一事务中所做的事情。
除非您非常确定您不会被由于陈旧数据引起的任何错误所困扰 return 由同一事务中的后续查询编辑,否则最好将刷新模式保留为 Auto
。
更改刷新模式以限制事务内的 SQL 往返需要检查所有当前事务并在需要考虑之前的查询之前添加对 Flush
的显式调用在他们的交易中完成。然后希望有什么新的发展也不要忘记做。因此,您不仅不一定会节省很多 SQL 往返,而且还会为错误创造新的机会。
我个人认为不值得更改默认刷新模式以减少 SQL 往返次数。特别是如果你不检查它在你的应用程序中是否显着或轻微。
我已经为使用 Web 和桌面应用程序构建了一个 NHibernate 基础结构。
但是,我想知道是否有必要在使用事务时使用FlushMode.Commit。
如果我在获取或创建 ISession 时使用默认 FlushMode.Auto 会有问题吗?
我已阅读以下内容:"Set the session's flush mode to Commit to avoid unnecessary trips to the database when using transactions."。
但是,我希望有人向我保证,就功能而言,FlushMode.Auto 也能胜任。
这只是避免不必要的数据库访问的性能问题吗?
我可能会忍受它。
请给我你的光!
TL;DR
不,没有必要改变刷新模式。如果不确定,请将其保留为 Auto
。您将避免错误。
完整解释
默认冲洗模式 Auto
的工作方式与描述的一样 here:
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. This process, flush, occurs by default at the following points
- from some invocations of Find() or Enumerable()
- from NHibernate.ITransaction.Commit()
- from ISession.Flush()
此逻辑是为了确保查询数据库不会 return 陈旧数据,关于对当前事务所做的事情。
Except when you explicity Flush(), there are absolutely no guarantees about when the Session executes the ADO.NET calls, only the order in which they are executed. However, NHibernate does guarantee that the ISession.CreateQuery(..) methods will never return stale data; nor will they return the wrong data.
更改默认的刷新模式可能会导致您在事务中查询 return 数据而不考虑之前在同一事务中所做的事情。
除非您非常确定您不会被由于陈旧数据引起的任何错误所困扰 return 由同一事务中的后续查询编辑,否则最好将刷新模式保留为 Auto
。
更改刷新模式以限制事务内的 SQL 往返需要检查所有当前事务并在需要考虑之前的查询之前添加对 Flush
的显式调用在他们的交易中完成。然后希望有什么新的发展也不要忘记做。因此,您不仅不一定会节省很多 SQL 往返,而且还会为错误创造新的机会。
我个人认为不值得更改默认刷新模式以减少 SQL 往返次数。特别是如果你不检查它在你的应用程序中是否显着或轻微。