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框架中获取连接池的几种方式
什么是数据库连接池? 数据库连接池是一种关键的有限的昂贵的资源,对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响到程序的性能指标.数据库连接池就是用来解决这些问题而提出的. 数据库连接 ...
随机推荐
- 【c】time.h
表示时间的三种类型 日历时间:从一个时间点到现在的秒数,用time_t表示 始终滴答时间:从进程启动到现在时钟的滴答数(每秒一般包含1000个).用clock_t表示 分解时间:分解的数据结构如下.用 ...
- 深度神经网络如何看待你,论自拍What a Deep Neural Network thinks about your #selfie
Convolutional Neural Networks are great: they recognize things, places and people in your personal p ...
- NDK(14)Native的char*和Java的String相互转换
转自: http://www.cnblogs.com/canphp/archive/2012/11/13/2768937.html 首先确保C/C++源文件的字符编码是UTF-8与JAVA的class ...
- java 菱形
//画菱形 一半 for(int hs=1;hs<11;hs++) //行数 { //画空格 for(int kg = 9; kg >= hs; kg--) //空格数 { System. ...
- [HDOJ3974]Assign the task(建树胡搞)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 出现在窝bin的线段树专题里…第一时间想的是记录入度找出根节点,然后标记深度转换到线段树中.但是 ...
- 转载:C++ STL set学习
声明:本文转载自Penguin的博客 http://blog.sina.com.cn/s/blog_779cf3410101389s.html 1,set的含义是集合,它是一个有序的容器,里面的元素都 ...
- 面试题_125_to_133_Java 面试中其他各式各样的问题
这部分包含 Java 中关于 XML 的面试题,JDBC 面试题,正则表达式面试题,Java 错误和异常及序列化面试题 125)嵌套静态类与顶级类有什么区别?(答案)一个公共的顶级类的源文件名称与类名 ...
- 面试题_66_to_75_Java IO 和 NIO 的面试题
IO 是 Java 面试中一个非常重要的点.你应该很好掌握 Java IO,NIO,NIO2 以及与操作系统,磁盘 IO 相关的基础知识.下面是 Java IO 中经常问的问题. 66)在我 Java ...
- RAPIDXML 中文手册,根据官方文档完整翻译!
简介:这个号称是最快的DOM模型XML分析器,在使用它之前我都是用TinyXML的,因为它小巧和容易上手,但真正在项目中使用时才发现如果分析一个比较大的XML时TinyXML还是表现一般,所以我们决定 ...
- ios9下ionic框架报[$rootScope:infdig] 10 $digest() iterations reached. Aborting!的解决办法
升级ios9后,ionic开发的app会报[$rootScope:infdig] 10 $digest() iterations reached. Aborting!的错误,加上一个patch就可以解 ...