Sunday, 22 September 2019

what is repeatable read in spring

This is basically related to transaction management in spring  . interviewer are not directly asking about isolation from experienced people.

so this is one kind of Isolation.

Spring transaction isolation level 

Isolation level defines how changes are made to data repository in concurrent transactions and effect this concurrent transactions by some other simultaneous transaction, and also how and when that changed data becomes available to other transactions.
while defining transactions using spring framework  we are also able to configure isolation level for same transactions execution.

these isolation level are 

READ_UNCOMMITTED 
READ_COMMITTED
REPEATABLE_READ
SERIALIZABLE 
isolation_default


ISOLATION_DEFAULT: This is the default isolation level.

ISOLATION_READ_COMMITTED: this Isolation level states that a transaction  can't read data that is not yet committed by other transactions.(dirty reads are prevented, non-repeatable and phantom reads are allowed.)

ISOLATION_READ_UNCOMMITTED : this Isolation level states that a transaction may read data that is still uncommitted by other transactions ( dirty reads are allowed, no-repeatable and phantom reads are allowed.)

ISOLATION_REPEATABLE_READ: this isolation states  level states that if a transaction reads and records data from db multiple times the result for all those multiple reading must be the same always.
(This dirty reads and non-repeatable reads are prevented but phantom reads are allowed.)

ISOLATION_SERIALIZABLE : This isolation level is most restrictive among  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.

(dirty , non- repeatable reads and phantom reads are prevented.)


What do these Jargons dirty reads, phantom reads, or repeatable reads mean?

  • Dirty Reads: 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.
  • Non-Repeatable Reads: 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.
  • Phantom Reads: 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).

No comments:

Post a Comment

Note: only a member of this blog may post a comment.