I have a table with the following fields - list_name, version, list, message, creator
. Each update on the table for a specific list_name
essentially adds a new version of that list_name to the table along with the updated list.
My update query prepared statement looks like:
"INSERT INTO tableA (list_name, version, list, message, creator)"
+ " SELECT tableA.list_name, tableA.version+1, ?, ?, ? FROM tableA ta"
+ " JOIN (SELECT MAX(version) version, list_name FROM tableA WHERE list_name=? GROUP BY list_name) max_version"
+ " ON ta.version=max_version.version and ta.list_name=max_version.list_name"
+ " WHERE ta.version=?";
Consider a scenario where one client A reads version 1 as the latest version and tries to insert a new row with version 2. Meanwhile another client B sees version 1 as the latest as well and effectively overwrites the updates of client A. How to avoid such races? I read somewhere that SELECT FOR UPDATE
causes serious performance issues. Is there an alternative approach for my use case?