0

我正在使用 node-mysql-queues 来处理我的应用程序中的数据库事务。

for (lineitem in lineitems) {
        transaction.query("SELECT n from inventory WHERE productId = ?", [lineitem], function (err, rows) {
            if (err)
                transaction.rollback();
            var newN = rows[0].n - lineitems[lineitem].quantity;
            if (newN >= 0) {
                transaction.query("UPDATE inventory SET n = ? WHERE productId = ?", [newN, lineitem], function (err) {
                    if (err){
                        transaction.rollback();
                        console.log(err);
                    }
                    //here I want to commit if all updates were successfull!!!
                });
            }
        })
    }

正如您在代码中看到的,我不知道如何处理提交部分。如果它是同步的,那会很容易,但不知道 ro 是如何解决这个问题的。

感谢和问候

4

2 回答 2

1

使用async模块之类的东西很容易。

async.each(lineitems, performQuery, function(err) {
  if(err) {
    transaction.rollback();
    console.log(err);
    return;
  }

  transaction.commit();
});

function performQuery(lineitem, callback) {
  transaction.query("SELECT n from inventory WHERE productId = ?", [lineitem], function (err, rows) {
    if (err) return callback(err);

    var newN = rows[0].n - lineitems[lineitem].quantity;
    if (newN >= 0) {
      transaction.query("UPDATE inventory SET n = ? WHERE productId = ?", [newN, lineitem], function (err) {
        if (err) return callback(err);

        callback();
      });
    }
  });
}
于 2013-08-14T21:12:12.367 回答
0

我找到了解决我的问题的方法。由于我在进行选择然后根据选择结果进行更新时遇到问题,因此我实现了条件更新之类的东西。但请参阅我的代码:

mysql.getTransaction(function (err, transaction) {
    //For each item in the cart, call the performUpdate method
    //If an error occures, rollback the whole transaction
    async.each(lineitems, performUpdate, function (err) {
        if (err) {
            transaction.rollback();
            res.json(err.message);
            return;
        }
        //Since we are going to call another callback, we need to pause the transaction, else it would be committed automatically
        transaction.pause();
        //If the Updates were successfull, create an Order in MongoDB
        orderController.createMongoOrder(lineitems, req.session.cart.total, req.session.passport.user, function (err) {
            if (err) {
                //If there is a Problem with Mongo, cancel the transaction
                transaction.resume();
                transaction.rollback();
                res.json(err.message);
            } else {
                //Else commit the transaction and empty the cart
                transaction.resume();
                transaction.commit();
                req.session.cart = {
                    products: {},
                    count: 0,
                    total: 0
                };
                res.json("Order accepted!");
            }
        })
    });

    function performUpdate(lineitem, callback) {
        //This query can be seen as conditional update. If the number of articles in stock is not sufficient, there will be no affectedRows in the returned info message
        transaction.query("UPDATE inventory SET n = n -? WHERE productId = ? AND n >= ?", [lineitem.quantity, lineitem.id, lineitem.quantity],function (err, info) {
            if (err) {
                return callback(err);
            } else {
                //if for any item there is no affectedRow, this means the Updated failed. This should make the whole transaction roll back so we return an error to the callback
                if (info.affectedRows != 1) {
                    return callback(new Error("Article: " + lineitem.productObject.name + " out of stock!"))
                }
                return callback(null, info);
            }
        }).execute()
    }
})
于 2013-08-16T08:41:16.277 回答