Concurrency is mainly a problem that can occur when we have a resource that is held by different persons.
If multiple process or multiple users wants to access the same resource then which one to allow and which one to deny needs a mechanism to be followed.
Problems
Short description
Explanation
Dirty reads
At the time when record read one transaction which is a part of half finished work of other transaction then the "Dirty Read" occurs.
• User A and user B are seeing value as “5”.
• User B changes the value “5” to “2”.
• User A is still seeing the value as “5”….Dirty read has happened.
Unrepeatable read
An “Unrepeatable Read” is a problem in which you get different values in every data read.
• User A is seeing value as “5”.
• User B changes the value”5” to “2”.
• User A refreshes see values “5”, he is surprised….unrepeatable read has happened.
Phantom rows
If “UPDATE” and “DELETE” SQL statements does not affect the data then it can be “Phantom Rows” problem.
• User A updates all value “5’ to “2”.
• User B inserts a new record with value “2”.
• User A selects all record with value “2’ if all the values have changed, he is surprised to still find value “2” records…..Phantom rows have been inserted.
Lost updates
"Lost Updates" are scenario where one updates which is successfully written to database is overwritten with other updates of other transaction.
• User A updates all value form “5” to “2”.
• User B comes and updates all “2” values to “5”.
• User A has lost all his updates.
Solution to the concurrency problem
Pessimistic and Optimistic approach needs to be implemented
Optimistic Approach
Record the current timestamp.
• Start changing the values.
• Before updating check if anyone else has changed the values by checking the old time stamp and new time stamp.
• If it’s not equal rollbacks or else commit.
Pessimistic Approach
Isolation is been done between the different layers of the application and the database
0 Comment(s)