How To Deal With Transaction Isolation Levels? 📎
Transaction Isolation is the "I" in ACID and defines the visibility of changes in concurrent transactions. Serializable
is the highest value and ensures absolute consistency. Read Uncommitted
provides the highest concurrency with low consistency. NONE comes with no consistency and indicates the lack of transaction support.
Isolation Levels can be set on java.sql.Connection level. In a Java EE environment isolation levels can be configured on Data Source / Connection Pool (see e.g. Glassfish).
The transaction consistency does influence the business logic and has to be discussed with domain experts.
A low risk approach to set a proper Isolation Level is:
- Set the highest possible Isolation Level (
Serializable
) - Perform Stress Tests and measure the impact
- If the required throughput / performance cannot be met or deadlock occurs, escalate the problem, talk to domain experts and either change the business logic, or lower the Isolation Level
Thanks Pavan Madiraju for the question!
[See also an in-depth discussion in the "Real World Java EE Patterns--Rethinking Best Practices" book (Second Iteration, "Green Book"), page 19 in, chapter "The Concurrency Problem"]