依赖模块:
1. MysqL:https://github.com/felixge/node-mysql
2. async:https://github.com/caolan/async
(ps: async模块可换成其它Promise模块如bluebird、q等)
因为Node.js的MysqL模块本身对于事务的封装过于简单,而且直接使用会有很严重callback hell,故我们封装了两个方法,一个用来初始化sql & params,一个用来执行事务。
初始化sql & params:
如果你要执行多条sql语句,则需要:
var sql2 = "update ...";
sqlParamsEntity.push(_getNewsqlParamEntity(sql1,[]));
//...更多要事务执行的sql
然后我在我自己的dbHelper.js里封装了execTrans的函数,用来执行事务
module.exports = {
execTrans: execTrans,}
var pool = MysqL.createPool({
host: "MysqL host",user: "MysqL login user",password: "MysqL login pwd",database: "target db name",connectionLimit: 10,port: "MysqL db port",waitForConnections: false
});
function execTrans(sqlparamsEntities,callback) {
pool.getConnection(function (err,connection) {
if (err) {
return callback(err,null);
}
connection.beginTransaction(function (err) {
if (err) {
return callback(err,null);
}
console.log("开始执行transaction,共执行" + sqlparamsEntities.length + "条数据");
var funcAry = [];
sqlparamsEntities.forEach(function (sql_param) {
var temp = function (cb) {
var sql = sql_param.sql;
var param = sql_param.params;
connection.query(sql,param,function (tErr,rows,fields) {
if (tErr) {
connection.rollback(function () {
console.log("事务失败," + sql_param + ",ERROR:" + tErr);
throw tErr;
});
} else {
return cb(null,'ok');
}
})
};
funcAry.push(temp);
});
async.series(funcAry,function (err,result) {
console.log("transaction error: " + err);
if (err) {
connection.rollback(function (err) {
console.log("transaction error: " + err);
connection.release();
return callback(err,null);
});
} else {
connection.commit(function (err,info) {
console.log("transaction info: " + JSON.stringify(info));
if (err) {
console.log("执行事务失败," + err);
connection.rollback(function (err) {
console.log("transaction error: " + err);
connection.release();
return callback(err,null);
});
} else {
connection.release();
return callback(null,info);
}
})
}
})
});
});
}
这样就可以执行事务了: