I am trying to implement a simple row lock in SQL without the overhead of transactions or even the ‘hassle’ of version numbers
My tentative solution is the following:
query = 'UPDATE my_table SET lock_field = 1 WHERE lock_field = 0 AND user_id = 17'
if (query.execute() == 1){
// We own the lock, modify record at will
// Finally: 'UPDATE my_table SET lock_field = 0 WHERE user_id = 17'
} else {
// Somebody else owns the lock...
}
My question is: is this code safe? Does it guarantee that only a single ‘client’ can own the lock?
My confusion stems from being unable to find similar examples in google for something that seems much simpler than transactions or even optimistic locking with version numbers