We are bound to encounter this situation: Bank A and Bank B almost simultaneously open your account and see your account on the original 1000 yuan deposit, and then both banks want to add 500 yuan deposit to your account. Then, bank A will change $1000 to $1500 and Bank B will change $1000 to $1500. That's a bad thing! Finally, your bank account last only 1500 yuan instead of the supposedly 2000 yuan, equals the loss of 500 yuan in vain! This is the serious problem caused by the modification without locking the data. However, we can solve this problem skillfully by time stamping.
Let's look at the idea:
In the Bank Account table, set the timestamp field timestamp to the text type varchar.
When bank a reads the deposit field in the Account table, it also reads the timestamp field, such as 123456.
When bank a modifies the value of the deposit, the previous read timestamp of 123456 is compared with the timestamp in the current table, and if it is consistent, it is allowed to save and then generate a new timestamp such as 456789 of the time stamp in the replacement table 123456.
What good would that do?
Let's look at the beginning of the situation: Bank A and Bank B almost simultaneously open your account and see your account on the original 1000 yuan deposit, while the two banks simultaneously read the timestamp 123456, then there is a difference, when bank a 1000 yuan into 1500 yuan, save, The system will compare the previous timestamp 123456 to the time stamp in the table when the disk was saved, obviously, it should now be consistent, then allow to save and generate a new timestamp 456789 to replace the old timestamp 123456. Next, bank B also changed 1000 yuan to 1500 yuan, save, the system compared to the previous timestamp 123456 is the same as the time stamp in the table, found that the previous timestamp 123456 has been different from the current time stamp 456789, the system refused to disk, the request to refresh the data, After the data refresh 1000 yuan already because before a bank deposited 500 yuan and became 1500 yuan, then Bank B will be on the basis of 1500 yuan to 2000 yuan, save again, the system allows. In this way, we avoid the error of repeating data changes!
Some like tongue twisters, hope everybody already understood my meaning ~
Finally, let's look at some of the operating code for timestamps in PHP.
Get time stamp
$timestamp =time ();
Echo $timestamp;
?>
Update the SQL statement for timestamp:
Update table name set field name = $timestamp WHERE condition = value;
Author: Sunec
Original download: Cenus Blog
All rights reserved. The author and original source and this statement must be indicated in the form of a link in the reprint.
http://www.bkjia.com/PHPjc/319129.html www.bkjia.com true http://www.bkjia.com/PHPjc/319129.html techarticle we are bound to encounter this situation: Bank A and Bank B almost simultaneously open your account and see your account on the original 1000 yuan deposit, and then both banks want to add 5 to your account ...