Database Reference
In-Depth Information
Much like conditional inserts, conditional updates include relevant information from the
row in question if the update is not applied. In this case, it tells us what the actual version
in the row is; we see that it's the one that we set when we changed the location to New
York.
Optimistic locking and accidental updates
Our optimistic locking strategy also solves another problem: that of accidentally creating
or recreating rows when we think we're updating existing rows. By only allowing the up-
date to happen if the version column matches what we expect it to, we also implicitly
ensure that the row exists. For instance, let's see what happens if we try to modify a
nonexistent row with a conditional update:
UPDATE "users"
SET "location" = 'Denver, CO', "version" = NOW()
WHERE "username" = 'ivan'
IF "version" = ec0c1fb7-321f-11e4-8eeb-5f98e903bf02;
Happily, the update is not applied, so we don't unwillingly create a new row for this ivan
character:
Since there's no row to take a version value, Cassandra omits that information from the
feedback, simply telling us that the operation was not applied.
If you're not using optimistic locking, you can get the same effect by adding a boolean
column that is set to true for every row in the table, and performing conditional updates
based on the presence of true in that column.
Search WWH ::




Custom Search