[英]Isolation level repeatable read spring boot
我的Spring Boot代碼有問題。 我試圖用@Transactional注釋理解隔離級別,並給出以下代碼:
//BookingService class bookingService2 instance
@Transactional(isolation = Isolation.REPEATABLE_READ)
public String readConstantly() throws InterruptedException {
String name = "";
for(int i=0; i<2; i++) {
name = jdbcTemplate.query("select FIRST_NAME from BOOKINGS where ID=1", (rs, rowNum) -> rs.getString("FIRST_NAME")).get(0);
logger.info("Read name " + name);
Thread.sleep(1000);
}
return name;
}
和此代碼:
//BookingService class bookingService instance
@Transactional
public void updateOne(){
jdbcTemplate.update("update BOOKINGS set FIRST_NAME = ? where ID = 1", "zz");
}
像這樣執行:
new Thread(() -> {
try {
bookingService2.readConstantly();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
bookingService.updateOne();
我希望updateOne等到readConstantly事務結束,但是當我調用它時,將輸出:
2016-11-30 12:29:15.158 INFO 6456 --- [ Thread-3] hello.BookingService : Read name Alice
2016-11-30 12:29:16.158 INFO 6456 --- [ Thread-3] hello.BookingService : Read name zz
我試圖了解為什么在這種情況下隔離級別沒有做很多工作?
以此看來,H2不支持可重復讀取。 您可能需要使用“可序列化”代替。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.