This famous problem is known as a "lost update"

Each of the instances of the component performs the following steps:
1. Read an integer X from a database.
2. Add 10 to X.
3. Write the new value of X to the database.

If each these three steps exceutes together in an atomic operation, everthing
is fine. Neither instance can interfere with the other instance's operations.
Remember, though, that the thread-scheduling algorithm being used in the
backgroud does not guarentee this. If two instances are executing these three
operations, the operations could be interleaves. The follwing order of operations
is possible:

1. Instance A reads integer X from database. The database now contains X = 0.
2. Instance B reads integer X from database. The database now contains X = 0.
3. Instance A adds 10 to its copy of X and persists it to the database. The
   database now contains X = 10.
4. Instance B adds 10 to its copy of X and persists it to the database. The
   database now contains X = 10;
  
What happened here? Due to the interleaving of database operations, instance
B is working with a stale copy of X: The copy before instance A performed a
write. Thus, instance A's operations have been lost! This famous problem is
known as a "lost update"
. It is very serious situation-instance B has been
working with stale data and has overwritten instance A's write. How can
transactions avoid this scenario?

The solution to this problem is to use "locking" on the database to prevent the
two components from reading data. By locking the data your transaction is using,
you guarantee that your transaction and only your transaction has access to
that data until you release that lock. This prevents interleaving of sensitive
data operations.

In our scenario, if our component acquired an exclusive lock before the trans-
action began and released that lock after the transaction, then no interleaving
would be possible.

1. Request a lock on X.
2. Read an integer X from a database.
3. Add 10 to X.
4. Write then new value of X to the database.
5. Release the lock on X.

If another component ran concurrently with ours, that component would have to
wait until we relinguished our lock, which would give that component our
fresh copy of X.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值