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

Commit ad7c3f08 by sdepold

Merge branch 'master' of https://github.com/megshark/sequelize into mysql-pooling

2 parents 35068ffb 9f8900f5
...@@ -13,24 +13,34 @@ module.exports = (function() { ...@@ -13,24 +13,34 @@ module.exports = (function() {
this.activeQueue = [] this.activeQueue = []
this.maxConcurrentQueries = (this.config.maxConcurrentQueries || 50) this.maxConcurrentQueries = (this.config.maxConcurrentQueries || 50)
this.poolCfg = this.config.pool this.poolCfg = this.config.pool
var self = this
if (this.poolCfg) { if (this.poolCfg) {
//the user has requested pooling, so create our connection pool //the user has requested pooling, so create our connection pool
if (!this.poolCfg.maxConnections) { if (!this.poolCfg.maxConnections) {
this.poolCfg.maxConnections = 10 this.poolCfg.maxConnections = 10
} }
var self = this
this.pool = Pooling.Pool({ this.pool = Pooling.Pool({
name: 'sequelize-mysql', name: 'sequelize-mysql',
create: function (done) { create: function (done) {
connect(self, done) connect.call(self, done)
}, },
destroy: function(client) { destroy: function(client) {
disconnect(null, client) disconnect.call(self, client)
}, },
max: self.poolCfg.maxConnections, max: self.poolCfg.maxConnections,
idleTimeoutMillis: self.poolCfg.maxIdleTime idleTimeoutMillis: self.poolCfg.maxIdleTime
}) })
} }
process.on('exit', function () {
//be nice & close our connections on exit
if (self.pool) {
self.pool.drain()
} else if (self.client) {
disconnect(self.client)
}
return
})
} }
Utils._.extend(ConnectorManager.prototype, require("../connector-manager").prototype) Utils._.extend(ConnectorManager.prototype, require("../connector-manager").prototype)
...@@ -56,7 +66,7 @@ module.exports = (function() { ...@@ -56,7 +66,7 @@ module.exports = (function() {
if (this.isConnecting || this.pool) { if (this.isConnecting || this.pool) {
return return
} }
connect(self, function(err, client) { connect.call(self, function(err, client) {
self.client = client self.client = client
return return
}) })
...@@ -65,12 +75,15 @@ module.exports = (function() { ...@@ -65,12 +75,15 @@ module.exports = (function() {
ConnectorManager.prototype.disconnect = function() { ConnectorManager.prototype.disconnect = function() {
if (this.client) if (this.client)
disconnect(this, this.client) disconnect.call(this, this.client)
return
} }
// private // private
var disconnect = function(self, client) { var disconnect = function(client) {
var self = this
client.end(function() { client.end(function() {
var intervalObj = null var intervalObj = null
var cleanup = function () { var cleanup = function () {
...@@ -92,7 +105,8 @@ module.exports = (function() { ...@@ -92,7 +105,8 @@ module.exports = (function() {
}) })
} }
var connect = function(self, done) { var connect = function(done) {
var self = this
var client = require("mysql").createClient({ var client = require("mysql").createClient({
user: self.config.username, user: self.config.username,
password: self.config.password, password: self.config.password,
...@@ -171,7 +185,7 @@ module.exports = (function() { ...@@ -171,7 +185,7 @@ module.exports = (function() {
} }
ConnectorManager.prototype.__defineGetter__('hasNoConnections', function() { ConnectorManager.prototype.__defineGetter__('hasNoConnections', function() {
return (this.queue.length == 0) && (this.activeQueue.length == 0) && this.client._queue && (this.client._queue.length == 0) return (this.queue.length == 0) && (this.activeQueue.length == 0) && (this.client == null || (this.client._queue && (this.client._queue.length == 0)))
}) })
ConnectorManager.prototype.__defineGetter__('isConnected', function() { ConnectorManager.prototype.__defineGetter__('isConnected', function() {
...@@ -189,3 +203,5 @@ module.exports = (function() { ...@@ -189,3 +203,5 @@ module.exports = (function() {
return ConnectorManager return ConnectorManager
})() })()
...@@ -3,13 +3,14 @@ module.exports = { ...@@ -3,13 +3,14 @@ module.exports = {
return parseInt(Math.random() * 999) return parseInt(Math.random() * 999)
}, },
//make maxIdleTime small so that tests exit promptly
mysql: { mysql: {
username: "root", username: "root",
password: null, password: null,
database: 'sequelize_test', database: 'sequelize_test',
host: '127.0.0.1', host: '127.0.0.1',
port: 3306, port: 3306,
pool: { maxConnections: 5, maxIdleTime: 30000} pool: { maxConnections: 5, maxIdleTime: 30}
}, },
sqlite: { sqlite: {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!