Hibernate Interview Questions – Part 6
Implementing Concurrency

What is Concurrency?
Each transaction is Hibernate is isolated from another transaction. So one transaction is not aware of another transaction. Which results in high possibility of a same record in database may be accessed by two or more transactions at same time. When multiple transactions access the same record in database and make changes concurrency issue occurs as data becomes inconsistent.

Concurrency Issues:
Dirty Read:
A transaction reads the data that has not been committed by another transaction

Lost Updates:
When two or more transactions try to access same row this issue occurs. Transactions overwrite the data on same row again and again leading to loss of data manipulation

Nonrepeatable Read:
Transaction reads the same row more than once and another transaction changes the data in the row between the successive reads of the transaction

Phantom Read:
A transaction reads a rowset more than once and another transaction inserts or deletes a row between the successive reads.


How to Control Concurrency Issues?
Setting transaction isolation levels
Implementing versioning
Implementing Locks


Setting Transaction Isolation Levels:
Read Uncommitted: 

In this level, a transaction can read the data modified by another transaction. But a transaction can read the data modified by another transaction even before the commit is made. Dirty Reads, Lost Updates, Nonrepeatable Read and Phantom read all issues occur at this level
Isolation Level – 1

Read Committed:
This is the default Isolation level in SQL database. In this level, When a transaction modifies a data other transactions will not be able to read the modified data. This prevents Dirty Reads but Lost Updates, Nonrepeatable Read and Phantom Read occurs at this level
Isolation Level – 2

Repeatable Read:
In this level, When a transaction is modifying a data no other transactions can Read or Update the data until the current transaction completes it process. At this level, Dirty Reads, Lost Updates and Nonrepeatable Read are prevented but Phantom Read may occur
Isolation Level – 4

Serializable:
In this level, When a transaction is modifying a data no other transaction can read, update, modify or insert data until the current transaction completes its process and commits.
Isolation Level – 8

1


Implementing Versioning:
In this method, version information is added to the persistent entities. Each entity has a version that represents the state of that entity. These version information are stored in a column in a database or timestamp that shows the time the data last modified.

How this Works?
When a record is modified in the database, hibernate increments the version number or timestamp of that record. So when another transaction tries to update the data, Hibernate will check for the version number or timestamp with the version or Timestamp of the transaction and throws exception it the transaction carries older version number


Implementing Locks:
We can implement the Locks when data’s are retrieved from database or when updating in database. Session interface provides lock() method.

LockMode.READ:
This acquires  lock when record is read from database

LockMode.UPGRADE:
This acquires database-level lock for persistent objects that are going to update.

LockMode.WRITE:
This acquires  lock when data is updated or inserted


 

By Sri

Leave a Reply

Your email address will not be published. Required fields are marked *