THIS FAMOUS Problem Is Known As A "Lost Update"

zhaozj2021-02-12  129

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 The Three Operations, The Operations Could Be Interleaves. The Follwing Order of Operations 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, instanceB 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 transaction 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 the new value of x to the database.5. Release The Lock ON.

IF Another Component Ran Concurrently With Ours, That Component Would Have To Wait Unsh WE Relinguished Our Lock, Which Would Give That Component Our Fresh Copy of X.

转载请注明原文地址:https://www.9cbs.com/read-6585.html

New Post(0)