0

Using isolation level as READ_UNCOMMITTED or READ_COMMITTED, the same set of rows are returned even on updating one of the rows in another transaction. I checked in the pg_stat_activity logs, query is fired everytime if I am running the same query multiple times. So, there is nothing related to hibernate first level cache.

Here is the code:

@Transactional(isolation = Isolation.READ_COMMITTED)
public void checkTransactional() {
    List<Subject> subjects = subjectRepository.findAllByTutorID(2l); // statement 1
    
    List<Subject> subjects1 = subjectRepository.findAllByTutorID(2l); // statement 2
}

Table state before running above code.

id | name | tutorial_id ----+--------------+------------- 7 | Mohan | 2 8 | Sohan | 2

On running application: Step 1: After executing statement 1: result in the subjects has two entities [(7, "Mohan", 2), (8, "Sohan", 2)]

Step 2: Putting debugger before running statement 2: the row with id 8 is updated with name "Tohan" directly via commandline. update subjects set name = 'Tohan' where id = 8;

Table State: id | name | tutorial_id ----+--------------+------------- 7 | Mohan | 2 8 | Tohan | 2

Step 3: Resuming the application, and after executing statement 2: result in the subjects1 is again having the same two entities as in statement 1 [(7, "Mohan", 2), (8, "Sohan", 2)]

It seems, it is always working in repeatable_read isolation. Any thoughts why it is happening?

  • The database isolation level is set to read_committed. ** The results are same even on using @Transactional(isolation = Isolation.READ_UNCOMMITTED)

After executing statement 2: result in the subjects1 should have been [(7, "Mohan", 2), (8, "Tohan", 2)]

  • 1
    Does this answer your question? [PostgreSQL Transaction Isolation READ UNCOMMITTED](https://stackoverflow.com/questions/33646012/postgresql-transaction-isolation-read-uncommitted) – Chin Huang Mar 23 '23 at 05:14

1 Answers1

1

PostgreSQL Transaction Isolation READ UNCOMMITTED explains that Postgres doesn't really make a difference between READ_UNCOMMITTED or READ_COMMITTED.

But also your assertion

query is fired everytime if I am running the same query multiple times. So, there is nothing related to hibernate first level cache.

is wrong. Hibernate might query the database and then discover that the entity is question is already in the cache and will use that instance, ignoring all the data just returned from the database.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348