|
@@ -1,30 +1,21 @@
|
|
|
/**
|
|
|
- * Created with JetBrains WebStorm.
|
|
|
- * User: Gary
|
|
|
- * Date: 12-11-30
|
|
|
- * Time: 上午10:38
|
|
|
- * To change this template use File | Settings | File Templates.
|
|
|
*/
|
|
|
var Mysql = require('mysql');
|
|
|
-// var config = MysqlConfig;
|
|
|
+
|
|
|
var pool = function(config){
|
|
|
this.free = [];//空闲连接集合
|
|
|
this.used = 0; //已使用连接集合
|
|
|
- for(var key in config.pool)
|
|
|
- this[key] = config.pool[key];
|
|
|
+ for(var key in config.pool){this[key] = config.pool[key];}
|
|
|
+
|
|
|
this.newConnection = function(){
|
|
|
var con = Mysql.createConnection(config.db);
|
|
|
- this.free.push(con);
|
|
|
return con;
|
|
|
};
|
|
|
this.getConnection = function(){
|
|
|
var con = null;
|
|
|
if(this.used < this.maxconn){
|
|
|
if(this.free.length > 0){
|
|
|
- con = this.free[0];
|
|
|
- this.free.splice(0,1);
|
|
|
- if(!con)
|
|
|
- con = this.getConnection();
|
|
|
+ con = this.free.shift();
|
|
|
}else{
|
|
|
con = this.newConnection();
|
|
|
}
|
|
@@ -63,8 +54,10 @@ var client = {
|
|
|
*/
|
|
|
freeConnection: function(name,con){
|
|
|
var pool = this.pools[name];
|
|
|
- if(pool)
|
|
|
+ if(pool){
|
|
|
+ console.log(1)
|
|
|
pool.freeConnection(con);
|
|
|
+ }
|
|
|
},
|
|
|
/**
|
|
|
* 释放一个数据库所有连接
|
|
@@ -84,10 +77,10 @@ var client = {
|
|
|
}
|
|
|
};
|
|
|
exports.instance = function(config){
|
|
|
- if(client.pools.length < 1){
|
|
|
+ // if(client.pools.length < 1){
|
|
|
// for(var i = 0; i < config.length; i++){
|
|
|
client.pools[config.pool.name] = new pool(config);
|
|
|
// }
|
|
|
- }
|
|
|
+ // }
|
|
|
return client;
|
|
|
};
|