ArangoDB 事务 - 如何防止抛出异常 [英] ArangoDB Transactions - How prevent from throwing exceptions

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

问题描述

如何防止 ArangoDB 在 Transaction 中查找当时可能不存在的特定文档时抛出异常?

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.

在交易过程中,我想查看特定的集合并检查文档是否存在,如果可以找到文档,则获取字段余额",但如果找不到文档或其字段,则我不想要抛出异常并且不想停止正在进行的事务.相反,我更想继续交易,我们为变量 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端指定了'user')在这里您可以看到发送到 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 事务 - 如何防止抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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