Domanda

Is it possible to use different transaction isolation levels within the same application? For example, I want to use READ_COMMITTED for simple client requests and READ_REPEATABLE for more complicated requests.

È stato utile?

Soluzione

I have wasted weeks trying to figure this out so I am posting a record of what I learned. Additional answers are welcome.

While it is technically possible to change the transaction isolation level using Connection.setTransactionIsolation(int) I highly recommend against doing this.

  • The transaction isolation must be set before the beginning a transaction. Doing otherwise results in "implementation-defined" behavior.
  • In order to detect the appropriate transaction isolation you will have to figure out the transaction isolation required by any methods you invoke (and any methods they invoke).
  • This quickly evolves into a maintenance nightmare where you're struggling to keep track of the right isolation level for each method.
  • If you attempt to programmatically query the right isolation level at runtime (by asking your dependencies what isolation level they need) you will end up with very ugly code and again it becomes a maintenance nightmare.

Instead, I recommend biting the bullet and picking a single transaction isolation level across your entire application. Start out with something safe and slowly move to the higher-performing isolation levels.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top