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

Commit 07257ee3 by Joel Trost Committed by Matt Broadstone

Fixed pooling for mssql

1 parent e2d17106
...@@ -63,7 +63,6 @@ ConnectionManager.prototype.close = function () { ...@@ -63,7 +63,6 @@ ConnectionManager.prototype.close = function () {
ConnectionManager.prototype.initPools = function () { ConnectionManager.prototype.initPools = function () {
var self = this var self = this
, config = this.config; , config = this.config;
if (config.replication) { if (config.replication) {
var reads = 0 var reads = 0
, writes = 0; , writes = 0;
...@@ -182,17 +181,12 @@ ConnectionManager.prototype.initPools = function () { ...@@ -182,17 +181,12 @@ ConnectionManager.prototype.initPools = function () {
ConnectionManager.prototype.getConnection = function(options) { ConnectionManager.prototype.getConnection = function(options) {
var self = this; var self = this;
options = options || {}; options = options || {};
self.pool.isTransaction = options.transaction;
//TODO: dialect check
return new Promise(function (resolve, reject) { 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) { ConnectionManager.prototype.releaseConnection = function(connection) {
var self = this; var self = this;
...@@ -204,7 +198,7 @@ ConnectionManager.prototype.releaseConnection = function(connection) { ...@@ -204,7 +198,7 @@ ConnectionManager.prototype.releaseConnection = function(connection) {
}; };
ConnectionManager.prototype.$connect = function(config) { 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) { ConnectionManager.prototype.$disconnect = function(connection) {
return this.dialect.connectionManager.disconnect(connection); return this.dialect.connectionManager.disconnect(connection);
......
"use strict"; "use strict";
var AbstractConnectionManager = require('../abstract/connection-manager') var AbstractConnectionManager = require('../abstract/connection-manager')
, Pooling = require('generic-pool')
, ConnectionManager , ConnectionManager
, Utils = require('../../utils') , Utils = require('../../utils')
, Promise = require('../../promise'); , Promise = require('../../promise');
...@@ -9,6 +10,7 @@ ConnectionManager = function(dialect, sequelize) { ...@@ -9,6 +10,7 @@ ConnectionManager = function(dialect, sequelize) {
this.sequelize = sequelize; this.sequelize = sequelize;
this.sequelize.config.port = this.sequelize.config.port || 1433; this.sequelize.config.port = this.sequelize.config.port || 1433;
this.connection = null;
try { try {
this.lib = require(sequelize.config.dialectModulePath || 'mssql'); this.lib = require(sequelize.config.dialectModulePath || 'mssql');
...@@ -19,9 +21,8 @@ ConnectionManager = function(dialect, sequelize) { ...@@ -19,9 +21,8 @@ ConnectionManager = function(dialect, sequelize) {
Utils._.extend(ConnectionManager.prototype, AbstractConnectionManager.prototype); Utils._.extend(ConnectionManager.prototype, AbstractConnectionManager.prototype);
ConnectionManager.prototype.connect = function(config, isTransaction) { ConnectionManager.prototype.connect = function(config) {
var self = this; var self = this;
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
var connectionConfig = { var connectionConfig = {
host: config.host, host: config.host,
...@@ -37,37 +38,27 @@ ConnectionManager.prototype.connect = function(config, isTransaction) { ...@@ -37,37 +38,27 @@ ConnectionManager.prototype.connect = function(config, isTransaction) {
connectionConfig[key] = config.dialectOptions[key]; connectionConfig[key] = config.dialectOptions[key];
}); });
} }
if(!self.connection){
//console.log('CONNECTION MADE', self.connection);
config = { config = {
user: connectionConfig.user, user: connectionConfig.user,
password: connectionConfig.password, password: connectionConfig.password,
server: connectionConfig.host, 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, config: config,
lib: self.lib lib: self.lib
}; };
var conn;
self.lib._transaction = null; self.lib._transaction = null;
var conn = new self.lib.Connection(config, function(err) {
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 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);
...@@ -79,11 +70,13 @@ ConnectionManager.prototype.connect = function(config, isTransaction) { ...@@ -79,11 +70,13 @@ ConnectionManager.prototype.connect = function(config, isTransaction) {
return; return;
} }
connection.context = conn; self.connection.context = conn;
resolve(connection); resolve(self.connection);
}); });
}else{
resolve(self.connection);
} }
//console.log(self.connection);
// var connection = { // var connection = {
// config: { // config: {
// user: connectionConfig.user, // user: connectionConfig.user,
...@@ -97,6 +90,22 @@ ConnectionManager.prototype.connect = function(config, isTransaction) { ...@@ -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) { ConnectionManager.prototype.disconnect = function(connection) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
resolve(); resolve();
......
...@@ -5,10 +5,10 @@ var _ = require('lodash') ...@@ -5,10 +5,10 @@ var _ = require('lodash')
, ConnectionManager = require('./connection-manager') , ConnectionManager = require('./connection-manager')
, Query = require('./query'); , Query = require('./query');
//MSSQL uses a single connection that pools on its own
var MssqlDialect = function(sequelize) { var MssqlDialect = function(sequelize) {
this.sequelize = sequelize; this.sequelize = sequelize;
this.connectionManager = new ConnectionManager(this, sequelize); this.connectionManager = new ConnectionManager(this, sequelize);
this.connectionManager.initPools();
}; };
MssqlDialect.prototype.supports = _.merge(_.cloneDeep(Abstract.prototype.supports), { MssqlDialect.prototype.supports = _.merge(_.cloneDeep(Abstract.prototype.supports), {
......
...@@ -113,14 +113,14 @@ Transaction.prototype.rollback = function() { ...@@ -113,14 +113,14 @@ Transaction.prototype.rollback = function() {
Transaction.prototype.prepareEnvironment = function() { Transaction.prototype.prepareEnvironment = function() {
var self = this; 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(
self.options.transaction ?
return Utils.Promise.resolve(conn) self.options.transaction.connection :
.then(function (connection) { self.sequelize.connectionManager.getConnection({ uuid: self.id })
).then(function (connection) {
self.connection = connection; self.connection = connection;
self.connection.uuid = self.id; self.connection.uuid = self.id;
}) }).then(function () {
.then(function () {
return self.begin(); return self.begin();
}).then(function () { }).then(function () {
return self.setIsolationLevel(); return self.setIsolationLevel();
......
...@@ -26,9 +26,10 @@ var qq = function(str) { ...@@ -26,9 +26,10 @@ var qq = function(str) {
} }
} }
describe(Support.getTestDialectTeaser("Sequelize"), function () { describe.only(Support.getTestDialectTeaser("Sequelize"), function () {
describe('constructor', 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 () { it('should work with minConnections', function () {
var ConnectionManager = require(__dirname + '/../lib/dialects/' + dialect + '/connection-manager.js') var ConnectionManager = require(__dirname + '/../lib/dialects/' + dialect + '/connection-manager.js')
, connectionSpy = ConnectionManager.prototype.connect = chai.spy(ConnectionManager.prototype.connect); , 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!