简体   繁体   English

来自两个集合的带有updatedocuments的AQL查询是否作为事务运行?

[英]AQL query with updatingdocuments from two collections runs as transaction or not?

I have a question about transactions in arangodb, if I run below AQL query, will it be executed as one transaction or will it be separated into two transaction? 我对arangodb中的事务有疑问,如果我在AQL查询下运行,它将作为一个事务执行还是将其分为两个事务? My backend is php: 我的后端是php:

LET r1 = (FOR u IN Users UPDATE u WITH { status: "inactive" } IN Users)
LET r2 = (FOR b IN Blogs UPDATE b WITH { status: "inactive" } IN Blogs)
RETURN true

For now I am using transaction as arangodb documentation suggests (using javascript code), but if using AQL query is possible, I'd prefer to remove js code from my php code! 目前,我正在按照arangodb文档的建议使用事务(使用javascript代码),但是如果可以使用AQL查询,我希望从我的php代码中删除js代码!

If it's possible, do you suggest this solution for commiting transactions or using js way is preferred? 如果可能,您是否建议使用此解决方案来提交事务或使用js方法?

(edited from original) (摘自原文)

According to the docs: 根据文档:

Each UPDATE operation is restricted to a single collection, and the collection name must not be dynamic. 每个UPDATE操作都限于一个集合,并且集合名称不得为动态。 Only a single UPDATE statement per collection is allowed per AQL query, and it cannot be followed by read or write operations that access the same collection, by traversal operations, or AQL functions that can read documents. 每个AQL查询只允许每个集合有一个UPDATE语句,并且不能跟随访问同一集合的读取或写入操作,遍历操作或可以读取文档的AQL函数。

In order to trigger a transaction you need to do so explicitly . 为了触发交易,您需要明确地进行

There are no individual BEGIN, COMMIT or ROLLBACK transaction commands in ArangoDB. ArangoDB中没有单独的BEGIN,COMMIT或ROLLBACK事务命令。 Instead, a transaction in ArangoDB is started by providing a description of the transaction to the db._executeTransaction JavaScript function: 而是通过向db._executeTransaction JavaScript函数提供该事务的描述来启动ArangoDB中的事务:

db._executeTransaction(description);

For example: 例如:

db._executeTransaction({
  collections: {
    write: [ "users", "logins" ],
    read: [ "recommendations" ]
  }
});

So, to answer the question, you need to use a client library to trigger a transaction as it will not happen automatically. 因此,要回答该问题,您需要使用客户端库来触发事务,因为它不会自动发生。

That being said, the main benefit of document/graph databases is the horizontal scaling, sharding, and cluster abilities. 话虽如此,文档/图形数据库的主要好处是水平缩放,分片和群集功能。 If you absolutely need to rely on transactions you might want to rethink why you are using a document based database. 如果您绝对需要依赖事务,则可能需要重新考虑为什么要使用基于文档的数据库。 Eventual Consistency is typically good enough for a lot of use cases but in other cases you absolutely need ACID . 最终一致性通常足以满足许多用例,但在其他情况下,则绝对需要ACID A blog probably doesn't need ACID. 博客可能不需要ACID。

AQL in a single server environment is executed in an ACID fashion. 单个服务器环境中的AQL以ACID方式执行。 This is automatically within a single transaction without any further need of a separate _executeTransaction . 这是自动在单个事务中完成的,不需要任何其他_executeTransaction Therefore you can use the above AQL statement to update the two collections within a single AQL statement. 因此,您可以使用上述AQL语句来更新单个AQL语句中的两个集合。

There are some caveat to keep in mind: 请注意以下几点:

  • after the update statement for a collection you cannot use this particular collection in any further update or read statement 在集合的更新语句之后,您不能在任何进一步的更新或读取语句中使用该特定集合
  • if you enable intermediate commits then the update will no longer be atomic 如果启用中间提交,则更新将不再是原子的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM