1

When we set isolation level in @Transactional annotation, then Isolation level is set on Connection attribute,

so when the transaction is began then it must be firing SET TRANSACTION ISOLATION LEVEL READ COMMITTED query. (This is my assumption)

So does it fires this query on every Transaction?

does it requires to set transaction isolation on every transaction is spring?

eatSleepCode
  • 4,427
  • 7
  • 44
  • 93

2 Answers2

0

Spring will run the statement to change the isolation only if you have set an isolation level in the annotation (obvious) and the isolation level is not the same one as the one currently set on the connection. At the end of the transaction, Spring will revert this and set the previous isolation level. Also, I think you get a nice exception if you mix isolation levels, but I cannot find that in the code now.

By default, oracle's connection isolation level is set to read committed, so you don't need to specify the isolation level unless you have set the default to serializable... and if you set it, then no damage done, as spring won't do anything.

All of this is in the AbstractPlatformTransactionManager, DataSourceTransactionManager and DataSourceUtils if you fancy looking under the hood :).

Augusto
  • 28,839
  • 5
  • 58
  • 88
0

Depends which db you are using default isolation level can be different. For instance my local mySQL and Google Cloud SQL have REPEATABLE-READ. To check these settings I used:

SHOW VARIABLES WHERE Variable_name ='tx_isolation'

does it requires to set transaction isolation on every transaction is spring?

No. To change REPEATABLE-READ to READ COMMITTED on every transaction without touching @Tansactional annotations I added this to my persistence.xml

<property name="hibernate.connection.isolation">2</property>

Where

1: READ UNCOMMITTED
2: READ COMMITTED
4: REPEATABLE READ
8: SERIALIZABLE

Now, at the beginning of every new transaction hibernate does

SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED
Marek Raki
  • 3,056
  • 3
  • 27
  • 50