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.
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.
We will see each
isolation level in detail in the next sections.
1. 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:
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.
2. 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:
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.
3. 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:
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).
4. 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.
5. DEFAULT
DEFAULT isolation level, as the name states, uses the
default isolation level of the datastore we are actually connecting from our
application
[Spring Interview Questions]
We recommend you take Big Data Hadoop class room training at eMexo Technologies in electronic city, Bangalore to learn more about Big Data Hadoop.
0 Comments:
Post a Comment