During database processing, you always need to be exposed to data synchronization and modification issues. Sometimes we need to ensure business data synchronization during business processing (generally, this situation includes statistical fields, that is to say, You need to calculate the field of the subsequent value based on the previous value). For example, after two people query a record at the same time, add 1 to it and save it to the database, this may cause storage problems. Therefore, you need to synchronize the data. The following three methods can be used:
1. Add a version field, get this field during query, and Add 1 after modification. Other modifications in the future need to be compared. If they are different, the storage fails (additional comparisons are required)
2. Depending on the database mechanism, lock the data to be modified (this method will cause the database access bottleneck)
3. Use a composite SQL statement to modify the data during storage, for example, updata A tem1 set tem1.filed = (select tem2.filed + 1 from a tem2 where tem1.id = tem2.id) Where tem1.id = 1;