Java JPA 中的 PESSIMISTIC_READ 和 PESSIMISTIC_WRITE 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1657124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between PESSIMISTIC_READ and PESSIMISTIC_WRITE in JPA?
提问by paka
I have read the article Locking and Concurrency in Java Persistence 2.0, and run the sample application. But i still cant 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".
我已经阅读了Java Persistence 2.0 中的锁定和并发文章,并运行了示例应用程序。但是我仍然无法意识到 PESSIMISTIC_READ 和 PESSIMISTIC_WRITE 之间的区别。我尝试修改代码,并且使用 PESSIMISTIC_READ 和 PESSIMISTIC_WRITE 的代码将具有与使用“for update”调用 sql 相同的结果。
回答by Joseph
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.
悲观_阅读。一旦事务读取实体,实体管理器就会锁定实体。锁一直保持到事务完成。当您希望使用可重复读取语义查询数据时,将使用此锁定模式。换句话说,您希望确保在连续读取之间不会更新数据。这种锁定模式不会阻止其他事务读取数据。
悲观_写。一旦事务更新实体,实体管理器就会锁定实体。此锁定模式强制尝试更新实体数据的事务之间进行序列化。当并发更新事务之间更新失败的可能性很高时,通常使用这种锁定模式。
回答by James
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).
规范允许 JPA 实现为每个实现使用不同类型的数据库锁。大多数数据库只有一种类型的声明锁,因此在大多数实现中,两者是相同的(没有区别)。
回答by Sergii Shevchyk
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_READ
lock 意味着当你有这样的锁时,脏读和不可重复读是不可能的。如果要更改数据,则需要获取PESSIMISTIC_WRITE
锁定
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).
PESSIMISTIC_WRITE
锁保证除了脏读和不可重复读是不可能的,你可以在不获得额外锁的情况下更新数据(并且可能deadlocks
在等待排他锁时)。
╔══════════════════════╦══════════════════════════╦══════════════════════════╗
║ 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:
资源:
回答by Vlad Mihalcea
The PESSIMISTIC_READ
acquires a shared (read) lock on the associated table row record, while the PESSIMISTIC_WRITE
acquires an exclusive (write) lock.
在PESSIMISTIC_READ
获取关于相关联的表的行记录的共享(读)锁定,而PESSIMISTIC_WRITE
获取独占(写)锁。
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 (PESSIMISTIC_READ
) will simply acquire an exclusive lock request (PESSIMISTIC_WRITE
).
值得一提的是,对于Hibernate,如果数据库不支持共享锁(例如Oracle),那么共享锁请求(PESSIMISTIC_READ
)只会获取排他锁请求(PESSIMISTIC_WRITE
)。
For more details, check out this article about locksand this article about JPA pessimistic lock types.
有关更多详细信息,请查看这篇关于锁的文章和这篇关于 JPA 悲观锁类型的文章。