本文介绍了ArangoDB Transactions-如何防止引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在查找可能当时不存在的特定文档时,如何防止ArangoDB在事务期间抛出异常?

How to prevent ArangoDB from throwing an exception during the Transaction when looking for a specific document which perhaps does not exist at that moment?

Nodejs将事务发送为一个阻止到ArangoDb进行处理。那很完美。我想将所有数学运算卸载到服务器上。

Nodejs sends the transaction in one block to ArangoDb where it gets processed. That's perfect. I want to unload all math to the server.

在事务处理期间,我想查看特定的Collection并检查是否存在文档,是否可以找到该文档,然后获取字段 balance,但是如果找不到Document或其字段,那么我不想抛出异常并且不想停止正在进行的事务。相反,我更想继续进行交易,我们为变量oldBalance分配了字符串'0'。

During the Transaction, I want to look at a specific Collection and check if a document exists, if document can be found, then get field 'balance', but if the Document cant be found or their field, then I dont want to throw an exception and dont want to stop the ongoing transaction. On the contrary, I much more want to proceed with the transaction, and we assign the variable oldBalance the string of '0'.

(供您参考:为收集写锁:在nodeJS端指定的用户)
,在这里您看到发送到ArangoDB的部分交易代码:

(for your information: there is a write lock for collection: 'user' specified on nodeJS side)and here you see part of transaction code sent to ArangoDB:

var db = require('internal').db;
// 1.) find specific document
var accountdoc = db.user.document('Johnny04'); // find doc by _key

查找文档,如果找不到具有该特定_key的文档,则会引发异常。那时,用户可能在集合中没有任何条目。在这种情况下,我们要假设他的余额为字符串 0。但是不幸的是已经抛出了异常。我更想像这样继续进行操作:

this throws an exception if that Document with that particular _key cant be found. At that time the user probably has no entry in the collection. In that case we want to assume his balance to be string '0'. But unfortunately an exception was thrown already. I much more want to proceed like the following:

//2.) calculate newBalance = oldBalance + additional
        if (accountdoc.error==true){ // document not found etc...
            var oldBalance='0';
            var documentExists = false;
        } else {
            var oldBalance=accountdoc.balance;
            var documentExists = true;
            var documentExistsID = accountdoc._id;
        }   


推荐答案

事务内部出现找不到文档错误,如下所示:

Can't you handle the "document not found" error inside the transaction like this:

function (params) {
  var db = require("org/arangodb").db;
  var accountdoc;

  // 1.) find specific document
  try {
    accountdoc = db.user.document('Johnny04'); // find doc by _key
  }
  catch (err) {
    // document not found etc.
    // TODO: rethrow exception if err is something different than "document not found"
  }

  // 2.) calculate newBalance = oldBalance + additional
  if (accountdoc === undefined) { // document not found etc...
    // create a new document with balance 0
    db.user.save({ _key: 'Johnny04', balance: '0' }); // note: if this fails, the transaction will throw
  } 
  else {
    // update the existing document
    var oldBalance = accountdoc.balance;
    var newBalance = oldBalance + 42;
    db.user.update('Johnny04', { balance: newBalance }); // note: if this fails, the transaction will throw
  }   
}

这篇关于ArangoDB Transactions-如何防止引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 20:59