0

I've got problem with spring boot code. I'm trying to understand isolation levels with @Transactional annotation and given this code:

   //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;
    }

and this code:

//BookingService class bookingService instance
@Transactional
public void updateOne(){
    jdbcTemplate.update("update BOOKINGS set FIRST_NAME = ? where ID = 1", "zz");
}

With execution like that:

 new Thread(() -> {
            try {
                bookingService2.readConstantly();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        bookingService.updateOne();

I expect updateOne to wait until readConstantly transaction ends, but when i invoke it this is output:

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

I try to understand why in this case isolation level don't do much work?

Kamil Banaszczyk
  • 1,133
  • 1
  • 6
  • 23

1 Answers1

0

Looking at this, it seems that H2 does not Support repeatable reads. You may Need to use "Serializable" instead.

Thomas Philipp
  • 261
  • 1
  • 5
  • Hi, it still don't works. What is even better, i simply added SET LOCK_MODE 1; in sql schema script, and even obtained datasource and set isolation level there through ((org.apache.tomcat.jdbc.pool.DataSource) template.getDataSource()).setDefaultTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); and it still don't work as expected. Might it be because of using JDBCTemplate instead of plain jdbc? Also i've even checked references to data source from txmanager and jdbctemplate and those are same – Kamil Banaszczyk Nov 30 '16 at 13:02