我使用该事件在带有nodejs的mysql数据库中执行查询,前5次正常执行,但5次后返回错误“超出了锁等待超时;尝试重新启动事务”。发生了什么事,我该如何解决? 我使用Mysql数据库。
emitter.on('mysql.transact', function(data, callback) {
inTransaction(pool,
function(db, next) {
//console.log('======');
//console.log(data.q);
//console.log(data.v);
db.query(data.q, data.v, function(err) {
if (err) return next(err);
return callback(null,data);
})},
function(err) {
console.log("All done, transaction ended and connection released");
console.log(err);
}
);
});
function inTransaction(pool, body, callback) {
withConnection(pool, function(db, done) {
//console.log('123');
//console.log(db);
db.beginTransaction(function(err) {
if (err) return done(err);
body(db,finished)
})
// Commit or rollback transaction, then proxy callback
function finished(err) {
var context = this;
var args = arguments;
if (err) {
if (err == 'rollback') {
args[0] = err = null;
}
db.rollback(function() { done.apply(context, args) });
} else {
db.commit(function(err) {
args[0] = err;
done.apply(context, args)
})
}
}
}, callback)
}
function withConnection(pool, body, callback) {
pool.getConnection(function(err, db) {
if (err) return callback(err);
body(db, finished);
function finished() {
db.release();
callback.apply(this, arguments);
}
})
}
=========
这是我尝试使用nodejs执行的查询
var q = ' UPDATE data_alarm set HideAlarmFrom = NULL, '+
' HideAlarmText = NULL ' +
' Where VariableID in (select VariableID from dbo_v_reportvariables' +
' where ClientID = ? ' +
' and SiteID in (select SiteID from dbo_v_accesslist where UserID = ? ) ) ';
var v = [req.user.ClientID,req.user.UserID];
exp.emitter.emit('mysql.transact',{ q: q, v : v }, function(err, data) {
if (err) {
res.status(500).json({ error : err });
} else {
res.status(200).json(data);
}
});