For example, our site has many orders, but the management of the rights of the customer service has a lot of people.
How to prevent multiple people from editing at the same time
Reply content:
For example, our site has many orders, but the management of the rights of the customer service has a lot of people.
How to prevent multiple people from editing at the same time
Many states, edit the time, the state into the editor, the other people can not operate, completed and then put it state modification, this is relatively easy to operate.
A more scientific approach
1. The way to submit is Ajax
2. Order increase Last_update_dateline Word Gencun timestamp, update the field every time you modify
3. When the order is submitted, the timestamp is compared, if it is later than Last_update_dateline, the Ajax box prompts the user to have been modified, whether to refresh the loading of the new order content, or continue to submit.
get√~
This scenario, many people at the same time editing can not be avoided,
The solution can refer to the SVN practice, the user formally submitted before the user to tell users, he submitted the version and at this time in the server version of the difference between, let him confirm the submission, or continue to modify
Commit the time to make a recent confirmation to see if the data is being extracted with you, and use the latest data to indicate whether the user needs to overwrite
Two more easily implemented methods:
1. Set the permissions for the order editing operation. Can be divided by the type of order, time, region and so on;
2. Modify the display conditions of the order. If an order is being edited, there can be a unique status, at which point the order list does not show the order;
Supplement upstairs.
Record entry time when editing.
If someone submits the data after the time of entry, it is a hint that someone has just submitted it.
- ETAG determines whether the modified resource is up-to-date
- DB also joins the logic to prevent concurrent modifications (simply use a row lock to do so)
Just like the toilet, can only be used by one person at a time; Someone in the toilet closes the door; If anyone else wants to use them, they'll be lined up in the back. Some people rush in the queue?
This is easier to achieve.
Check the order status in the list of orders, if it is processing, the order is grayed out, customer service Click on the order after the status of the order to change the status of processing , customer service completed or canceled changes, order status changed to processed/not processed . Then there is the logical judgment of the details;
is not allowed to multi-person editing at the same time, or to solve multiple simultaneous editing conflicts. If it is not allowed to set an edit status of the order, the time stamp should be OK soon. If you are solving a multiplayer edit conflict, then you can emulate the GitHub branch, which is already the main branch, and each editor creates a branch that is merged into the main branch after editing is complete. It should be OK.
Using the lock mechanism
This problem landlord asked the relatively rough, the core is to solve the database is bound to lock, or do not have the concurrency caused by multiple customer service to read the same order and led to the situation of many people to edit the order.
Suppose the database is the Mysql,innodb engine, and the order table is orders,id as the primary key. All customer service first see a list of orders, and then customer service click on an order to enter the order modification page, to enter the order modification page of the PHP logic as follows:
1,update orders set status= "edit" where id=xx and status! = "in edit";
Check the return value after the UPDATE statement executes, if the return value is 1 (1 indicates that a row of data is updated), indicating that the update was successful, or that the order is being edited.
The above method has two main points:
The 1,update operation will lock the row by default, releasing the row lock after update execution is complete;
2,status! = "In edit" is important, assuming that a, B two customer service concurrent access, a first perform an update operation, then B will wait for a operation to complete to perform its own update operation, after waiting to find status= "edit", so b do nothing, Although the update operation was successful, the number of rows updated was 0;
Another situation is that there are many order systems that need to be modified randomly assigned to the customer service, that is, a lobbyist to enter a page will see a need to modify the order, and ask the customer service to see the order can not be repeated, the logic can be:
1,begin; (Start a transaction)
2,select * FROM orders where status! = "Edit" Limit 1 for update;
3, will get this order update operation set status = "in edit"
4,commit;
The point is that the 2nd step locks all Status! = "edit" order records, and other people need to wait for the transaction to be submitted before they can read the data. If the transaction wait time-out is short, you can set a bit longer, and if the operation is lost, you can read it again.
Using Memcache to implement locks