JPA 中的 PESSIMISTIC_READ 和 PESSIMISTIC_WRITE 有什么区别?

发布于 2024-08-09 03:23:07 字数 367 浏览 2 评论 0原文

我已阅读文章 Java Persistence 2.0 中的锁定和并发,并运行示例应用程序。但我仍然无法意识到 PESSIMISTIC_READPESSIMISTIC_WRITE 之间的区别。我尝试修改代码,其中使用 PESSIMISTIC_READPESSIMISTIC_WRITE 的代码将具有与使用 for update 调用 SQL 相同的结果。

I have read the article Locking and Concurrency in Java Persistence 2.0, and run the sample application. But I still can't realize the difference between PESSIMISTIC_READ and PESSIMISTIC_WRITE. I tried to modify the code, and where the code using PESSIMISTIC_READ and PESSIMISTIC_WRITE will have the same result that the SQL will invoked with for update.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

转身以后 2024-08-16 03:23:07

这可能是最不技术性的答案,所以如果我的语义错误,请道歉。但我对之前答案中语言的复杂性感到沮丧,所以我决定发布一个简单的答案:

  • PESSIMISTIC_READ:您在事务开始时获得记录上的锁定,仅用于读取目的。基本上你是在说“我不希望任何人在我阅读该记录时更新该记录,但我不介意其他人也阅读它”。这意味着尝试 PESSIMISTIC_READ 的人将会成功,但尝试 PESSIMISTIC_WRITE 的人将会失败

  • PESSIMISTIC_WRITE:您在事务开始时获得记录上的锁定,以便进行写入。您所说的是“我将更新此记录,因此在我完成之前没有人可以读取或写入它”。这意味着尝试 PESSIMISTIC_READ 或 PESSIMISTIC_WRITE 的操作都会失败

PESSIMISTIC 部分指的是您在事务开始时获得锁,即在对记录进行任何更改之前,而不是在事务结束时获得锁。即将将更改提交到记录。

This is probably going to be the least technical answer, so apologies if I get the semantics wrong. But I was frustrated with the complexity of the language in the previous answers, so I decided to post a simple answer:

  • PESSIMISTIC_READ: you obtain a lock on the record at the start of the transaction, for the purpose of reading only. Basically you're saying "I don't want anyone updating this record while I'm reading it, but I don't mind others reading it as well". That means people also attempting a PESSIMISTIC_READ will succeed, but those attempting a PESSIMISTIC_WRITE will fail

  • PESSIMISTIC_WRITE: you obtain a lock on the record at the start of the transaction, for the purpose of writing. What you're saying is "I'm going to be updating this record, so no one can read or write to it until I'm done". That means both those attempting a PESSIMISTIC_READ or PESSIMISTIC_WRITE will fail

the PESSIMISTIC part refers to the fact that you get the lock at the start of the transaction i.e. before making any changes to the record, rather than at the end of the transaction, when you are about to commit the changes to the record.

慢慢从新开始 2024-08-16 03:23:07

区别在于锁定机制。

PESSIMISTIC_READ 锁意味着当你拥有这样的锁时,脏读和不可重复读是不可能的。如果要更改数据,则需要获取 PESSIMISTIC_WRITE

PESSIMISTIC_WRITE 锁保证除了脏读和不可重复读之外,您还可以在不获取额外锁的情况下更新数据(并且可能<等待独占锁时发生代码>死锁)。

╔══════════════════════╦══════════════════════════╦══════════════════════════╗
║     LockModeType     ║     PESSIMISTIC_READ     ║    PESSIMISTIC_WRITE     ║
╠══════════════════════╬══════════════════════════╬══════════════════════════╣ 
║         type         ║       SHARED LOCK        ║      EXCLUSIVE LOCK      ║
╠══════════════════════╬══════════════════════════╬══════════════════════════╣   
║  isReadOnly without  ║                          ║                          ║
║   additional locks   ║            YES           ║            NO            ║
╠══════════════════════╬══════════════════════════╬══════════════════════════╣
║      dirty reads     ║            NO            ║            NO            ║
╠══════════════════════╬══════════════════════════╬══════════════════════════╣
║ non-repeatable reads ║            NO            ║            NO            ║
╠══════════════════════╬══════════════════════════╬══════════════════════════╣
║ how to update data   ║ obtain PESSIMISTIC_WRITE ║         ALLOWED          ║
╠══════════════════════╬══════════════════════════╬══════════════════════════╣
║                      ║       no one holds       ║      no one holds        ║
║ how to obtain lock   ║     PESSIMISTIC_WRITE    ║   PESSIMISTIC_READ   or  ║
║                      ║                          ║   PESSIMISTIC_WRITE      ║
╠══════════════════════╬══════════════════════════╬══════════════════════════╣
║                      ║                          ║   when there is a high   ║
║                      ║  you want to ensure no   ║ likelihood of deadlock or║
║      when to use     ║ dirty or non-repeatable  ║   update failure among   ║ 
║                      ║   reads are possible     ║    concurrent updating   ║
║                      ║                          ║       transactions       ║
╚══════════════════════╩══════════════════════════╩══════════════════════════╝

资源:

JPA 2.1

The difference lies in locking mechanism.

PESSIMISTIC_READ lock means that dirty reads and non-repeatable reads are impossible when you have such a lock. If data should be changed it's required to obtain PESSIMISTIC_WRITE lock

PESSIMISTIC_WRITE lock guarantees that besides dirty and non-repeatable reads are impossible you can update data without obtaining additional locks(and possible deadlocks while waiting for exclusive lock).

╔══════════════════════╦══════════════════════════╦══════════════════════════╗
║     LockModeType     ║     PESSIMISTIC_READ     ║    PESSIMISTIC_WRITE     ║
╠══════════════════════╬══════════════════════════╬══════════════════════════╣ 
║         type         ║       SHARED LOCK        ║      EXCLUSIVE LOCK      ║
╠══════════════════════╬══════════════════════════╬══════════════════════════╣   
║  isReadOnly without  ║                          ║                          ║
║   additional locks   ║            YES           ║            NO            ║
╠══════════════════════╬══════════════════════════╬══════════════════════════╣
║      dirty reads     ║            NO            ║            NO            ║
╠══════════════════════╬══════════════════════════╬══════════════════════════╣
║ non-repeatable reads ║            NO            ║            NO            ║
╠══════════════════════╬══════════════════════════╬══════════════════════════╣
║ how to update data   ║ obtain PESSIMISTIC_WRITE ║         ALLOWED          ║
╠══════════════════════╬══════════════════════════╬══════════════════════════╣
║                      ║       no one holds       ║      no one holds        ║
║ how to obtain lock   ║     PESSIMISTIC_WRITE    ║   PESSIMISTIC_READ   or  ║
║                      ║                          ║   PESSIMISTIC_WRITE      ║
╠══════════════════════╬══════════════════════════╬══════════════════════════╣
║                      ║                          ║   when there is a high   ║
║                      ║  you want to ensure no   ║ likelihood of deadlock or║
║      when to use     ║ dirty or non-repeatable  ║   update failure among   ║ 
║                      ║   reads are possible     ║    concurrent updating   ║
║                      ║                          ║       transactions       ║
╚══════════════════════╩══════════════════════════╩══════════════════════════╝

Resources:

JPA 2.1

病毒体 2024-08-16 03:23:07

一个是读锁,另一个是写锁,或者分别在读取或更新期间。

FTA:

  • PESSIMISTIC_READ。实体经理
    立即锁定该实体
    事务读取它。锁是
    保留直至交易完成。
    当您需要时使用此锁定模式
    使用可重复读取查询数据
    语义。换句话说,你想要
    以确保数据不
    在连续读取之间更新。
    该锁定模式不会阻塞其他
    通过读取数据进行交易。

    悲观_写。实体经理
    立即锁定该实体
    事务更新它。这把锁
    模式强制序列化
    尝试更新的事务
    实体数据。这种锁定方式经常出现
    当可能性很高时使用
    并发更新失败的情况
    更新事务。

One is a read lock and the other is a write lock, or during a read or an update, respectively.

FTA:

  • PESSIMISTIC_READ. The entity manager
    locks the entity as soon as a
    transaction reads it. The lock is
    held until the transaction completes.
    This lock mode is used when you want
    to query data using repeatable-read
    semantics. In other words, you want
    to ensure that the data is not
    updated between successive reads.
    This lock mode does not block other
    transactions from reading the data.

    PESSIMISTIC_WRITE. The entity manager
    locks the entity as soon as a
    transaction updates it. This lock
    mode forces serialization among
    transactions attempting to update the
    entity data. This lock mode is often
    used when there is a high likelihood
    of update failure among concurrent
    updating transactions.

一杯敬自由 2024-08-16 03:23:07

PESSIMISTIC_READ 获取关联表行记录上的共享(读)锁,而 PESSIMISTIC_WRITE 获取排它(写)锁。

共享锁会阻止任何其他并发的独占锁请求,但它允许其他共享锁请求继续进行。

独占锁会阻止共享锁和独占锁请求。

值得一提的是,对于Hibernate来说,如果数据库不支持共享锁(例如Oracle),则需要共享锁请求(例如PESSIMISTIC_READ)将简单地获取独占锁定请求(例如,PESSIMISTIC_WRITE)。

The PESSIMISTIC_READ acquires a shared (read) lock on the associated table row record, while the PESSIMISTIC_WRITE acquires an exclusive (write) lock.

The shared lock blocks any other concurrent exclusive lock requests, but it allows other shared lock requests to proceed.

The exclusive lock blocks both shared and exclusive lock requests.

What's worth mentioning is that, for Hibernate, if the database does not support shared locks (e.g. Oracle), then a shared lock request (e.g., PESSIMISTIC_READ) will simply acquire an exclusive lock request (e.g., PESSIMISTIC_WRITE).

剩余の解释 2024-08-16 03:23:07

该规范允许 JPA 实现为每个数据库使用不同类型的数据库锁。大多数数据库只有一种类型的声明性锁,因此在大多数实现中,两者是相同的(没有区别)。

The Spec allows for the JPA implementation to use a different type of database lock for each. Most databases only have one type of declarative lock, so in most implementations the two are identical (there is no difference).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文