代码之家  ›  专栏  ›  技术社区  ›  9me

定向数据库和水线中的事务

  •  0
  • 9me  · 技术社区  · 11 年前

    我正在尝试在水线中创建事务,但我从OrientDB收到了此错误:

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

    这是我的代码:

     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);  
      });
    }
    

    我还检查了 BEGIN 命令直接在orientdb中执行,但错误相同。

    1 回复  |  直到 11 年前
        1
  •  1
  •   Dário    11 年前

    目前的问题是混合了几个不同的概念,我将尝试逐一解决:

    1. Waterline的API本身不支持事务(请检查 issue #755 );

    2. 看起来您正在使用 sails-orientdb 适配器,并从中执行 未经加工的 SQL查询;

    3. 在示例的第二行中执行的SQL查询 BEGIN 这就是OrientDB本身抛出错误的地方。事务SQL查询必须类似于:

      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
      

      示例取自 OrientDB docs .

    4. 或者,您可以在更多 javascript样式 通过使用 Oriento DB对象。假设您正在使用 帆定向db 您可以像这样获取Oriento DB对象:

      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
      });
      

      示例取自 Oriento tests 。另一个很好的例子可以在 Oriento's example folder .