Hibernate乐观锁定. .它是如何工作的?

时间:2021-08-09 00:55:26

I was reading the below blog about hibernate optimistic locking. I am planning to use it with hibernate. But, I have one concern. we have java code and c++ code, both connect to one database. While, java code can use hibernate to achieve optimistic locking, I want to make the c++ code do the same thing. Also, c++ code is using some legacy code.

我正在阅读下面的关于hibernate乐观锁定的博客。我计划在hibernate中使用它。但是,我有一个问题。我们有java代码和c++代码,它们都连接到一个数据库。虽然java代码可以使用hibernate实现乐观锁定,但我想让c++代码做同样的事情。此外,c++代码使用了一些遗留代码。

http://turgaykivrak.wordpress.com/2009/05/16/72/

http://turgaykivrak.wordpress.com/2009/05/16/72/

Is there a documentation that explains how hibernate achieves optimistic locking?

有没有说明hibernate如何实现乐观锁定的文档?

Any suggestions are appreciated.

任何建议都欣赏。

Thank you
Bala

谢谢你巴拉

1 个解决方案

#1


11  

To be precise, you don't mean optimistic locking, but optimistic concurrency (without a lock). Using a timestamp for version is just for legacy database support, because a modern database can (at least theoretically) work faster than it's accuracy of storing a timestamp.

确切地说,您不是指乐观锁定,而是指乐观并发(没有锁)。为版本使用时间戳仅仅是为了支持遗留数据库,因为现代数据库(至少在理论上)可以比存储时间戳的准确性工作得更快。

Using the integer version property is very simple:

使用整数版本属性非常简单:

  • On insert: set version to 1
  • 插入时:将版本设置为1
  • On update and delete: increase version with 1 and append "where version=@version" to every sql statementent. Return the number of changed records. Throw a StaleObjectStateException when the number of changed records is different than expected.
  • 更新和删除:增加版本1和附加“where version=@version”到每个sql语句元素。返回已更改记录的数量。当更改记录的数量与预期的不同时抛出一个StaleObjectStateException。

Personally, I would not create two separate applications writing the same data in a non-legacy situation, because that means that business logic have to be duplicated and changes have to be applied to two applications, also when the change is relevant for only one of the applications.

就我个人而言,我不会创建两个单独的应用程序在一个非遗留的情况下编写相同的数据,因为这意味着业务逻辑必须被复制,并且必须将更改应用到两个应用程序中,而且当更改只与应用程序中的一个应用程序相关时。

#1


11  

To be precise, you don't mean optimistic locking, but optimistic concurrency (without a lock). Using a timestamp for version is just for legacy database support, because a modern database can (at least theoretically) work faster than it's accuracy of storing a timestamp.

确切地说,您不是指乐观锁定,而是指乐观并发(没有锁)。为版本使用时间戳仅仅是为了支持遗留数据库,因为现代数据库(至少在理论上)可以比存储时间戳的准确性工作得更快。

Using the integer version property is very simple:

使用整数版本属性非常简单:

  • On insert: set version to 1
  • 插入时:将版本设置为1
  • On update and delete: increase version with 1 and append "where version=@version" to every sql statementent. Return the number of changed records. Throw a StaleObjectStateException when the number of changed records is different than expected.
  • 更新和删除:增加版本1和附加“where version=@version”到每个sql语句元素。返回已更改记录的数量。当更改记录的数量与预期的不同时抛出一个StaleObjectStateException。

Personally, I would not create two separate applications writing the same data in a non-legacy situation, because that means that business logic have to be duplicated and changes have to be applied to two applications, also when the change is relevant for only one of the applications.

就我个人而言,我不会创建两个单独的应用程序在一个非遗留的情况下编写相同的数据,因为这意味着业务逻辑必须被复制,并且必须将更改应用到两个应用程序中,而且当更改只与应用程序中的一个应用程序相关时。