Java Reference
In-Depth Information
System.out.println(threadName + " - Book stock is " + stock);
sleep(threadName);
return stock;
}
private void sleep(String threadName) {
System.out.println(threadName + " - Sleeping " );
try {
Thread.sleep(10000);
} catch (InterruptedException e) {}
System.out.println(threadName + " - Wake up " );
}
}
To simulate concurrency, your operations need to be executed by multiple threads. You can track
the current status of the operations through the println statements. For each operation, you print a
couple of messages to the console around the SQL statement's execution. The messages should include
the thread name for you to know which thread is currently executing the operation.
After each operation executes the SQL statement, you ask the thread to sleep for 10 seconds. As you
know, the transaction will be committed or rolled back immediately once the operation completes.
Inserting a sleep statement can help to postpone the commit or rollback. For the increase() operation,
you eventually throw a RuntimeException to cause the transaction to roll back.
Before you start with the isolation level examples, enter the data from Tables 4-11 and 4-12 into your
bookshop database. (Note that the ACCOUNT table isn't needed in this example.)
Table 4-11. Sample Data in the BOOK Table for Testing Isolation Levels
ISBN
BOOK_NAME
PRICE
0001
The First Book
30
Table 4-12. Sample Data in the BOOK_STOCK Table for Testing Isolation Levels
ISBN
STOCK
0001
10
The READ_UNCOMMITTED and READ_COMMITTED Isolation Levels
READ_UNCOMMITTED is the lowest isolation level that allows a transaction to read uncommitted changes
made by other transactions. You can set this isolation level in the @Transaction annotation of your
checkStock() method.
Search WWH ::




Custom Search