Java Reference
In-Depth Information
package com.apress.springenterpriserecipes.bookshop.spring;
...
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
public class JdbcBookShop extends JdbcDaoSupport implements BookShop {
...
@Transactional(
isolation = Isolation.READ_UNCOMMITTED)
public int checkStock(String isbn) {
...
}
}
You can create some threads to experiment on this transaction isolation level. In the following
Main
class, there are two threads you are going to create. Thread 1 increases the book stock, while thread 2
checks the book stock. Thread 1 starts 5 seconds before thread 2.
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() {
try {
bookShop.increaseStock(
"
0001
"
, 5);
} catch (RuntimeException e) {}
}
},
"
Thread 1
"
);
Thread thread2 = new Thread(new Runnable() {
public void run() {
bookShop.checkStock(
"
0001
"
);
}
},
"
Thread 2
"
);
thread1.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {}
thread2.start();
}
}
Search WWH ::
Custom Search