Is there a way in Java using MySQL connector, I can perform a read modify write atomically, i.e., I want to perform this:
// pseudocode
atomic
{
r = getRecord();
r = processRecord(r);
writeRecord(r);
}
I am a little hesitant to lock the entire table using mysql LOCK command because I can’t reliably unlock the contentious table. I am wondering if there is a better way to do this. Any help is much appreciated. Thanks!
By atomically, I’m assuming you mean transactionally? If you set the autocommit state on your database connection to false, and commit after each
writeRecord(r)then you’ll only be locking record r.If you want to ensure that r does not get changed between your
getRecord()andwriteRecord(r)methods by another process, then you couls apply a shared lock by locking in SHARED MODESomething along the lines of :