Java Reference
In-Depth Information
Thread 1_Prepare to increase book stock
Thread 1 _ Book stock increased by 5
Thread 1 _ Sleeping
Thread 2 _ Prepare to check book stock
Thread 1 _ Wake up
Thread 1 _ Book stock rolled back
Thread 2 _ Book stock is 10
Thread 2 _ Sleeping
Thread 2_Wake up
In order that the underlying database can support the READ_COMMITTED isolation level, it may acquire
an update lock on a row that was updated but not yet committed. Then other transactions must wait to
read that row until the update lock is released, which happens when the locking transaction commits or
rolls back.
The REPEATABLE_READ Isolation Level
Now let's restructure the threads to demonstrate another concurrency problem. Swap the tasks of the
two threads so that thread 1 checks the book stock before thread 2 increases the book stock.
package com.apress.springenterpriserecipes.bookshop.spring;
...
public class Main {
public static void main(String[] args) {
...
final BookShop bookShop = (BookShop) context.getBean( " bookShop " );
Thread thread1 = new Thread(new Runnable() {
public void run() {
bookShop.checkStock( " 0001 " );
}
}, " Thread 1 " );
Thread thread2 = new Thread(new Runnable() {
public void run() {
try {
bookShop.increaseStock( " 0001 " , 5);
} catch (RuntimeException e) {}
}
}, " Thread 2 " );
 
Search WWH ::




Custom Search