具有@Version的实体注释字段或属性时,乐观锁如何自动启用?

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

Recently I have been studying database transaction and one article quotes as following

最近我一直在研究数据库事务,一篇文章引用如下

JPA provide automatic support of row versioning via the @Version annotation.When you have entity with @Version annotatted field or property, optimistic locking will be enabled automatically.

JPA通过@Version注释自动支持行版本控制。当您拥有带有@Version annotatted字段或属性的实体时,将自动启用乐观锁定。

My understanding is that database isolation level strategies are maintained using different locks like

我的理解是使用不同的锁来维护数据库隔离级别策略

  1. Read uncommitted: implemented with exclusive write-locks
  2. Read uncommitted:使用独占写锁实现
  3. Read committed: implemented using shared read locks and exclusive write-locks.
  4. 读取已提交:使用共享读锁和独占写锁实现。

so on. So, transactional isolation is implemented by different locking which I guess by using pessimistic locking.

等等。因此,事务隔离是通过不同的锁实现的,我猜通过使用悲观锁定。

My question is when a field is declared as @Version annotatted does it override the underlying default isolation level and optimistic locking take place ?

我的问题是当一个字段被声明为@Version annotatted它是否覆盖了底层的默认隔离级别并且发生了乐观锁定?

2 个解决方案

#1


2  

No, they are different things. By default, isolation level is configured to read-commited so no change could be read until transaction commit.

不,他们是不同的东西。默认情况下,隔离级别配置为read-commited,因此在事务提交之前不能读取任何更改。

If you decide to use optimistic locking by means of @Version, you are not changing isolation level at all, but it is assumed you want to use read-commited isolation level, because I think it has no sense to use read-uncommited or read-serialized when you are using optimistic locking, but you can.

如果你决定通过@Version使用乐观锁定,你根本就不会改变隔离级别,但是假设你想要使用read-commited隔离级别,因为我认为使用read-uncommited或read是没有意义的 - 在使用乐观锁定时进行序列化,但是可以。

You define isolation level when creating transaction, usually you specify read-only mode, isolation level, propagation mode and a name for the transaction.

您可以在创建事务时定义隔离级别,通常指定只读模式,隔离级别,传播模式和事务名称。

Optimistic locking is controlled by ORM infrastructure, taking care of object's proper number version when persisting. So, they are different things.

乐观锁定由ORM基础设施控制,在持久化时处理对象的正确数字版本。所以,他们是不同的东西。

Hope it helps!

希望能帮助到你!

#2


1  

No, you cannot override the underlying database's isolation level with JPA's optimistic locking.

不,您不能使用JPA的乐观锁定覆盖底层数据库的隔离级别。

If somehow you were able to do that, and doing that would be no good.

如果你能以某种方式做到这一点,那么这样做并不好。

Most of the databases adopt READ_COMMITED isolation level as their default.

大多数数据库都采用READ_COMMITED隔离级别作为默认值。

Consider the following scenario under READ_COMMITED isolation level.

请考虑READ_COMMITED隔离级别下的以下方案。

  • Two Managers loads a Product entity
  • 两个经理加载一个产品实体
  • Both of them decides to increase price and then save it.
  • 他们两人决定提高价格,然后保存。
  • One price update gets committed before the other.
  • 一个价格更新在另一个之前提交。
  • So one manager's Price will be overridden from the other manager's price with out managers knowing that.
  • 所以一位经理的价格将被其他经理的价格所取代,经理们知道这一点。

Although this scenario is trivial, these kind of things could happen in non trivial scenarios.

尽管这种情况微不足道,但这些事情可能会发生在非平凡的情况中。

This unintended scenario could have been avoided by using Optimistic Locking with the database's default isolation level.

通过将Optimistic Locking与数据库的默认隔离级别一起使用,可以避免这种意外情况。

Hope this helps.

希望这可以帮助。

#1


2  

No, they are different things. By default, isolation level is configured to read-commited so no change could be read until transaction commit.

不,他们是不同的东西。默认情况下,隔离级别配置为read-commited,因此在事务提交之前不能读取任何更改。

If you decide to use optimistic locking by means of @Version, you are not changing isolation level at all, but it is assumed you want to use read-commited isolation level, because I think it has no sense to use read-uncommited or read-serialized when you are using optimistic locking, but you can.

如果你决定通过@Version使用乐观锁定,你根本就不会改变隔离级别,但是假设你想要使用read-commited隔离级别,因为我认为使用read-uncommited或read是没有意义的 - 在使用乐观锁定时进行序列化,但是可以。

You define isolation level when creating transaction, usually you specify read-only mode, isolation level, propagation mode and a name for the transaction.

您可以在创建事务时定义隔离级别,通常指定只读模式,隔离级别,传播模式和事务名称。

Optimistic locking is controlled by ORM infrastructure, taking care of object's proper number version when persisting. So, they are different things.

乐观锁定由ORM基础设施控制,在持久化时处理对象的正确数字版本。所以,他们是不同的东西。

Hope it helps!

希望能帮助到你!

#2


1  

No, you cannot override the underlying database's isolation level with JPA's optimistic locking.

不,您不能使用JPA的乐观锁定覆盖底层数据库的隔离级别。

If somehow you were able to do that, and doing that would be no good.

如果你能以某种方式做到这一点,那么这样做并不好。

Most of the databases adopt READ_COMMITED isolation level as their default.

大多数数据库都采用READ_COMMITED隔离级别作为默认值。

Consider the following scenario under READ_COMMITED isolation level.

请考虑READ_COMMITED隔离级别下的以下方案。

  • Two Managers loads a Product entity
  • 两个经理加载一个产品实体
  • Both of them decides to increase price and then save it.
  • 他们两人决定提高价格,然后保存。
  • One price update gets committed before the other.
  • 一个价格更新在另一个之前提交。
  • So one manager's Price will be overridden from the other manager's price with out managers knowing that.
  • 所以一位经理的价格将被其他经理的价格所取代,经理们知道这一点。

Although this scenario is trivial, these kind of things could happen in non trivial scenarios.

尽管这种情况微不足道,但这些事情可能会发生在非平凡的情况中。

This unintended scenario could have been avoided by using Optimistic Locking with the database's default isolation level.

通过将Optimistic Locking与数据库的默认隔离级别一起使用,可以避免这种意外情况。

Hope this helps.

希望这可以帮助。