Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lost update in Concurrency control? [closed]

Tags:

concurrency

I have two transactions T and U which are executed simultaneously in a DB. How does one provide an example of the lost update problem?

We can assume that we have three accounts A,B,C and they each have £100,£200 and £300 respectively.

like image 445
CGF Avatar asked Dec 06 '25 02:12

CGF


2 Answers

The "lost update" problem relates to concurrent reads and updates to data, in a system where readers do not block writers. It is not necessary for the transactions to be exactly simultaneous.

  1. Session #1 reads Account A, gets 100.
  2. Session #2 reads Account A, gets 100.
  3. Session #2 updates Account A to 150 (+50) and commits.
  4. Session #1 updates Account A to 120 (+20) and commits.

In this scenario, because Session #1 does not know that another session has already modified the account, the update by Session #2 is overwritten ("lost").

There are several ways to solve this, e.g. version numbers or before-and-after compares.

like image 165
Jeffrey Kemp Avatar answered Dec 09 '25 15:12

Jeffrey Kemp


(3) Update Account A to 150 Where Account is 100 -> Account A is now 150

(4) Update Account A to 120 Where Account is 100 -> Update fail because account A is 150 and not 100

like image 34
Steen A Avatar answered Dec 09 '25 17:12

Steen A



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!