不要怂,就是干,撸起袖子干!

Commit 274beb7e by Joel Trost Committed by Matt Broadstone

Now supports separate connections for transactions

1 parent 54e12fcd
...@@ -13,7 +13,7 @@ ConnectionManager = function(dialect, sequelize) { ...@@ -13,7 +13,7 @@ ConnectionManager = function(dialect, sequelize) {
try { try {
this.lib = require(sequelize.config.dialectModulePath || 'mssql'); this.lib = require(sequelize.config.dialectModulePath || 'mssql');
} catch (err) { } catch (err) {
throw new Error('Please install mssql package manually'); throw new Error('Please install mssql package');
} }
}; };
...@@ -43,9 +43,13 @@ ConnectionManager.prototype.connect = function(config, isTransaction) { ...@@ -43,9 +43,13 @@ ConnectionManager.prototype.connect = function(config, isTransaction) {
server: connectionConfig.host, server: connectionConfig.host,
database: connectionConfig.database database: connectionConfig.database
}; };
var connection = {
config: config,
lib: self.lib
};
var conn;
if (isTransaction) { if (isTransaction) {
var conn = new self.lib.Connection(config, function (err){ conn = new self.lib.Connection(config, function (err){
var trans = new self.lib.Transaction(conn); var trans = new self.lib.Transaction(conn);
self.lib._transaction = trans; self.lib._transaction = trans;
...@@ -54,12 +58,13 @@ ConnectionManager.prototype.connect = function(config, isTransaction) { ...@@ -54,12 +58,13 @@ ConnectionManager.prototype.connect = function(config, isTransaction) {
reject(err); reject(err);
return; return;
} }
connection.context = conn;
resolve(self.lib); //connection.transaction = trans;
resolve(connection);
}); });
}); });
} else { } else {
self.lib.connect(config, function(err) { conn = new self.lib.Connection(config, function(err) {
// var request = new self.lib.Request(); // var request = new self.lib.Request();
// request.query('select 1 as number', function(err, recordset) { // request.query('select 1 as number', function(err, recordset) {
// console.log('err2', err); // console.log('err2', err);
...@@ -71,7 +76,8 @@ ConnectionManager.prototype.connect = function(config, isTransaction) { ...@@ -71,7 +76,8 @@ ConnectionManager.prototype.connect = function(config, isTransaction) {
return; return;
} }
resolve(self.lib); connection.context = conn;
resolve(connection);
}); });
} }
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
var Utils = require('../../utils') var Utils = require('../../utils')
, AbstractQuery = require('../abstract/query') , AbstractQuery = require('../abstract/query')
, uuid = require('node-uuid')
, sequelizeErrors = require('../../errors.js'); , sequelizeErrors = require('../../errors.js');
module.exports = (function() { module.exports = (function() {
...@@ -10,7 +9,6 @@ module.exports = (function() { ...@@ -10,7 +9,6 @@ module.exports = (function() {
this.connection = connection; this.connection = connection;
this.callee = callee; this.callee = callee;
this.sequelize = sequelize; this.sequelize = sequelize;
this.uuid = uuid.v4();
this.options = Utils._.extend({ this.options = Utils._.extend({
logging: console.log, logging: console.log,
plain: false, plain: false,
...@@ -32,12 +30,12 @@ module.exports = (function() { ...@@ -32,12 +30,12 @@ module.exports = (function() {
this.sql = sql; this.sql = sql;
if (this.options.logging !== false) { if (this.options.logging !== false) {
this.sequelize.log('Executing (' + this.connection.uuid + '): ' + this.sql); this.sequelize.log('Executing (' + (this.connection.uuid || 'default') + '): ' + this.sql);
} }
var promise = new Utils.Promise(function(resolve, reject) { var promise = new Utils.Promise(function(resolve, reject) {
console.log(self.sql); //console.log(self.sql);
// self // self
// .connection // .connection
...@@ -57,7 +55,7 @@ module.exports = (function() { ...@@ -57,7 +55,7 @@ module.exports = (function() {
var request, transCommand; var request, transCommand;
if (self.connection._transaction) { if (self.connection._transaction) {
request = new self.connection.Request(self.connection._transaction); request = new self.connection.lib.Request(self.connection.context._transaction);
if (self.sql === 'COMMIT TRANSACTION;') { if (self.sql === 'COMMIT TRANSACTION;') {
transCommand = 'commit'; transCommand = 'commit';
...@@ -67,7 +65,7 @@ module.exports = (function() { ...@@ -67,7 +65,7 @@ module.exports = (function() {
if (self.sql === 'COMMIT TRANSACTION;' || if (self.sql === 'COMMIT TRANSACTION;' ||
self.sql === 'ROLLBACK TRANSACTION;') { self.sql === 'ROLLBACK TRANSACTION;') {
self.connection._transaction[transCommand](function (err, result) { self.connection.context._transaction[transCommand](function (err, result) {
if (err) { if (err) {
console.log(err.message); console.log(err.message);
reject(self.formatError(err)); reject(self.formatError(err));
...@@ -78,7 +76,7 @@ module.exports = (function() { ...@@ -78,7 +76,7 @@ module.exports = (function() {
return promise; return promise;
} }
} else { } else {
request = new self.connection.Request(); request = new self.connection.lib.Request(self.connection.context);
} }
request.query(self.sql, function(err, recordset) { request.query(self.sql, function(err, recordset) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!