我在arangodb.beginTransaction()
上遇到了一些困难。在我的测试中,创建事务、通过trx.run()
API调用函数然后中止trx
并不会回滚数据库中的数据更改。我不清楚发生了什么,而且文档非常稀少。有些文件是相当不祥的,但也非常模糊:
如果给定的函数包含异步逻辑,则只有函数的同步部分将在事务中运行。例如,当使用异步/等待时,只有到第一个等待的代码才会在事务中运行。
在被调用的异步函数中嵌套的异步/等待怎么办?例如,如果我有这个:
async runQuery(query) {
try {
const cursor = await this.arangodb.query(query)
return cursor.all()
} catch (error) {
logger.error('ArangoDB query failed', { stack: error })
throw error
}
}
async touchDocument(id, revision) {
const type = await this.getObjectTypeFromId(id)
const collection = await this.getCollection(type, false, true)
const idCollection = await this.getCollection(ID_COLLECTION, false, true)
const touchAql = aql`
LET permanentDocId = DOCUMENT(${idCollection}, ${id}).docId
LET permanentDoc = MERGE( DOCUMENT(permanentDocId), { _rev : ${revision} })
UPDATE permanentDoc WITH permanentDoc in ${collection} OPTIONS { ignoreRevs: false }
RETURN NEW
`
return this.runQuery(touchAql)
}
trx.run(() => this.touchDocument(parentId, parentRevision))
this.arangodb.query()
是否会在事务中执行?
发布于 2020-01-22 12:52:22
我是阿兰戈斯的作者。对于后人,我想澄清的是,这个答案是关于arangojs 6的,这是在撰写本文时的arangojs的当前版本,以及第一次增加对流事务的支持的版本。API在将来的版本中可能会改变,尽管目前还没有这样做的计划。
由于事务是如何工作的(从arangojs 6开始),您需要特别注意documentation for the run
method中提到的警告
如果给定的函数包含异步逻辑,则只有函数的同步部分将在事务中运行。例如,当使用异步/等待时,只有到第一个等待的代码才会在事务中运行。注意下面的例子。
换句话说,如果您只是将async
函数包装在trx.run
中,那么它们的行为可能会不正确。我建议将事务对象传递给每个函数,并在trx.run
中包装这些函数中的方法调用。
例如:
async runQuery(query, trx) {
try {
const cursor = await trx.run(() => this.arangodb.query(query))
return trx.run(() => cursor.all())
} catch (error) {
logger.error('ArangoDB query failed', { stack: error })
throw error
}
}
async touchDocument(id, revision, trx) {
const type = await this.getObjectTypeFromId(id, trx)
const collection = await this.getCollection(type, false, true, trx)
const idCollection = await this.getCollection(ID_COLLECTION, false, true, trx)
const touchAql = aql`
LET permanentDocId = DOCUMENT(${idCollection}, ${id}).docId
LET permanentDoc = MERGE( DOCUMENT(permanentDocId), { _rev : ${revision} })
UPDATE permanentDoc WITH permanentDoc in ${collection} OPTIONS { ignoreRevs: false }
RETURN NEW
`
return this.runQuery(touchAql, trx)
}
this.touchDocument(parentId, parentRevision, trx)
原因是trx.run
将整个驱动程序设置为“事务模式”,执行给定函数,然后在执行“事务模式”后禁用“事务模式”,这样就不会意外地在事务中运行不相关的代码。
这种方法的缺点是,如果函数是异步的,并且包含多个await
语句,那么事务中只会运行导致和包含第一个await
的代码。在您的代码中,意思是在this.getObjectTypeFromId(id)
返回后禁用“事务模式”。如果该方法本身包含多个await
表达式,那么同样只有第一个表达式将是事务的一部分(等等)。
我希望这能消除一些混乱。
https://stackoverflow.com/questions/59844658
复制相似问题