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

Commit 07257ee3 by Joel Trost Committed by Matt Broadstone

Fixed pooling for mssql

1 parent e2d17106
......@@ -63,7 +63,6 @@ ConnectionManager.prototype.close = function () {
ConnectionManager.prototype.initPools = function () {
var self = this
, config = this.config;
if (config.replication) {
var reads = 0
, writes = 0;
......@@ -182,17 +181,12 @@ ConnectionManager.prototype.initPools = function () {
ConnectionManager.prototype.getConnection = function(options) {
var self = this;
options = options || {};
self.pool.isTransaction = options.transaction;
//TODO: dialect check
return new Promise(function (resolve, reject) {
resolve(self.$connect(self.config));
self.pool.acquire(function(err, connection) {
if (err) return reject(err);
resolve(connection);
}, options.priority, options.type);
});
// return new Promise(function (resolve, reject) {
// self.pool.acquire(function(err, connection) {
// if (err) return reject(err);
// resolve(connection);
// }, options.priority, options.type);
// });
};
ConnectionManager.prototype.releaseConnection = function(connection) {
var self = this;
......@@ -204,7 +198,7 @@ ConnectionManager.prototype.releaseConnection = function(connection) {
};
ConnectionManager.prototype.$connect = function(config) {
return this.dialect.connectionManager.connect(config, this.pool.isTransaction);
return this.dialect.connectionManager.connect(config);
};
ConnectionManager.prototype.$disconnect = function(connection) {
return this.dialect.connectionManager.disconnect(connection);
......
"use strict";
var AbstractConnectionManager = require('../abstract/connection-manager')
, Pooling = require('generic-pool')
, ConnectionManager
, Utils = require('../../utils')
, Promise = require('../../promise');
......@@ -9,6 +10,7 @@ ConnectionManager = function(dialect, sequelize) {
this.sequelize = sequelize;
this.sequelize.config.port = this.sequelize.config.port || 1433;
this.connection = null;
try {
this.lib = require(sequelize.config.dialectModulePath || 'mssql');
......@@ -19,9 +21,8 @@ ConnectionManager = function(dialect, sequelize) {
Utils._.extend(ConnectionManager.prototype, AbstractConnectionManager.prototype);
ConnectionManager.prototype.connect = function(config, isTransaction) {
ConnectionManager.prototype.connect = function(config) {
var self = this;
return new Promise(function (resolve, reject) {
var connectionConfig = {
host: config.host,
......@@ -37,37 +38,27 @@ ConnectionManager.prototype.connect = function(config, isTransaction) {
connectionConfig[key] = config.dialectOptions[key];
});
}
if(!self.connection){
//console.log('CONNECTION MADE', self.connection);
config = {
user: connectionConfig.user,
password: connectionConfig.password,
server: connectionConfig.host,
database: connectionConfig.database
database: connectionConfig.database,
pool: {
max: config.max,
min: config.min,
idleTimeoutMillis: config.idle
}
};
var connection = {
self.connection = {
config: config,
lib: self.lib
};
var conn;
self.lib._transaction = null;
if (isTransaction) {
conn = new self.lib.Connection(config, function (err){
var trans = new self.lib.Transaction(conn);
self.lib._transaction = trans;
trans.begin(function(err) {
if (err) {
reject(err);
return;
}
connection.context = conn;
//connection.transaction = trans;
resolve(connection);
});
});
} else {
conn = new self.lib.Connection(config, function(err) {
var conn = new self.lib.Connection(config, function(err) {
// var request = new self.lib.Request();
// request.query('select 1 as number', function(err, recordset) {
// console.log('err2', err);
......@@ -79,11 +70,13 @@ ConnectionManager.prototype.connect = function(config, isTransaction) {
return;
}
connection.context = conn;
resolve(connection);
self.connection.context = conn;
resolve(self.connection);
});
}else{
resolve(self.connection);
}
//console.log(self.connection);
// var connection = {
// config: {
// user: connectionConfig.user,
......@@ -97,6 +90,22 @@ ConnectionManager.prototype.connect = function(config, isTransaction) {
});
};
ConnectionManager.prototype.getConnection = function(options) {
var self = this;
options = options || {};
//TODO: dialect check
return new Promise(function (resolve, reject) {
resolve(self.$connect(self.config));
});
};
ConnectionManager.prototype.releaseConnection = function(connection) {
var self = this;
return new Promise(function (resolve, reject) {
//self.pool.release(connection);
resolve();
});
};
ConnectionManager.prototype.disconnect = function(connection) {
return new Promise(function (resolve, reject) {
resolve();
......
......@@ -5,10 +5,10 @@ var _ = require('lodash')
, ConnectionManager = require('./connection-manager')
, Query = require('./query');
//MSSQL uses a single connection that pools on its own
var MssqlDialect = function(sequelize) {
this.sequelize = sequelize;
this.connectionManager = new ConnectionManager(this, sequelize);
this.connectionManager.initPools();
};
MssqlDialect.prototype.supports = _.merge(_.cloneDeep(Abstract.prototype.supports), {
......
......@@ -113,14 +113,14 @@ Transaction.prototype.rollback = function() {
Transaction.prototype.prepareEnvironment = function() {
var self = this;
var conn = self.options.transaction ? self.options.transaction.connection : self.sequelize.connectionManager.getConnection({ uuid: self.id, transaction: true });
return Utils.Promise.resolve(conn)
.then(function (connection) {
return Utils.Promise.resolve(
self.options.transaction ?
self.options.transaction.connection :
self.sequelize.connectionManager.getConnection({ uuid: self.id })
).then(function (connection) {
self.connection = connection;
self.connection.uuid = self.id;
})
.then(function () {
}).then(function () {
return self.begin();
}).then(function () {
return self.setIsolationLevel();
......
......@@ -26,9 +26,10 @@ var qq = function(str) {
}
}
describe(Support.getTestDialectTeaser("Sequelize"), function () {
describe.only(Support.getTestDialectTeaser("Sequelize"), function () {
describe('constructor', function() {
if (dialect !== 'sqlite') {
//MSSQL already pools, this test is not relevent
if (dialect !== 'sqlite' && dialect !== 'mssql') {
it('should work with minConnections', function () {
var ConnectionManager = require(__dirname + '/../lib/dialects/' + dialect + '/connection-manager.js')
, connectionSpy = ConnectionManager.prototype.connect = chai.spy(ConnectionManager.prototype.connect);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!