英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论