在NHibernate中如何处理并发性?

时间:2022-05-25 06:59:29

How do you support optimistic / pessimistic concurrency using NHibernate?

您如何使用NHibernate来支持乐观/悲观的并发性?

3 个解决方案

#1


16  

NHibernate, by default, supports optimistic concurrency. Pessimistic concurrency, on the other hand, can be accomplished through the ISession.Lock() method.

默认情况下,NHibernate支持乐观并发。另一方面,可以通过ISession.Lock()方法实现悲观并发。

These issues are discussed in detail in this document.

本文将详细讨论这些问题。

#2


22  

NHibernate supports 2 types of optimistic concurrency.

NHibernate支持两种类型的乐观并发。

You can either have it check dirty fields by using "optimistic-lock=dirty" attribute on the "class" element in your mapping files or you can use "optimistic-lock=version" (which is also the default). If you are using version you need to provide a "version" element in your mapping file that maps to a field in your database.

您可以通过在映射文件中的“class”元素上使用“optimislock =dirty”属性来检查脏字段,或者您可以使用“optimislock =version”(这也是默认的)。如果您正在使用版本,您需要在映射文件中提供一个“版本”元素,映射到数据库中的一个字段。

Version can be of type Int64, Int32, Int16, Ticks, Timestamp, or TimeSpan and are automatically incremented on save. See Chapter 5 in the NHibernate documentation for more info.

版本可以是Int64、Int32、Int16、嘀嗒、时间戳或TimeSpan,并且在保存时自动递增。有关更多信息,请参见NHibernate文档中的第5章。

#3


2  

You can also 'just' manually compare the version numbers (assuming you've added a Version property to your entity).

您还可以“仅”手动比较版本号(假设您已经向实体添加了一个版本属性)。

Clearly Optimistic is the only sane option. Sometimes of course, we have to deal with crazy scenarios however...

显然,乐观是唯一明智的选择。当然,有时我们不得不面对疯狂的场景……

#1


16  

NHibernate, by default, supports optimistic concurrency. Pessimistic concurrency, on the other hand, can be accomplished through the ISession.Lock() method.

默认情况下,NHibernate支持乐观并发。另一方面,可以通过ISession.Lock()方法实现悲观并发。

These issues are discussed in detail in this document.

本文将详细讨论这些问题。

#2


22  

NHibernate supports 2 types of optimistic concurrency.

NHibernate支持两种类型的乐观并发。

You can either have it check dirty fields by using "optimistic-lock=dirty" attribute on the "class" element in your mapping files or you can use "optimistic-lock=version" (which is also the default). If you are using version you need to provide a "version" element in your mapping file that maps to a field in your database.

您可以通过在映射文件中的“class”元素上使用“optimislock =dirty”属性来检查脏字段,或者您可以使用“optimislock =version”(这也是默认的)。如果您正在使用版本,您需要在映射文件中提供一个“版本”元素,映射到数据库中的一个字段。

Version can be of type Int64, Int32, Int16, Ticks, Timestamp, or TimeSpan and are automatically incremented on save. See Chapter 5 in the NHibernate documentation for more info.

版本可以是Int64、Int32、Int16、嘀嗒、时间戳或TimeSpan,并且在保存时自动递增。有关更多信息,请参见NHibernate文档中的第5章。

#3


2  

You can also 'just' manually compare the version numbers (assuming you've added a Version property to your entity).

您还可以“仅”手动比较版本号(假设您已经向实体添加了一个版本属性)。

Clearly Optimistic is the only sane option. Sometimes of course, we have to deal with crazy scenarios however...

显然,乐观是唯一明智的选择。当然,有时我们不得不面对疯狂的场景……