Permalink
Browse files

Merge pull request #3 from CarnegieLearning/master

Updated to use node-mysql 0.9.2
  • Loading branch information...
2 parents fe78825 + f60798d commit a631875f4a0a31ae19c71bc36ff19aa3816f5621 @Kijewski committed Aug 11, 2011
Showing with 17 additions and 65 deletions.
  1. +1 −0 .gitignore
  2. +14 −63 lib/mysql-pool/pool.js
  3. +2 −2 package.json
View
@@ -0,0 +1 @@
+node_modules
View
@@ -2,6 +2,7 @@
var util = require("util");
var EventEmitter = require("events").EventEmitter;
+var mysql = require("mysql");
function MySQLPool(properties) {
if(!(this instanceof MySQLPool)) {
@@ -10,8 +11,6 @@ function MySQLPool(properties) {
EventEmitter.call(this);
- this.poolSize = 1;
- this.Client = null;
this.properties = {};
// connections waiting for a query to execute
@@ -33,6 +32,14 @@ function MySQLPool(properties) {
}
}
+ this._populate();
+
+ for(var i = 0; i < this.poolSize; ++i) {
+ var client = mysql.createClient(this.properties);
+ this._connectionPool.push(client);
+ this._avail(client);
+ }
+
return this;
}
util.inherits(MySQLPool, EventEmitter);
@@ -47,65 +54,8 @@ MySQLPool.prototype._avail = function _avail(client) {
}
MySQLPool.prototype.connect = function connect(n, cb) {
- var pool = this;
- this._idleQueue = [];
- this._connectionPool = [];
-
- if(!this.Client) {
- this.Client = require('mysql').Client;
- }
- this._populate();
-
- if(typeof n == "function") {
- cb = n;
- n = undefined;
- }
-
- var poolSize = n || this.poolSize;
- var calledBack = 0;
- var availableConnections = [];
- var errors = [];
-
- function mkCallback(client) {
- return function(err) {
- if(err) {
- errors.push(err);
- --pool.poolSize;
- } else {
- availableConnections.push(client);
- pool._connectionPool.push(client);
- }
- if(++calledBack >= poolSize) {
- if(availableConnections.length > 0) {
- if(cb) {
- if(errors.length == 0) {
- cb(null, {connections:availableConnections.length});
- } else {
- cb(null, {connections:availableConnections.length, errors:errors});
- }
- }
- for(var i in availableConnections) {
- pool._avail(availableConnections[i]);
- }
- } else {
- var err = new Error("All connections failed.");
- if(cb) {
- cb(err, {errors:errors});
- } else {
- pool.emit("error", err);
- }
- }
- }
- };
- }
-
- for(var i = 0; i < poolSize; ++i) {
- var client = this.Client(this.properties);
- client.connect(mkCallback(client));
- }
-
- return this;
-}
+ throw new Error('deprecated: specify poolSize in constructor options.');
+};
MySQLPool.prototype._forEach = function _forEach(params) {
// TODO: callback _once_
@@ -191,9 +141,10 @@ MySQLPool.prototype._populate = function _populate() {
};
}
- for(var key in this.Client.prototype) {
+ var Client = mysql.Client;
+ for(var key in Client.prototype) {
if(!key.match(/^[_A-Z]/) && !(key in this) && !(key in EventEmitter.prototype)) {
- this[key] = mkPrototypeMethod(this.Client.prototype[key], key);
+ this[key] = mkPrototypeMethod(Client.prototype[key], key);
}
}
}
View
@@ -1,7 +1,7 @@
{
"name": "mysql-pool",
"description": "MySQL connection pool for node.js on top of node-mysql.",
- "version": "0.1.3",
+ "version": "0.2.0",
"homepage": "https://github.com/Kijewski/node-mysql-pool",
"repository": {
"type": "git",
@@ -16,6 +16,6 @@
"node": ">=0.4.0"
},
"devDependencies": {
- "mysql": ">=0.9.0"
+ "mysql": ">=0.9.2"
}
}

0 comments on commit a631875

Please sign in to comment.