node-mysql中的连接池代码学习
node-mysql是一个node.js下的mysql驱动,前段时间在处理连接池的问题上遇到了连接不释放的疑难杂症,虽已解决,但仍需总结经验避免下次重蹈覆辙。下面是node-mysql中的连接池的部分代码,我加入了详细日志,以作备忘之用。
/**
* 在连接池中获取Connection
* @param cb
* @returns {*}
*/
Pool.prototype.getConnection = function (cb) {
//本地加的日志
console.log("getConnection _allConnections.length: %j, _freeConnections.length: %j", this._allConnections.length, this._freeConnections.length); //池关闭检查
if (this._closed) {
return process.nextTick(function () {
return cb(new Error('Pool is closed.'));
});
} var connection;
//检查可用Connection,大于0直接从池中出栈并返回
if (this._freeConnections.length > 0) {
connection = this._freeConnections.shift();
return process.nextTick(function () {
return cb(null, connection);
});
} //检查连接数是否超过上限(默认10),没有超过则创建(真正的CreateConnection)
if (this.config.connectionLimit === 0 || this._allConnections.length < this.config.connectionLimit) {
connection = new PoolConnection(this, { config: this.config.connectionConfig }); //新创建的连接入栈池中
this._allConnections.push(connection); //验证新创建的Connection是否可用
return connection.connect(function (err) {
if (this._closed) {
return cb(new Error('Pool is closed.'));
}
if (err) {
return cb(err);
} this.emit('connection', connection);
return cb(null, connection);
}.bind(this));
} //检查是否允许排队等待(默认True),False时直接抛错
if (!this.config.waitForConnections) {
return process.nextTick(function () {
return cb(new Error('No connections available.'));
});
} //检查排队人数是否超过上限(默认0,无限)
if (this.config.queueLimit && this._connectionQueue.length >= this.config.queueLimit) {
return cb(new Error('Queue limit reached.'));
} //开始排队
this._connectionQueue.push(cb);
}; /**
* 释放Connection
* @param connection
*/
Pool.prototype.releaseConnection = function (connection) {
var cb;
//非连接池模式处理
if (!connection._pool) {
//如果有人排队
if (this._connectionQueue.length) {
//出栈一个排队回调
cb = this._connectionQueue.shift();
//调用getConnection返回Connection给予回调使用
process.nextTick(this.getConnection.bind(this, cb));
}
}
/**
*连接池模式处理 有人排队
*/
else if (this._connectionQueue.length) {
cb = this._connectionQueue.shift();
//将释放的connection直接给予排队列表的第一个人使用
process.nextTick(cb.bind(null, null, connection));
}
//连接池模式处理 无人排队
else {
//将释放的connection加入可用连接数组,待使用
this._freeConnections.push(connection);
//我本地加的日志
console.log("releaseConnection _allConnections.length: %j, _freeConnections.length: %j", this._allConnections.length, this._freeConnections.length);
}
};
node-mysql中的连接池代码学习的更多相关文章
- 解决Mysql连接池被关闭 ,hibernate尝试连接不能连接的问题。 (默认mysql连接池可以访问的时间为8小时,如果超过8小时没有连接,mysql会自动关闭连接池。系统发布第二天访问链接关闭问题。
解决Mysql连接池被关闭 ,hibernate尝试连接不能连接的问题. (默认MySQL连接池可以访问的时间为8小时,如果超过8小时没有连接,mysql会自动关闭连接池. 所以系统发布第二天访问会 ...
- DB数据源之SpringBoot+MyBatis踏坑过程(六)mysql中查看连接,配置连接数量
DB数据源之SpringBoot+MyBatis踏坑过程(六)mysql中查看连接,配置连接数量 liuyuhang原创,未经允许禁止转载 系列目录连接 DB数据源之SpringBoot+Mybati ...
- mongoDB中的连接池(转载)
一.mongoDB中的连接池 刚上手MongoDB,在做应用时,受以前使用关系型数据库的影响,会考虑数据库连接池的问题! 关系型数据库中,我们做连接池无非就是事先建立好N个连接(connection) ...
- xPool - 基于mysqlclient的mysql的c++连接池 - xnhcx的个人空间 - 开源中国社区
xPool - 基于mysqlclient的mysql的c++连接池 - xnhcx的个人空间 - 开源中国社区 xPool - 基于mysqlclient的mysql的c++连接池
- Mybatis中的连接池
Mybatis中DataSource的存取 MyBatis是通过工厂模式来创建数据源DataSource对象的,MyBatis定义了抽象的工厂接口:org.apache.ibatis.datasour ...
- WebSphere中数据源连接池太小导致的连接超时错误记录
WebSphere中数据源连接池太小导致的连接超时错误记录. 应用连接超时错误信息: [// ::: CST] webapp E com.ibm.ws.webcontainer.webapp.WebA ...
- JDBC创建mysql连接池代码
1.底层实现类(DBConnection) package JDBC.JDBCPool.MyJDBCPool; import java.sql.Connection; import java.sql. ...
- node 连接MySQL及其分装, 连接池连接
const mysql = require('mysql') const config = require('./../../config/config.default') var connectio ...
- Spring框架中获取连接池的几种方式
什么是数据库连接池? 数据库连接池是一种关键的有限的昂贵的资源,对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响到程序的性能指标.数据库连接池就是用来解决这些问题而提出的. 数据库连接 ...
随机推荐
- Haxe数据类型
以下是Haxe里面的一些数据类型 基本类型 空特性 类 枚举 匿名结构 方法 动态 抽象 1. 基本类型Bool, Float, Int 2. 空特性由于Haxe可以被编译为各种不同的target,不 ...
- linux下的共享库(动态库)和静态库
1.什么是库在windows平台和linux平台下都大量存在着库.本质上来说库是一种可执行代码的二进制形式,可以被操作系统载入内存执行.由于windows和linux的本质不同,因此二者库的二进制是不 ...
- Android用户界面布局(layouts)
Android用户界面布局(layouts) 备注:view理解为视图 一个布局定义了用户界面的可视结构,比如activity的UI或是APP widget的UI,我们可以用下面两种方式来声明布局: ...
- Effective C++学习笔记 条款04:确定对象被使用前已先被初始化
一.为内置类型对象进行手工初始化,因为C++不保证初始化它们. 二.对象初始化数据成员是在进入构造函数用户编写代码前完成,要想对数据成员指定初始化值,那就必须使用初始化列表. class A { pu ...
- bzoj3747
经典题,记录每个位置对应数下次出现的位置next[i] 每个位置维护当前左端点下到这个位置的和 随着左端点的右移一位到i+1,对[i+1,next[i]-1] 的影响是-a[i], [next[i], ...
- JSOI2008最大数(线段树)
注意到数列只增不减,而题目中又明确说道m<=200000;这样的数据规模线段树完全可以承受得了.所以我们可以事先建好一棵200000个子节点的线段树,然后求极值就好了. type node=re ...
- Blog CSS
你好 print("你好.") haode
- 【JSP】<meta>标签用法
转载自:http://blog.sina.com.cn/s/blog_65c74cce0102v39z.html 非常感谢这位博主,急着用,改日再细细品味重新整理这篇博文. http-equiv M ...
- 将Temporary文件夹里的Logo文件转移到Logo文件夹
/// <summary> /// 将Temporary文件夹里的Logo文件转移到Logo文件夹 /// </summary> /// <param name=&quo ...
- cgroup的测试数据
[root@xxxx /cgroup/memory/rule3021]#cat memory.limit_in_bytes503316480 480M [root@xxxx /cgroup/mem ...