Google 数据存储事务乐观并发控制与否?

Google Datastore Transactions Optimistic Concurrency Control or not?

题目比较简单。

是否 Google 数据存储事务乐观并发控制?

一部分文档说它确实如此:

When a transaction starts, App Engine uses optimistic concurrency control by checking the last update time for the entity groups used in the transaction. Upon commiting a transaction for the entity groups, App Engine again checks the last update time for the entity groups used in the transaction. If it has changed since our initial check, an exception is thrown. Source

文档的另一部分表明它没有:

When a transaction is started, the datastore rejects any other attempts to write to that entity group before the transaction is complete. To illustrate this, say you have an entity group consisting of two entities, one at the root of the hierarchy and the other directly below it. If these entities belonged to separate entity groups, they could be updated in parallel. But because they are part of the same entity group, any request attempting to update one of the entities will necessarily prevent a simultaneous request from updating any other entity in the same group until the original request is finished. Source

据我了解,第一句话告诉我,如果我没有理由更新实体,则可以启动事务、读取实体并忽略关闭事务。

第二个引用告诉我,如果我开始一个事务并读取一个实体,那么我应该永远记得再次关闭它,否则我不能在同一个实体上开始一个新的。

文档的哪一部分是正确的?

顺便说一句。如果正确的报价是第二个,我将使用 Objectify 来处理我的所有交易。这会记得关闭所有已开始的事务,即使没有进行任何更改吗?

评论者 (Greg) 是正确的。无论您是否显式关闭事务,所有事务都会在请求结束时由容器关闭。您不能 "leak" 事务(尽管您可以在单个请求中搞砸事务)。

此外,使用 Objectify 的交易 API,当您执行一个 Work 单元时,交易会自动为您打开和关闭。您不自己管理交易。

回答您的根本问题:是的,GAE 数据存储中的所有事务都是乐观的。数据存储中没有悲观锁定;您可以在单个实体组上启动任意数量的事务,但只有第一次提交会成功。所有后续的提交尝试都将回滚 ConcurrentModificationException.