JPA中的乐观锁定如何工作? [英] Optimistic locking in JPA how does it work?

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

问题描述

我理解什么是乐观锁定和它如何工作但我不知道如何在Java EE上实现它。

I understand what is optimistic locking and "how it work" but I don't know how can I implement it on Java EE.

我有一个实体JPA和我再添加一个版本列,并使用 @Version 对其进行注释。但是为了获得乐观的锁定管理,我只需要@Version注释?

I have an entity in JPA and I add one more version column and I annotated it with @Version . But to have an optimistic locking management I need only @Version annotation?

这是我的Java类:

@Entity
public class MyClass implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;
    //other variables
    @Version
    @Column(name = "version")
    private int version;
 //other code
}

在我的项目中,我使用无状态会话bean 访问Entity类并保留更多操作,因此我默认使用 @TransactionManagement(TransactionManagementType.CONTAINER)来处理我的事务。

In my project I use Stateless session bean to access to Entity class and persist more operations, so I use @TransactionManagement(TransactionManagementType.CONTAINER) by default to handle my transaction.

我的疑问是:
@TransactionManagement(TransactionManagementType.CONTAINER)(独立于 @TransactionAttribute (REQUIRED,MANDATORY等))并且仅在MyClass.java中注释 version 变量我获得了乐观锁定管理?

My doubt is: with @TransactionManagement(TransactionManagementType.CONTAINER) ( independently what is @TransactionAttribute (REQUIRED,MANDATORY etc)) and only annotating version variable in MyClass.java I obtain the optimistic locking management?

推荐答案

来自 Pro JPA 2:掌握Java™Persistence API


几句警告关于版本字段是有序的。第一个是它们不能保证在托管实体或数据库中更新作为批量更新操作的一部分。 (...)值得记住的第二点是,只有当非关系字段或拥有的外键关系字段(例如,多对一和一个)时,版本字段才会自动更新一对一的源外键关系)被修改。 (...)默认情况下, JPA假设(...)Read Committed 隔离。使用版本锁定的正常执行与Read Committed隔离一起使用,以在交叉写入的情况下提供额外的数据一致性检查。满足更严格的锁定约束比锁定提供要求使用额外的锁定策略

A couple of words of warning about version fields are in order. The first is that they are not guaranteed to be updated, either in the managed entities or the database, as part of a bulk update operation. (...) The second point worth remembering is that version fields will be automatically updated only when either the non-relationship fields or the owning foreign key relationship fields (e.g., many-to-one and one-to-one source foreign key relationships) are modified. (...) By default, JPA assumes (...) Read Committed isolation. Normal execution using version locking works with Read Committed isolation to provide additional data-consistency checks in the face of interleaved writes. Satisfying tighter locking constraints than what this locking offers requires that an additional locking strategy be used.

(强调我的)

所以,回答你的问题:否。使用 @Version 并未涵盖所有基础。

So, to answer your question: No. Using @Version does not cover all the bases.

上述额外锁定策略涉及通过适当的锁定模式( OPTIMISTIC OPTIMISTIC_FORCE_INCREMENT )到 EntityManager.lock() EntityManager.refresh() EntityManager.find()查询.setLockMode(),如果适用的话。我不会在这里详细介绍,而是建议跟进阅读(这里有)书中的详细讨论也是如此)。

The aforementioned additional locking strategy involves passing an appropriate locking mode (OPTIMISTIC or OPTIMISTIC_FORCE_INCREMENT) to EntityManager.lock(), EntityManager.refresh(), EntityManager.find(), and Query.setLockMode(), where applicable. I won't go into more details here and instead recommend a follow up read (there's a detailed discussion in the book as well).

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

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