loopback-connector-mysql/lib/transaction.js

63 lines
1.5 KiB
JavaScript

var debug = require('debug')('loopback:connector:mysql:transaction');
module.exports = mixinTransaction;
/*!
* @param {MySQL} MySQL connector class
* @param {Object} mysql mysql driver
*/
function mixinTransaction(MySQL, mysql) {
/**
* Begin a new transaction
* @param isolationLevel
* @param cb
*/
MySQL.prototype.beginTransaction = function(isolationLevel, cb) {
debug('Begin a transaction with isolation level: %s', isolationLevel);
this.client.getConnection(function(err, connection) {
if(err) return cb(err);
if(isolationLevel) {
connection.query(
'SET SESSION TRANSACTION ISOLATION LEVEL ' + isolationLevel,
function(err) {
if (err) return cb(err);
connection.beginTransaction(function(err) {
if (err) return cb(err);
return cb(null, connection);
});
});
} else {
connection.beginTransaction(function(err) {
if (err) return cb(err);
return cb(null, connection);
});
}
});
};
/**
*
* @param connection
* @param cb
*/
MySQL.prototype.commit = function(connection, cb) {
debug('Commit a transaction');
connection.commit(function(err) {
connection.release();
cb(err);
});
};
/**
*
* @param connection
* @param cb
*/
MySQL.prototype.rollback = function(connection, cb) {
debug('Rollback a transaction');
connection.rollback(function(err) {
connection.release();
cb(err);
});
};
}