开发者

mysql ensure only one access at a time

开发者 https://www.devze.com 2023-03-12 21:19 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消