Spring transaction isolation level tutorial
31 January 2013
(4.75 / 8 ratings) - Rate this tutorial!
tags: txspringjava
Share this tutorial
In this tutorial you will learn about the transaction isolation level provided by the Spring framework.
1. Introduction

Transaction isolation level is a concept that is not exclusive to the Spring framework. It is applied to transactions in general and is directly related with the ACID transaction properties. Isolation level defines how the changes made to some data repository by one transaction affect other simultaneous concurrent transactions, and also how and when that changed data becomes available to other transactions. When we define a transaction using the Spring framework we are also able to configure in which isolation level that same transaction will be executed.

2. Usage example

Using the @Transactional annotation we can define the isolation level of a Spring managed bean transactional method. This means that the transaction in which this method is executed will run with that isolation level:

Isolation level in a transactional method
@Autowired
private TestDAO testDAO;

@Transactional(isolation=Isolation.READ_COMMITTED)
public void someTransactionalMethod(User user) {

  // Interact with testDAO

}

We are defining this method to be executed in a transaction which isolation level is READ_COMMITTED. We will see each isolation level in detail in the next sections.

3. READ_UNCOMMITTED

READ_UNCOMMITTED isolation level states that a transaction may read data that is still uncommitted by other transactions. This constraint is very relaxed in what matters to transactional concurrency but it may lead to some issues like dirty reads. Let's see the following image:

Dirty read

In this example Transaction A writes a record. Meanwhile Transaction B reads that same record before Transaction A commits. Later Transaction A decides to rollback and now we have changes in Transaction B that are inconsistent. This is a dirty read. Transaction B was running in READ_UNCOMMITTED isolation level so it was able to read Transaction A changes before a commit occurred.

Note: READ_UNCOMMITTED is also vulnerable to non-repeatable reads and phantom reads. We will also see these cases in detail in the next sections.
4. READ_COMMITTED

READ_COMMITTED isolation level states that a transaction can't read data that is not yet committed by other transactions. This means that the dirty read is no longer an issue, but even this way other issues may occur. Let's see the following image:

Non-repeatable read

In this example Transaction A reads some record. Then Transaction B writes that same record and commits. Later Transaction A reads that same record again and may get different values because Transaction B made changes to that record and committed. This is a non-repeatable read.

Note: READ_COMMITTED is also vulnerable to phantom reads. We will also see this case in detail in the next section.
5. REPEATABLE_READ

REPEATABLE_READ isolation level states that if a transaction reads one record from the database multiple times the result of all those reading operations must always be the same. This eliminates both the dirty read and the non-repeatable read issues, but even this way other issues may occur. Let's see the following image:

Phantom read

In this example Transaction A reads a range of records. Meanwhile Transaction B inserts a new record in the same range that Transaction A initially fetched and commits. Later Transaction A reads the same range again and will also get the record that Transaction B just inserted. This is a phantom read: a transaction fetched a range of records multiple times from the database and obtained different result sets (containing phantom records).

6. SERIALIZABLE

SERIALIZABLE isolation level is the most restrictive of all isolation levels. Transactions are executed with locking at all levels (read, range and write locking) so they appear as if they were executed in a serialized way. This leads to a scenario where none of the issues mentioned above may occur, but in the other way we don't allow transaction concurrency and consequently introduce a performance penalty.

7. DEFAULT

DEFAULT isolation level, as the name states, uses the default isolation level of the datastore we are actually connecting from our application.

8. Summary

To summarize, the existing relationship between isolation level and read phenomena may be expressed in the following table:

  dirty reads non-repeatable reads phantom reads
READ_UNCOMMITTED yes yes yes
READ_COMMITTED no yes yes
REPEATABLE_READ no no yes
SERIALIZABLE no no no
(4.75 / 8 ratings) - Rate this tutorial!
tags: txspringjava
Share this tutorial
Comments
Francisco A. Lozano
31 January 2013
It's important to mention that setting a the right isolation level in the datasource or datastore's defaults instead of forcing to explicit isolation level in annotations, together with the right connection string parameters in MySQL (so that it doesn't change always isolation level) can boost performance quite a lot
Reply
gmarques
31 January 2013
Hello, Thank you for your feedback. Can you provide reference documentation on that assumption?
Reply
Add Comment
Display name
Email
Comment
 
When inserting code please use <pre class="prettyprint">
Example:
<pre class="prettyprint">
int x = 1;
</pre>
 
Please insert the following characters in the box below:
  All fields are required
 
Notify me by email on subsequent replies to this post
 
Submit Comment
Comment registered
Your comment will be approved before
showing up on this page!
Rate this tutorial
Poor
Excellent
You are rating this tutorial as:
-
Please insert the following characters in the box below:
Subscribe
Subscribe and receive the latest tutorials by email
Popular tags
Archive
 
About the author
Gonçalo Marques is a Software Engineer with 7+ years of experience in software development and architecture design. During this period his main focus was delivering software solutions both in banking and telecommunications area. He created the Bytes Lounge website with one ultimate goal: share his knowledge with the software development community. His main area of expertise is Java and open source.

He is also the author of the WiFi File Browser Android application: