i have a really heavy business logic. I use spring data jpa with microsoft sql server. My code looks like this.
@Transactional(rollbackFor = Exception.class)
public void executeAllFlows() {
RandomTableObject a = randomTableObjectRepository.findById(1L);
executeFlow1();
executeFlow2();
executeFlow3();
executeFlow4();
a.setSomeAttribute(true);
randomTableObjectRepository.save(a);
}
@Transactional(rollbackFor = Exception.class)
private void executeFlow1() {
//read items from tempTable 1 and persist them in other 2 tables after performing logic
}
@Transactional(rollbackFor = Exception.class)
private void executeFlow2() {
//read items from tempTable 2 and persist them in other 2 tables after performing logic
}
@Transactional(rollbackFor = Exception.class)
private void executeFlow3() {
//read items from tempTable 3 and persist them in other 2 tables after performing logic
}
@Transactional(rollbackFor = Exception.class)
private void executeFlow4() {
//read items from tempTable 4 and persist them in other 2 tables after performing logic
}
This process may take up to two minutes because there are thousands of records involved. If something fails all tables go on rollback. Which is what i want.
The problem occurs when i want to do some dirty reads while this process runs. While the process starts running i try to read from the table of RandomTableObject. At the start of the process i get result but after a few seconds it delays the result until the whole process ends.
It seems while hibernate selects from this table and starts modifying the record a read lock is implemented. Default behavior of hibernate is not to enforce read locks so my next suspect are isolation levels of ms sql server.
According to this documentation of ms sql server: Understanding isolation levels Isolation level Read uncommitted allows dirty reads. I tried to enforce this by adding
@Transactional(rollbackFor = Exception.class , isolation = Isolation.READ_UNCOMMITTED)
above each method but this does not solve the problem. The import is from org.springframework.transactions.annotation.
Has anyone had the same problem with me and if yes is there any solution to this.