Google Datastore节点api中是否存在乐观锁定? [英] Is there optimistic locking in Google Datastore node api?

查看:59
本文介绍了Google Datastore节点api中是否存在乐观锁定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对我的Google数据存储区实现乐观.

I need to implement optimistic on my google datastore kind.

我使用自己的密钥而不是生成的密钥,并使用google datastore node.js api.

I am using my own key rather than a generated one and using the google datastore node.js api.

作为插入,更新,向上插入似乎都委托给save()方法(根据文档),如果不小心,我很容易最终覆盖数据.

As insert, update, upsert all seem to de delegated to the save() method ( as per the docs) I can easily end up overwriting data if I am not careful.

因此,基本上,在进行任何插入或更新之前,我必须先尝试读取",然后在调用save()之前,我必须小心覆盖"正确的属性.

So basically I have to try a 'read' before doing any insert or update and then after that I have to be careful to 'overwrite' the correct properties before calling save().

如果我设法读取然后正确地覆盖"正确的字段,那么我仍然需要确保我是该实体的最新作者,因此我需要使用版本/时间戳字段进行某种乐观锁定.我在某处已经读到它是内置的,但在

If I manage to read and then properly 'overwrite' the correct fields I still need to be sure that I am the latest writer to that entity so I would need some sort of optimistic locking with a version/timestamp field. I have read somewhere that this is in built in but its not so explicit in the official docs at https://googlecloudplatform.github.io/google-cloud-node/#/docs/datastore/0.7.1/datastore?method=insert

有人能对此有所启发吗?

can someone shed some light on this ?

推荐答案

Cloud Datastore事务使用乐观锁定.

Cloud Datastore transactions use optimistic locking.

过程A:

  • T1->开始交易
  • T2->读取实体X
  • T4->写实体X
  • T5->提交交易

过程B:

  • T3->写实体X

在上述情况下,由于Cloud Datastore使用乐观锁定,因此由于实体X是在读取和写入之间写入的,因此进程A的事务将失败.

In the above scenario, since Cloud Datastore uses optimistic locking, Process A's transaction will fail since entity X was written between the Read and the Write.

注意: insert upsert update 都映射到 save ,但是必须明确传递方法通过可选的 method 字符串来执行正确的写入类型:

Note: insert, upsert, update all map to save, but have the method explicitly passed along via the optional method string so the correct type of write is performed: source

这篇关于Google Datastore节点api中是否存在乐观锁定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆