Hibernate在使用entityManager.flush()时锁定行。

huangapple go评论49阅读模式
英文:

Hibernate locking rows when using entityManager.flush()

问题

I have a standard stack Spring / JPA / Hibernate / Oracle, and found out that when I manually control the flush during a transaction, all entities subject to this flush (because of dirty check mainly) are locked in the database with a READ lock until the end of the transaction.

We are not using any read lock, we have some write locks but not for the transaction I am testing, and this transaction is not read-only.

Is this standard behavior? Can it be avoided?
On my application, I have a set of queries that are run in a single transaction, they are of any kind, deletion, insertion, and updates. I want to be sure my delete queries are run first, thus the manual flush I need after the delete queries, but I don't want them to be locked in the database.

英文:

I have a standard stack Spring / JPA / Hibernate / Oracle, and found out that when I manually control the flush during a transaction, all entities subject to this flush (because of dirty check mainly) are locked in database with a READ lock until the end of the transaction.

We are not using any read lock, we have some write locks but not for the transaction I am testing, and this transaction is not read-only.

Is this standard behavior? Can it be avoided?
On my application, I have a set of queries that are run in a single transaction, they are of any kind, deletion, insertion, and updates. I want to be sure my delete queries are run first, thus the manual flush I need after the delete queries, but I don't want them to be locked in database.

答案1

得分: 1

No, it is not on Hibernate, it is done by Oracle.

>The purpose of a DML lock, also called a data lock, is to guarantee the integrity of data being accessed concurrently by multiple users.
...
DML statements automatically acquire locks at both the table level and the row level.

DML语句获得的锁的摘要

SQL语句 行锁 表锁模式 RS RX S SRX X
INSERT INTO table ... SX Y Y N N N
UPDATE table ... SX Y Y N N N
DELETE FROM table ... SX Y Y N N N
  • RS - 行共享锁
  • RX - 行排他锁
  • S - 表共享锁
  • SRX - 表共享行排他锁
  • X - 表排他锁
  • SX - 子排他表锁 (行排他锁的同义词)

有关自动锁的完整文档可在以下链接找到:

英文:

No, it is not on Hibernate, it is done by Oracle.

>The purpose of a DML lock, also called a data lock, is to guarantee the integrity of data being accessed concurrently by multiple users.
...
DML statements automatically acquire locks at both the table level and the row level.

Chunked summary of Locks Obtained by DML Statements

SQL Statement Row Locks Table Lock Mode RS RX S SRX X
INSERT INTO table ... Yes SX Y Y N N N
UPDATE table ... Yes SX Y Y N N N
DELETE FROM table ... Yes SX Y Y N N N
  • RS - row share lock
  • RX - row exclusive lock
  • S - share table lock
  • SRX - share row exclusive table lock
  • X - exclusive table lock
  • SX - subexclusive table lock (synonym for row exclusive lock)

Full documentation about Automatic Locks are available at:

huangapple
  • 本文由 发表于 2023年4月7日 00:45:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951890.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定