By making the update conditional on the current value we expect we * are implementing an optimistic lock: if the update results in * changes on the DB we know that the data_version did not change * under our feet and no other transaction ran in the meantime. * * Notice that this update effectively locks the row, so that other * operations attempting to change this outside the transaction wil
| 103 | * until our transaction is committed. |
| 104 | */ |
| 105 | static void db_data_version_incr(struct db *db) |
| 106 | { |
| 107 | struct db_stmt *stmt = db_prepare_v2( |
| 108 | db, SQL("UPDATE vars " |
| 109 | "SET intval = intval + 1 " |
| 110 | "WHERE name = 'data_version'" |
| 111 | " AND intval = ?")); |
| 112 | db_bind_int(stmt, db->data_version); |
| 113 | db_exec_prepared_v2(stmt); |
| 114 | if (db_count_changes(stmt) != 1) |
| 115 | db_fatal(stmt->db, "Optimistic lock on the database failed. There" |
| 116 | " may be a concurrent access to the database." |
| 117 | " Aborting since concurrent access is unsafe."); |
| 118 | tal_free(stmt); |
| 119 | db->data_version++; |
| 120 | } |
| 121 | |
| 122 | void db_begin_transaction_(struct db *db, const char *location) |
| 123 | { |
no test coverage detected