0

I am trying to to create transaction in waterline but I am getting this error from OrientDB:

com.orientechnologies.orient.core.command.OCommandExecutorNotFoundException: Cannot find a command executor for the command request: sql.BEGIN

Here is my code:

 try {
  itemsModel.query("BEGIN", function(err) { if (err) {throw new Error(err);}
    itemsModel.update({id:items_ids,status:ACTIVE},{status:INACTIVE})
      .exec(function(err, INA_items){ if (err) {throw new Error(err);}
        if (INA_items.length != items_ids.length ) {throw new Error({err:RECORD_NOT_FOUND});}
        itemsModel.query("COMMIT", function(err) { if (err) {throw new Error({err:MSG.RECORD_NOT_FOUND,title:"ITEMS"});} });
      });
  });
}
catch(e){
  itemsModel.query("ROLLBACK", function(err) { 
    if (err) {return res.serverError(err);}
    return res.serverError(e);  
  });
}

I also checked BEGIN command directly in orientdb, but same error.

Dário
  • 2,002
  • 1
  • 18
  • 28
9me
  • 1,078
  • 10
  • 36

1 Answers1

1

The question as it stands is mixing several different concepts, I'll try to address one by one:

  1. Waterline's API itself does not support transactions (check issue #755);

  2. It looks like you are using sails-orientdb adapter and from it you are executing a raw SQL query;

  3. The SQL query you are executing in the second line of your example is just BEGIN and that's where OrientDB itself is throwing an error. A transaction SQL query must be something like:

    begin
    let account = create vertex Account set name = 'Luke'
    let city = select from City where name = 'London'
    let edge = create edge Lives from $account to $city
    commit retry 100
    return $edge
    

    Example taken from OrientDB docs.

  4. Alternatively you can use transactions in a more javascript style by making use of the Oriento DB object. Assuming you are using sails-orientdb you can get the Oriento DB object like this:

    var db = itemsModel.getDB();
    
    // using oriento syntax, you can do something like
    var tx = db.begin();
    tx.create({
      '@class': 'TestClass',
      name: 'item3'
    });
    return tx.commit()
    .then(function (results) {
      console.log(results.created.length);  // should be 1
    });
    

    Example taken from Oriento tests. Another good example can be found at Oriento's example folder.

Dário
  • 2,002
  • 1
  • 18
  • 28
  • Sorry. I am using waterline-orientdb. – 9me Mar 24 '15 at 17:49
  • Hey @Dário I am trying to follow 4 option above. but i am getting this execption `TypeError: undefined is not a function` on this line `var db = itemsModel.getDB();` – 9me Mar 25 '15 at 13:05
  • Hey @9me, are you using the latest version of `waterline-orientdb`? Also, is `itemsModel` a model as those returned by `waterline.initialize()`? You can check a working example of `.getDB()` on this link: https://github.com/appscot/waterline-orientdb/blob/master/test/integration-orientdb/tests/adapterCustomMethods/getDB_Server.js#L6 – Dário Mar 25 '15 at 15:31
  • Hi @Dário i am using global.WL_MODELS = all models in app.js ( main file) and i want to get DB object like WL_MODELS.users.getDB() but I got this error `TypeError: undefined is not a function at module.exports.DbHelper.getDB (node_modules\waterline-orientdb\lib\connection.js:412:12) at module.exports.adapter.getDB (node_modules\waterline-orientdb\lib\adapter.js:343:38) at self.(anonymous function) [as getDB] (node_modules\waterline\lib\waterline\query\adapters.js:35:29)` What am I doing wrong ? – 9me Mar 30 '15 at 10:17
  • Which version of sails-orientdb (it's no longer called waterline-orientdb) are you using? Regarding the error, `waterline-orientdb\lib\connection.js:412:12` is in `createEdge()` method ([connection.js:412](https://github.com/appscot/sails-orientdb/blob/master/lib/connection.js#L412)). Either you are not using the latest version or your error is happening somewhere else. – Dário Mar 30 '15 at 10:46
  • I am using version `0.10.42` – 9me Mar 30 '15 at 10:53
  • That's recent enough, if you look at connection.js code around [line 412](https://github.com/appscot/sails-orientdb/blob/v0.10.42/lib/connection.js#L412) you'll see the error you mentioned is coming from `.createEdge()`, so your problem doesn't seem to be related to `.query()` but somewhere else... – Dário Mar 30 '15 at 13:54