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框架中获取连接池的几种方式
什么是数据库连接池? 数据库连接池是一种关键的有限的昂贵的资源,对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响到程序的性能指标.数据库连接池就是用来解决这些问题而提出的. 数据库连接 ...
随机推荐
- Linux 查找软件安装路径
root@kali:~# whereis sqlmap sqlmap: /usr/bin/sqlmap /usr/share/sqlmap /usr/share/man/man1/sqlmap..gz ...
- OSCache 缓存技术
前言:OSCache标记库由OpenSymphony设计,它是一种开创性的JSP定制标记应用,提供了在现有JSP页面之内实现快速内存缓冲的功能.OSCache是个一个广泛采用的高性能的J2EE缓存框架 ...
- Mvc Kissy uploader实现图片批量上传 附带瀑布流的照片墙
前言 KISSY 是由阿里集团前端工程师们发起创建的一个开源 JS 框架.它具备模块化.高扩展性.组件齐全,接口一致.自主开发.适合多种应用场景等特性.本人在一次项目中层使用这个uploader组件. ...
- [HDOJ2818]Building Block(带权并查集,路径压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2818 题意:有N个块,每次有两个操作: M x y表示把x所在的那一堆全部移到y所在的那一堆的下方. ...
- [HDOJ4635]Strongly connected(强连通分量,缩点)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4635 题意:给一张图,问最多往这张图上加多少条边,使这张图仍然无法成为一个强连通图. 起初是先分析样例 ...
- IntelliJ IDEA For Mac 快捷键——常用版
一.搜索 搜索文件 command+shift+n 打开方法实现类 command+option+b 全文搜索 ctrl+shift+f (1)类和方法 查看类的继承结构 ctrl+h 查看方法的 ...
- git push --no-thin
有时候我们执行 git push 将一个 new branch 推送到远程仓库的时候,会被远程仓库阻止. 可能是我们没有相应的权限吧.然而,我在 git push 的时候加上 --no-thin 参数 ...
- VB获取浏览器版本
String userAgent; userAgent = Request.UserAgent; ) { // The browser is Microsoft Internet Explorer V ...
- UVa 11572 Unique snowflakes【滑动窗口】
题意:给出 n个数,找到尽量长的一个序列,使得该序列中没有重复的元素 看的紫书,滑动窗口来做的 当右端碰到有相同的数的时候,左端向前滑动一个数 模拟一个样例好理解些 #include<iostr ...
- erlang 线上分析工具集锦
1.Recon-Erlang线上系统诊断工具(引自): Erlang系统素以稳定可靠闻名,但是它也是c实现的,也是要管理比如内存,锁等等复杂的事情,也会出现Crash,而且crash的时候大部分原因是 ...