Hibernate Interview Questions – Part 6
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.
A transaction reads the data that has not been committed by another transaction
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
Transaction reads the same row more than once and another transaction changes the data in the row between the successive reads of the transaction
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
Setting Transaction Isolation Levels:
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
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
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
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
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
We can implement the Locks when data’s are retrieved from database or when updating in database. Session interface provides lock() method.
This acquires lock when record is read from database
This acquires database-level lock for persistent objects that are going to update.
This acquires lock when data is updated or inserted