NodeJS - MySQL Transactions not working as expected

ぃ、小莉子 提交于 2020-05-17 06:22:07

问题


I am trying to apply Trasnactions (beginTransaction,rollback and commit. however if the query of adding invoice (Frst Query) executed successfully I want to update the supplier amount (Second Query). I intended to write wrong syntax in the second query by changing UPDATE to UPDATEEE. I assumed this should rollback. I got query syntax error message but the invoice added (first query successfully executed).

What is the wrong thing i am doing?

Invoice.addNewInvoice = function (invoice_data,result){

    sql.beginTransaction(function(err){
        if (err) { throw err; }

        sql.query('INSERT INTO invoice SET ?',invoice_data, function(err,res){
            if(err){
                sql.rollback(function() {
                    throw err;
                });
            }else{
                sql.query('UPDATEEEEE supplier SET supplier_amount = supplier_amount + ' + invoice_data.invoice_amount + ' WHERE supplier_id = ' + invoice_data.supplier_id, function(err,res){
                    if(err){
                        sql.rollback(function() {
                            throw err;
                        });
                    }
                })
                sql.commit(function(err) {
                    if (err) { 
                        sql.rollback(function() {
                            throw err;
                        });
                    }else{

                        result(null,res);
                    }
                });
            }

        });
    });
}

回答1:


The problem is that i set the commit to be executed even if the second query faild. it should be set on the else as below:

Invoice.addNewInvoice = function (invoice_data,result){

    sql.beginTransaction(function(err){
        if (err) { throw err; }

        sql.query('INSERT INTO invoice SET ?',invoice_data, function(err,res){
            if(err){
                sql.rollback(function() {
                    throw err;
                });
            }else{
                sql.query('UPDATEEEEE supplier SET supplier_amount = supplier_amount + ' + invoice_data.invoice_amount + ' WHERE supplier_id = ' + invoice_data.supplier_id, function(err,res){
                    if(err){
                        sql.rollback(function() {
                            throw err;
                        });
                    }else{
                        sql.commit(function(err) {
                            if (err) { 
                                sql.rollback(function() {
                                    throw err;
                                });
                            }else{

                                result(null,res);
                            }
                        });
                    }
                })

            }

        });
    });
}

This way it rollback since if the second query faild.



来源:https://stackoverflow.com/questions/61593344/nodejs-mysql-transactions-not-working-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!