I'm connecting to a mysql-database by several threads in Java. Sometimes, the threads are reading and updating the same column of a database-table so that some inconsistency appears.
In Java there is a synchronized
keyword which limits the access to one ressource. Are there any possible limitation for the mysql-database? So that these inco开发者_开发问答nsistencies do not occur?
You should use transactions with the appropiate isolation level.
Simplified example:
BEGIN TRANSACTION
...
COMMIT
Mysql docs about transactions
Mysql docs about isolation levels.
Try using a version column, that way you can know if someone messed with your data while you work on it.
meaning: add a column named "updated" of type datetime, and whenever updating check that the updated you got is the same as the one on row in db, if they're not you know someone worked on your record.
精彩评论