英文:
Golang cassandra gocql transaction need a delay time to executed
问题
这段代码没有删除数据,我不知道为什么。
s.Query(
"INSERT INTO smth (id, name) VALUES (?,?)",
data.ID,
data.Name,
).ScanCAS(nil, nil, nil)
s.Query(
"DELETE FROM smth WHERE id = ? AND name = ?",
data.ID,
data.Name,
).Exec()
但是如果我添加一些延迟,像这样:
s.Query(
"INSERT INTO smth (id, name) VALUES (?,?)",
data.ID,
data.Name,
).ScanCAS(nil, nil, nil)
time.Sleep(time.Second)
s.Query(
"DELETE FROM smth WHERE id = ? AND name = ?",
data.ID,
data.Name,
).Exec()
数据将被删除。
有人知道如何在不添加延迟的情况下修复吗?
我正在使用github.com/gocql/gocql
。
英文:
This block of code does not delete the data, I don't know why.
s.Query(
"INSERT INTO smth (id, name) VALUES (?,?)",
data.ID
data.Name
).ScanCAS(nil, nil, nil)
s.Query(
"DELETE FROM smth WHERE id = ? AND name = ?",
data.ID
data.Name
).Exec()
but if I added some delay like so:
s.Query(
"INSERT INTO smth (id, name) VALUES (?,?)",
data.ID
data.Name
).ScanCAS(nil, nil, nil)
time.Sleep(time.Second)
s.Query(
"DELETE FROM smth WHERE id = ? AND name = ?",
data.ID
data.Name
).Exec()
The data will be deleted.
Does anyone know how to fix it without adding the delay?
I'm using github.com/gocql/gocql
答案1
得分: 1
Gocql查询是异步执行的,因此不能保证您的DELETE
在INSERT
之后执行。
无论如何,您的测试用例是无效的,因为在刚刚插入分区后立即删除它不是一个有效的用例。
另外,我觉得您的代码有问题。在插入数据时,您调用了Query.ScanCAS()
,但查询不是轻量级事务--它缺少条件IF EXISTS
/IF NOT EXISTS
。谢谢!
英文:
Gocql queries are executed asynchronously so there are no guarantees that your DELETE
is executed after the INSERT
.
In any case, your test case is invalid because immediately deleting a partition after it has just been inserted isn't a valid use case.
As a side note, your code appears to be wrong to me. You are calling Query.ScanCAS()
when you insert data but the query isn't a lightweight transaction -- it's missing the conditional IF EXISTS
/IF NOT EXISTS
. Cheers!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论