I have a cassandra table1:
CREATE TABLE Policy.table1 (
name VARCHAR ,
date TIMESTAMP ,
version_num INT,
PRIMARY KEY (
name
)) WITH caching = 'all'
-- and memtable_flush_period_in_ms = 7200 ;
;
I need to implement optimistic locking on tis table. When we read a row from table1 we remember its version_num. And when we want to update this row we compare current version_num value and value that we remembered. Also we need to increment version_num on each update.
Problems:
We cannot put version_num into where clause, this will create an error: Bad Request: Non PRIMARY KEY version_num found in where clause:
update table where name = 'abc' and version = 3
We cannot set version_num as a part of primary key, because we need to update its value
Do you see any solution for this problem?
Cassandra does not use RDBMS ACID transactions with rollback or locking mechanisms, but instead offers atomic, isolated, and durable transactions with eventual/tunable consistency that lets the user decide how strong or eventual they want each transaction's consistency to be.
Optimistic locking , where a record is locked only when changes are committed to the database. Pessimistic locking , where a record is locked while it is edited.
In order to use optimistic locking, we need to have an entity including a property with @Version annotation. While using it, each transaction that reads data holds the value of the version property. Before the transaction wants to make an update, it checks the version property again.
Optimistic locking is a technique for SQL database applications that does not hold row locks between selecting and updating or deleting a row. The application is written to optimistically assume that unlocked rows are unlikely to change before the update or delete operation.
The solution was found there: Cassandra 2.0, Lightweight transactions http://www.datastax.com/documentation/cassandra/2.0/cassandra/dml/dml_ltwt_transaction_c.html
In case I execute query:
update table1 set version_num = 5 where name = 'abc' if version_num = 4;
I will receive a row with [applied]
column. This row contains boolean value: true
= update was successful, false
= in other case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With