简体   繁体   English

交易在Arangodb

[英]transactions in Arangodb

I have some problem with transactions in ArangoDB+nodejs. 我在ArangoDB + nodejs中的事务有一些问题。 I need to do something like this: 我需要做这样的事情:

transaction
{ 
      insertedId=insertItemOneInDB();
     insertItemTwoInDB();
}

but when the second insert failed, the first one didn't rollback! 但是当第二次插入失败时,第一个不会回滚! please help me with an example! 请帮我举个例子!

here is my code: 这是我的代码:

var transaction = function (collections,params,callback)
{
    try
    {

        db.transaction.submit("user_merchant_tbl",params,
            function () 
            {
                console.log("_collections:",collections);
                console.log("params:");
                console.log(params);
                console.log("---");
                console.log("+==========+");

                //
                var insertedDataId;
                var relationsArrayIds=[];
                db.document.create(collections,params.data).then(function(_insertedId)
                {   
                    insertedDataId=_insertedId;
                }
                ,function(err)
                { 
                    console.log("ERROR: Arango--insert-->err: %j", err);
                    //throw "Error: "+err;
                    return false; 
                });

                /////
                var relations=params.relations;
                for(var i=0;i<relations.length;i++)
                {
                    db.document.create(relations[i].edge,relations[i].data).then(
                    function(_id)
                    {   
                        relationsArrayIds.push(_id);
                        next(true);
                    }
                    ,function(err)
                    { 
                        console.log("ERROR: Arango--insert.edge-->err:23232 %j", err);
                        console.log("after return");
                        next(false); 
                        return false
                    });
                }

                console.log("transaction before true"); 

                function next(result)
                {
                    if(result==true)
                    {
                        console.log("transaction is ok:",result);
                        callback(insertedDataId,result);
                    }
                    else 
                    {   
                        console.log("transaction is not OK:",result);
                        callback(insertedDataId,false);
                    }
                }
            }
            );
    }
    catch(e)
    {
        console.log("catch->error in -->Arango.transaction: ",e);
    }
}

first of all there seems to be a misunderstanding in how to write the action that is supposed to be executed. 首先,在如何编写应该执行的动作方面似乎存在误解。 This action is executed directly on the Database Server , hence you cant use any functionality provided by the Arango Javascript api. 此操作直接在数据库服务器上执行,因此您不能使用Arango Javascript api提供的任何功能。 If you want to design your action it has to run in the arango shell or on the server console (bin/arangod data --console) I took a look into your code and assume you want to store relations between users and merchants. 如果要设计动作,则必须在arango shell或服务器控制台(bin / arangod数据-控制台)上运行,我研究了您的代码,并假设您要存储用户与商家之间的关系。 As Arango comes with a nice graph module you could follow the following approach : 由于Arango附带了一个不错的图形模块,您可以采用以下方法:

// First we define a graph, containing of 2 document collections ("users" and "merchants")        and 2 edge collections (one per relation type, in this example "contactRequested" and "boughtSomethingFrom".
// Note that in this definition the relation "boughtSomethingFrom" is only allowed from a user to a merchant. Of course this is just one way to design it, you have to do it the way it suits you the best.
var edgeDefinitions = [{
  collection: "contactRequested",
  from: ["users", "merchants"],
  to: ["users", "merchants"]
}, {
collection: "boughtSomethingFrom",
from: ["users"],
to: ["merchants"]
}];

// Now we create a graph called "user_merchant_graph" and in the callback function execute a transaction
db.graph.create("user_merchant_graph", edgeDefinitions, function(err, ret, message) {

  // Lets define the action for the transaction, again this will be executed directly on the server ......
  var action = function (params) {

// We have to require the database module ....

var db = require("internal").db;
var relationsArrayIds = [];

// now we store the user provided to the function
var insertedUserId = db["users"].insert(params.data)._id;

var relations = params.relations;
// Now we loop over through the relations object, store each merchant and it's relations to the user
Object.keys(relations).forEach(function (relation) {
  // store merchant
  var insertedMerchantId = db["merchants"].insert({merchantName : relation})._id;
  // store relation as edge from "insertedUserId" to "insertedMerchantId".
  var edgeId = db[relations[relation].relation].insert(insertedUserId, insertedMerchantId, relations[relation].additionalData)._id;
  relationsArrayIds.push(edgeId);
});
  };
  // End of action

  var options = {};
  options.params = {
data: {
  userName : "someUserName",
  userSurname : "someUserSurname"
},
relations : {
  merchantA : {relation : "contactRequested", additionalData : {data :"someData"}},
  merchantB : {relation : "boughtSomethingFrom", additionalData : {data :"someData"}},
  merchantC : {relation : "contactRequested", additionalData : {data :"someData"}}
}
  };
  // Now we call the transaction module ...  a note to the collections parameter, it has to be an object containing the keys "write" and "read" which have a list of all collections as value into which the action is writing /reading from
  // This collections object is NOT available within your action, the only thing passed as argument to your action is "options.params" !!
  db.transaction.submit({write : ["users", "merchants", "contactRequested", "boughtSomethingFrom"]}, action, options, function(err, ret, message) {
//some callback
  });

});

With regards to transactions they are working, you can give this code a shot and if you fe mess up the storing of the edges (change it to "var edgeId = db[relations[relation].relation].insert(relations[relation].additionalData)._id;") you will see that your user and merchant have not been stored 关于它们正在运行的事务,您可以试一下一下此代码,如果您搞砸了边缘的存储(将其更改为“ var edgeId = db [relations [relation] .relation] .insert(relations [relation] .additionalData)._ id;“),您将看到您的用户和商家尚未存储

I hope this helps 我希望这有帮助

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

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