隔离级可重复读弹簧启动

时间:2016-11-30 11:35:16

标签: java spring transactions isolation-level

我遇到弹簧启动代码问题。我试图通过@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

我试着理解为什么在这种情况下隔离级别没有做太多工作?

1 个答案:

答案 0 :(得分:0)

this,似乎H2不支持可重复读取。您可能需要使用&#34; Serializable&#34;代替。