20

谁能提供我如何在 Node.js 中实现 MySQL 事务的示例。我正在尝试使用 node-mysql 驱动程序和 node-mysql-queue。

据我所知,使用 node-mysql-queue 大大降低了 Node.js 的异步特性,因为新查询必须等到现有查询完成。为了解决这个问题,有没有人尝试将 node-mysql-queue 与 node-mysql 的连接池功能结合起来。即为每个新的http请求启动一个新的mysql连接,并在单个连接上启动事务队列?

4

7 回答 7

23

更新

请参阅下面的编辑以了解 async/await 语法


我花了一些时间写了node mysql给出的事务示例的通用版本,所以我想在这里分享一下。我使用 Bluebird 作为我的承诺库,并用它来“承诺”连接对象,这大大简化了异步逻辑。

const Promise = ('bluebird');
const mysql = ('mysql');

/**
 * Run multiple queries on the database using a transaction. A list of SQL queries
 * should be provided, along with a list of values to inject into the queries.
 * @param  {array} queries     An array of mysql queries. These can contain `?`s
 *                              which will be replaced with values in `queryValues`.
 * @param  {array} queryValues An array of arrays that is the same length as `queries`.
 *                              Each array in `queryValues` should contain values to
 *                              replace the `?`s in the corresponding query in `queries`.
 *                              If a query has no `?`s, an empty array should be provided.
 * @return {Promise}           A Promise that is fulfilled with an array of the
 *                              results of the passed in queries. The results in the
 *                              returned array are at respective positions to the
 *                              provided queries.
 */
function transaction(queries, queryValues) {
    if (queries.length !== queryValues.length) {
        return Promise.reject(
            'Number of provided queries did not match the number of provided query values arrays'
        )
    }

    const connection = mysql.createConnection(databaseConfigs);
    Promise.promisifyAll(connection);
    return connection.connectAsync()
    .then(connection.beginTransactionAsync())
    .then(() => {
        const queryPromises = [];

        queries.forEach((query, index) => {
            queryPromises.push(connection.queryAsync(query, queryValues[index]));
        });
        return Promise.all(queryPromises);
    })
    .then(results => {
        return connection.commitAsync()
        .then(connection.endAsync())
        .then(() => {
            return results;
        });
    })
    .catch(err => {
        return connection.rollbackAsync()
        .then(connection.endAsync())
        .then(() => {
            return Promise.reject(err);
        });
    });
}

如果您想按照问题中的建议使用池,您可以轻松地使用 切换createConnectionmyPool.getConnection(...),并使用 切换connection.endconnection.release()


编辑

我使用mysql2库(与承诺支持相同的 api mysql)和新的 async/await 运算符对代码进行了另一次迭代。这里是

const mysql = require('mysql2/promise')

/** See documentation from original answer */
async function transaction(queries, queryValues) {
    if (queries.length !== queryValues.length) {
        return Promise.reject(
            'Number of provided queries did not match the number of provided query values arrays'
        )
    }
    const connection = await mysql.createConnection(databaseConfigs)
    try {
        await connection.beginTransaction()
        const queryPromises = []

        queries.forEach((query, index) => {
            queryPromises.push(connection.query(query, queryValues[index]))
        })
        const results = await Promise.all(queryPromises)
        await connection.commit()
        await connection.end()
        return results
    } catch (err) {
        await connection.rollback()
        await connection.end()
        return Promise.reject(err)
    }
}
于 2017-07-24T16:07:43.540 回答
20

一个月前在文档中添加了以下交易示例:

https://github.com/felixge/node-mysql#transactions

connection.beginTransaction(function(err) {
  if (err) { throw err; }
  connection.query('INSERT INTO posts SET title=?', title, function(err, result) {
    if (err) { 
      connection.rollback(function() {
        throw err;
      });
    }

    var log = 'Post ' + result.insertId + ' added';

    connection.query('INSERT INTO log SET data=?', log, function(err, result) {
      if (err) { 
        connection.rollback(function() {
          throw err;
        });
      }  
      connection.commit(function(err) {
        if (err) { 
          connection.rollback(function() {
            throw err;
          });
        }
        console.log('success!');
      });
    });
  });
});
于 2013-10-26T03:39:13.153 回答
2

我正在使用以下方法。我的模型中有一个添加函数,我正在执行数据库操作。

  add : function (data, callback) {

    //Begin transaction
    connection.beginTransaction(function(err) {
        if (err) {
            throw err;
        }

        var user_query = "INSERT INTO `calldata`.`users` (`username`, `password`, `enabled`, `accountNonExpired`, `accountNonLocked`, `credentialsNonExpired`) VALUES ('" + data.mobile + "', '" + sha1(data.password) + "', '1', '1', '1', '1')";
        connection.query(user_query, function(err, results) {
            if (err) {
                return connection.rollback(function() {
                    throw err;
                });
            }

            var accnt_dtls_query = "INSERT INTO `calldata`.`accnt_dtls` (`req_mob_nmbr`, `usr_nme`, `dvce_id`, `mngr_id`, `cmpny_id`, `actve_flg`, `crtd_on`, `usr`) VALUES (" + data.mobile + ", '" + data.name + "', '', " + data.managerId + ", " + data.companyId + ", 1, now(), '" + data.mobile+ "')";

            connection.query(accnt_dtls_query, function(err, results) {
                if (err) {
                    return connection.rollback(function() {
                        throw err;
                    });
                }
                var user_role_query = "INSERT INTO `calldata`.`user_roles` (`username`, `ROLE`) VALUES ('" + data.mobile + "', '" + data.role + "')";

                connection.query(user_role_query, function(err, result) {
                    if (err) {
                        return connection.rollback(function() {
                            throw err;
                        });
                    }

                    //add an entry to manager table
                    var mngr_dtls_query = "INSERT INTO `calldata`.`mngr_dtls` (`mngr_nm`, `cmpny_id`, `crtd_on`, `usr_nm`, `eml_id`) VALUES ('" + data.name + "'," + data.companyId + " , now(), '" + data.mobile + "', '" + data.mobile + "')";
                    connection.query(mngr_dtls_query, function(err, result) {
                        if (err) {
                            return connection.rollback(function () {
                                throw err;
                            });
                        }
                        console.log('Changed ' + result.changedRows + ' results');
                        connection.commit(function (err) {
                            console.log('Commiting transaction.....');
                            if (err) {
                                return connection.rollback(function () {
                                    throw err;
                                });
                            }

                            console.log('Transaction Complete.');
                            connection.end();
                            callback(null, result);
                        });
                    });
                });
            });
        });
    });
    //transaction ends here
}

并从控制器调用:

 agentAccountModel.add(data, function(err, results) {
                if(err)
                {
                    res.status(500);
                    res.json({
                        "status": 500,
                        "message": err
                    });
                }

                res.status(200);
                res.json({
                    "status": 200,
                    "message": "Saved successfully"

                });
            });
于 2017-06-20T05:59:15.280 回答
1

我想出了一个使用递归函数的解决方案。

var sql = 'INSERT INTO logs SET data = ?';

// array of rows to insert
var rows = [[/*first row*/], [/*additional row*/]];

connection.beginTransaction(function (err) {

    if (err) { 
        throw err; 
    }

    var insertEachRow = function () {

        var row = rows.shift();

        if (! row) {
            // Done, now commit
            return noMoreRows();
        }

        connection.query(sql, row, function (err, result) {
            if (err) { 
                connection.rollback(function () {
                    throw err;
                });
            }  

            insertEachRow();
        });
    };

    var noMoreRows = function () {
        connection.commit(function (err) {
            if (err) { 
                connection.rollback(function () {
                    throw err;
                });
            }
            console.log('success!');
        });
    };

    insertEachRow();
});
于 2017-01-10T19:48:58.597 回答
0

我为此特定目的创建了一个包装器 ORM 类型的东西,希望它有助于SQl-connecton - 池 ORM 类型辅助方法

于 2019-11-18T06:57:27.530 回答
0

您也可以将它与 mysql 一起使用,只需使用此函数而无需任何参数 /call back 。确保您具有异步功能并使用这些功能

await con.rollback();
await con.beginTransaction();
await con.commit();

生活设定......

于 2020-07-13T10:10:47.717 回答
0

我发现了一个有用的链接,它使用 node js mysql pooling with transaction。数据库连接池总是有用的。可以查看此链接

https://github.com/mysqljs/mysql

于 2018-02-05T14:15:57.233 回答