What method can be used to ensure atomicity during batch business operations? For example, if you delete multiple articles, but one of them has been deleted, and an error occurs here, how can you roll back the entire operation and locate the error message. Database transactions guarantee atomicity, but cannot locate error information. What method can be used to ensure atomicity during batch business operations?
For example, if you delete multiple articles, but one of them has been deleted, and an error occurs here, how can you roll back the entire operation and locate the error message.
Database transactions guarantee atomicity but cannot locate error information. What should we do if we encounter a scenario where transactions cannot be used.
Reply content:
What method can be used to ensure atomicity during batch business operations?
For example, if you delete multiple articles, but one of them has been deleted, and an error occurs here, how can you roll back the entire operation and locate the error message.
Database transactions guarantee atomicity but cannot locate error information. What should we do if we encounter a scenario where transactions cannot be used.
- It is most reasonable to use database transactions. The error information can be recorded. An error is thrown when an operation fails.
- The application logic ensures that each operation is recorded once, And all successful failures are recorded. If an error occurs in the middle, you can roll back successfully. Generally, deletion is a false deletion, so it is easy. If the record is deleted, complete information must be recorded.
This is a good question. I look forward to your answers.
It is indeed a good problem.
The method I can think of is the same as @ star001007;
1. Create a db that supports transaction types
2. The physical deletion is not in the true sense, but a flag is set. For example, status = 1 indicates deletion, and 0 indicates normal.
3. Record deletion logs. These redundant information helps to recover records.
You can use the Transaction Features of the innodb table of mysql to implement it. See my case:
How to update, delete, and insert data in large batches to a database or send emails in large batches online?
How can I avoid a single requirement being executed multiple times? How can I ask my colleagues to help me with the same requirement?
The problem is, is there any atomicity for deleting the same table?
Mysql exampledelete from table where xxx=xxx
When the deletion is performed, it is logically ensured that an SQL statement is used to complete the deletion, and mysql can guarantee it by itself.
Deleting multiple records from different tables requires transactions.