Home > database >  Database locking problems or affairs of small white for help
Database locking problems or affairs of small white for help

Time:04-20

Is that from the MYSQL database. Innodb reads A data, then the corresponding operation. A, back to write the data to the database, because the operation involved for A long time, and will need other database operations support, such A database lock is easy to cause conflict, how to carry on the design, please?

CodePudding user response:

Design program, a transaction from the lock to submit, the shortest possible time, cannot have interactive operation among them, otherwise easy to cause conflict,
When design program, try to use optimistic locking (baidu), probably is: read the data for the first time without lock, interactivity, lock again when submit data, and then compare the current value and database for the first time to get the old values are consistent, if agree, that the data did not change after the first time you read, or have been modified, can't submit,

Select. * into the old value where id=XXX
,,,,,, interactive operation,,,,

Submitted:
 
Start the transaction
Select * into the current value where id=XXX for update
If the old value & lt;> The current value then
- the record when you modify it people have modified
The rollback
End the if
The update... Where id==new values set XXX XXX
Commit

Need more fields to see your business needs, generally is an important field, some database have the version number concept (e.g., postgresql), can compare with the version number,
  • Related