Skip to content

Commit 11f1120

Browse files
authoredDec 5, 2020
Deadlock
1 parent 7a6186f commit 11f1120

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
 

‎README.md

+16
Original file line numberDiff line numberDiff line change
@@ -4075,3 +4075,19 @@ From Openjdk JEP359:
40754075

40764076
- [Batch Inserts In Spring Boot Style Via `CompletableFuture` And Return `List<S>` (1)](https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/HibernateSpringBootBatchInsertsCompletableFutureReturnList)
40774077
- [Batch Inserts In Spring Boot Style Via `CompletableFuture` And Return `List<S>` (2)](https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/HibernateSpringBootBatchInsertsCompletableFutureReturnGivenList)
4078+
4079+
-----------------------------------------------------------------------------------------------------------------------
4080+
4081+
278. **[How to simulate a deadlock](https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/HibernateSpringBootDeadlockExample)**
4082+
4083+
**Description:** This application is an example of causing a database deadlock in MySQL. This application produces an exception of type: `com.mysql.cj.jdbc.exceptions.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction`. However, the database will retry until one of the transaction (A) succeeds.
4084+
4085+
**Key points:**
4086+
- start *Transaction (A)* and trigger a `SELECT` with `PESSIMISTIC_WRITE` to acquire an exclusive lock to table `author`
4087+
- *Transaction (A)* update `author` genre with success and sleeps for 10s
4088+
- after 5s, start a concurrent *Transaction B* that trigger a `SELECT` with `PESSIMISTIC_WRITE` to acquire an exclusive lock to table `book`
4089+
- *Transaction (B)* update `book` title with success and sleeps for 10s
4090+
- *Transaction (A)* wakes up and attempt to update the book but it cannot acquire the lock holded by *Transaction (B)*
4091+
- *Transaction (B)* wakes up and attempt to update the author but it cannot acquire the lock holded by *Transaction (A)*
4092+
- DEADLOCK
4093+
- database retry and succeeds after *Transaction (B)* releases the lock

0 commit comments

Comments
 (0)
Failed to load comments.