如何使用nodeJs在Lambda函数中使用mysq事务提交回滚

时间:2018-07-31 14:39:44

标签: mysql node.js lambda aws-lambda

嗨,我想在我的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;
                    }
                })
        });
}

1 个答案:

答案 0 :(得分:0)

对不起,答复延迟。 只需要指定无回调的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;
                    }
                });
}