Skip to content

Commit ec44dff

Browse files
committed
Deadlock
1 parent 4d93507 commit ec44dff

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
package com.bookstore.repository;
22

33
import com.bookstore.entity.Author;
4+
import java.util.List;
45
import java.util.Optional;
56
import javax.persistence.LockModeType;
67
import org.springframework.data.jpa.repository.JpaRepository;
78
import org.springframework.data.jpa.repository.Lock;
9+
import org.springframework.data.jpa.repository.Modifying;
10+
import org.springframework.data.jpa.repository.Query;
811
import org.springframework.stereotype.Repository;
912

1013
@Repository
1114
public interface AuthorRepository extends JpaRepository<Author, Long> {
1215

1316
@Override
1417
@Lock(LockModeType.PESSIMISTIC_WRITE)
15-
public Optional<Author> findById(Long id);
18+
public Optional<Author> findById(Long id);
19+
20+
@Modifying
21+
@Query("UPDATE Author SET genre = ?1 WHERE id = ?2")
22+
public void updateGenre(String genre, long id);
1623
}

HibernateSpringBootDeadlockExample/src/main/java/com/bookstore/repository/BookRepository.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@
55
import javax.persistence.LockModeType;
66
import org.springframework.data.jpa.repository.JpaRepository;
77
import org.springframework.data.jpa.repository.Lock;
8+
import org.springframework.data.jpa.repository.Modifying;
9+
import org.springframework.data.jpa.repository.Query;
810
import org.springframework.stereotype.Repository;
911

1012
@Repository
1113
public interface BookRepository extends JpaRepository<Book, Long> {
1214

1315
@Override
1416
@Lock(LockModeType.PESSIMISTIC_WRITE)
15-
public Optional<Book> findById(Long id);
17+
public Optional<Book> findById(Long id);
18+
19+
@Modifying
20+
@Query("UPDATE Book SET title = ?1 WHERE id = ?2")
21+
public void updateTitle(String title, long id);
1622
}

HibernateSpringBootDeadlockExample/src/main/java/com/bookstore/service/BookstoreService.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ protected void doInTransactionWithoutResult(
4040

4141
log.info("Starting first transaction (A) ...");
4242

43-
Author author = authorRepository.findById(1L).orElseThrow();
44-
author.setGenre("Comedy"); // this ends successfully
43+
authorRepository.findById(1L).orElseThrow(); // get the lock
44+
authorRepository.updateGenre("Comedy", 1L);
4545

4646
try {
4747
log.info("Holding in place first transaction (A) for 10s ...");
@@ -51,8 +51,8 @@ protected void doInTransactionWithoutResult(
5151
Thread.currentThread().interrupt();
5252
}
5353

54-
Book book = bookRepository.findById(1L).orElseThrow();
55-
book.setTitle("A happy day"); // this cannot be done, transaction (B) holds the lock
54+
bookRepository.findById(1L).orElseThrow(); // this cannot be done, transaction (B) holds the lock
55+
bookRepository.updateTitle("A happy day", 1L);
5656
}
5757
});
5858

@@ -71,8 +71,8 @@ protected void doInTransactionWithoutResult(
7171

7272
log.info("Starting second transaction (B) ...");
7373

74-
Book book = bookRepository.findById(1L).orElseThrow();
75-
book.setTitle("A long night"); // this ends successfully
74+
bookRepository.findById(1L).orElseThrow(); // get the lock
75+
bookRepository.updateTitle("A long night", 1L);
7676

7777
try {
7878
log.info("Holding in place second transaction (B) for 10s ...");
@@ -82,8 +82,8 @@ protected void doInTransactionWithoutResult(
8282
Thread.currentThread().interrupt();
8383
}
8484

85-
Author author = authorRepository.findById(1L).orElseThrow();
86-
author.setGenre("Horror"); // // this cannot be done, transaction (A) holds the lock
85+
authorRepository.findById(1L).orElseThrow(); // this cannot be done, transaction (A) holds the lock
86+
authorRepository.updateGenre("Horror", 1L);
8787
}
8888
});
8989

0 commit comments

Comments
 (0)