如何使用nodejs在lambda函数中使用mysq事务提交回滚

kuuvgm7e  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(357)

嗨,我想在lambda(node)函数中使用mysql的begintransactio或transactio提交回滚功能。我尝试了mysql包的基本结构,但在lambda中似乎不起作用

const mysql = require('mysql');
exports.handler = async (event) => {
    const con = mysql.createConnection(
        {
            host: "host",
            user: "user",
            password: "*****",
            database: "db"
        }
    );
    con.beginTransaction(
        function (err) {
            con.query(
                "query goes here",
                function (err, status) {
                    if (err) {
                        con.rollback();
                        con.end();
                        return err;
                    } else {
                        con.commit();
                        con.end();
                        return true;
                    }
                })
        });
}
798qvoo8

798qvoo81#

抱歉,迟了答复。只需要指定begintransaction而不需要回调

const mysql = require('mysql');
exports.handler = async (event) => {
    const con = mysql.createConnection(
        {
            host: "host",
            user: "user",
            password: "*****",
            database: "db"
        }
    );
    con.beginTransaction(); //here i was declaring standard callback function with err parameter
            con.query(
                "query goes here",
                function (err, status) {
                    if (err) {
                        con.rollback();
                        con.end();
                        return err;
                    } else {
                        con.commit();
                        con.end();
                        return true;
                    }
                });
}

相关问题