乐观锁定在Hibernate中默认 [英] Optimistic Locking in Hibernate by default

查看:109
本文介绍了乐观锁定在Hibernate中默认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于Hibernate乐观锁定的问题。我试图深入与Hibernate进行乐观锁定,但我有一个疑问。 Hibernate使用版本方法(整数或时间戳)来实现乐观锁定。要进行配置,您可以使用@Version注释(或xml配置)并创建一个版本属性。另一种选择是使用optimistic-lock =all属性进行配置而不使用版本控制。

I have one question about optimistic locking in Hibernate. I am trying to go deep inside optimistic locking with Hibernate, but I have one doubt. Hibernate uses version approach (integer or timestamp) to implement optimistic locking. To configure you can use @Version annotation (or xml configuration) and create a version attribute. The other option is configuring without versioning using optimistic-lock="all" attribute.

我的问题是,如果您没有定义任何版本控制属性,也没有指定乐观锁定属性,那么在这种情况下哪种策略使用Hibernate? Pessimistc锁定我非常肯定,不,我想这是乐观锁定,但不知道如何。

My question is in case that you don not define any versioning attribute and also you do not specify an optimistic-lock attribute, which strategy uses Hibernate in this cases? Pessimistc Locking I am pretty sure that no, so I suppose that is optimistic locking but don't know how.

非常感谢您的关注。

推荐答案

如果您没有配置Hibernate使用乐观锁定,它根本不使用锁定。因此,在这种情况下,上次更新始终会获胜。

If you don't configure Hibernate to use optimistic locking, it uses no locking at all. So, in this case last update always wins.

为了清楚起见,请注意Hibernate乐观锁定与DBMS事务隔离完全不同。 Hibernate乐观锁定仅适用于在一个事务中加载对象,修改它并稍后将其保存在另一个事务中的情况。在这种情况下,乐观锁确保其他一些事务没有改变数据库中的那个对象。然而,乐观锁定不影响并发事务的隔离 - 因此,无论是否启用Hibernate锁定,DBMS内部用于实现事务隔离的锁定(乐观或悲观)仍然有效。

Just to make it clear, note that Hibernate optimistic locking is completely different from DBMS transaction isolation. Hibernate optimistic locking only works in situation when you load object in one transaction, modify it and save it later in another transaction. In this case optimistic locking ensures that some other transaction haven't changed that object in the database in between. However, optimistic locking doesn't affect isolation of concurrent transactions - so, locks (optimistic or pessimistic) used by DBMS internally to implement transaction isolation still works, no matter whether Hibernate locking is enabled or not.

这篇关于乐观锁定在Hibernate中默认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆