2016-05-03 23:52:03 +00:00
|
|
|
// Copyright IBM Corp. 2015,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback-connector-mysql
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
'use strict';
|
2015-05-18 19:02:25 +00:00
|
|
|
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) {
|
2016-08-10 18:41:03 +00:00
|
|
|
if (err) return cb(err);
|
|
|
|
if (isolationLevel) {
|
2015-05-18 19:02:25 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
};
|
2016-08-10 18:41:03 +00:00
|
|
|
}
|