CREATE TABLE user_test (
id uuid,
firstname text,
lastname text,
randomfield text,
PRIMARY KEY (id)
)
val insertQuery: Insert = QueryBuilder.insertInto(keyspace, TABLE_NAME)
.using(ttl(86400))
.value("id", id)
if(firstName.isDefined) insertQuery.value("firstname", firstName.get)
if(lastName.isDefined) insertQuery.value("lastname", lastName.get)
if(randomField.isDefined) insertQuery.value("randomfield", randomField.get)
println(insertQuery)
session.executeAsync(insertQuery)
}
I have a test:
cassandra.insertUser(id, Some("Scrappy"), Some("Doo"), None)The query (as printed in console): INSERT INTO mykeyspace.user_test (id,firstname,lastname) VALUES (2918728c-bba7-433f-a063-6bb0a44850c2,'Scrappy','Doo') USING TTL 86400;
This is my update:
def updateUser(id: UUID, newFirstName: Option[String], newLastName: Option[String], randomField: Option[String]): ResultSetFuture = {
val updateQuery = QueryBuilder.update(keyspace, TABLE_NAME)
.where(QueryBuilder.eq("id", id))
if(newFirstName.isDefined) updateQuery.`with`(QueryBuilder.set("firstname", newFirstName.get))
if(newLastName.isDefined) updateQuery.`with`(QueryBuilder.set("lastname", newLastName.get))
if(randomField.isDefined) updateQuery.`with`(QueryBuilder.set("randomfield", randomField.get))
updateQuery.ifExists()
println(updateQuery)
session.executeAsync(updateQuery)
}
The update test:
cassandra.updateUser(id, Some("Scooby"), Some("Dooby Doo"), Some("this update found the id"))The query as printed in the console:
UPDATE mykeyspace.user_test SET firstname='Scooby',lastname='Dooby Doo',randomfield='this update found the id' WHERE id=2918728c-bba7-433f-a063-6bb0a44850c2 IF EXISTS;
cqlsh:mykeyspace> select id, firstname, writetime(firstname), lastname, writetime(lastname), randomfield, writetime(randomfield) from mykeyspace.user_test;
id | firstname | writetime(firstname) | lastname | writetime(lastname) | randomfield | writetime(randomfield)
--------------------------------------+-----------+----------------------+-----------+---------------------+--------------------------+------------------------
2918728c-bba7-433f-a063-6bb0a44850c2 | Scooby | 1502463404035000 | Dooby Doo | 1502463404035000 | this update found the id | 1502463404035000
Is it possible that the queries ran in very close succession in a multi-node environment where it's possible that the clocks aren't in sync between your cassandra nodes?
--
You received this message because you are subscribed to the Google Groups "DataStax Java Driver for Apache Cassandra User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-user+unsubscribe@lists.datastax.com.
Yes, this is exactly the case. The queries indeed ran in very close succession and we do have multiple nodes! Though I do not know how/if the clocks are being in sync. It might be possible that they aren't.
Lightweight transactions will block other lightweight transactions from occurring, but will not stop normal read and write operations from occurring. Lightweight transactions use a timestamping mechanism different than for normal operations and mixing LWTs and normal operations can result in errors. If lightweight transactions are used to write to a row within a partition, only lightweight transactions for both read and write operations should be used.
But there's one thing I don't understand. If the problem is caused by the fact that the clock's aren't in sync, then why the update without ifExists() works? Shouldn't it fail for simple update too?
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-us...@lists.datastax.com.
Another thing that I didn't think about at the time is that it usually not a good practice to mix light weight transactions (IF EXISTS, IF NOT EXISTS) with non-lightweight transactions.
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-user+unsubscribe@lists.datastax.com.