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中的连接池代码学习的更多相关文章

  1. 解决Mysql连接池被关闭 ,hibernate尝试连接不能连接的问题。 (默认mysql连接池可以访问的时间为8小时,如果超过8小时没有连接,mysql会自动关闭连接池。系统发布第二天访问链接关闭问题。

    解决Mysql连接池被关闭  ,hibernate尝试连接不能连接的问题. (默认MySQL连接池可以访问的时间为8小时,如果超过8小时没有连接,mysql会自动关闭连接池. 所以系统发布第二天访问会 ...

  2. DB数据源之SpringBoot+MyBatis踏坑过程(六)mysql中查看连接,配置连接数量

    DB数据源之SpringBoot+MyBatis踏坑过程(六)mysql中查看连接,配置连接数量 liuyuhang原创,未经允许禁止转载 系列目录连接 DB数据源之SpringBoot+Mybati ...

  3. mongoDB中的连接池(转载)

    一.mongoDB中的连接池 刚上手MongoDB,在做应用时,受以前使用关系型数据库的影响,会考虑数据库连接池的问题! 关系型数据库中,我们做连接池无非就是事先建立好N个连接(connection) ...

  4. xPool - 基于mysqlclient的mysql的c++连接池 - xnhcx的个人空间 - 开源中国社区

    xPool - 基于mysqlclient的mysql的c++连接池 - xnhcx的个人空间 - 开源中国社区 xPool - 基于mysqlclient的mysql的c++连接池

  5. Mybatis中的连接池

    Mybatis中DataSource的存取 MyBatis是通过工厂模式来创建数据源DataSource对象的,MyBatis定义了抽象的工厂接口:org.apache.ibatis.datasour ...

  6. WebSphere中数据源连接池太小导致的连接超时错误记录

    WebSphere中数据源连接池太小导致的连接超时错误记录. 应用连接超时错误信息: [// ::: CST] webapp E com.ibm.ws.webcontainer.webapp.WebA ...

  7. JDBC创建mysql连接池代码

    1.底层实现类(DBConnection) package JDBC.JDBCPool.MyJDBCPool; import java.sql.Connection; import java.sql. ...

  8. node 连接MySQL及其分装, 连接池连接

    const mysql = require('mysql') const config = require('./../../config/config.default') var connectio ...

  9. Spring框架中获取连接池的几种方式

    什么是数据库连接池? 数据库连接池是一种关键的有限的昂贵的资源,对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响到程序的性能指标.数据库连接池就是用来解决这些问题而提出的. 数据库连接 ...

随机推荐

  1. highcharts 结合phantomjs纯后台生成图片系列二之php

    上篇文章中介绍了phantomjs的使用场景,方法.本篇文章详细介绍使用php,highcharts 结合phantomjs纯后台生成图片. 一.准备: 下载phantomjs解析插件,从 highc ...

  2. 深度卷积神经网络用于图像缩放Image Scaling using Deep Convolutional Neural Networks

    This past summer I interned at Flipboard in Palo Alto, California. I worked on machine learning base ...

  3. 基于eclipse创建android的helloworld工程

    基于eclipse创建android的helloworld工程 之前用过Android studio感觉很慢,决定采用eclipse来学习Android开发.下面来看是怎么创建的. 选择File--- ...

  4. Proxifier设置代理

    1.首先需要开启http代理选项---配置文件->高级->HTTP代理服务器,勾选“启用HTTP代理服务器支持” 2.然后开始添加代理服务器选择“配置文件->代理服务器”,在弹出框点 ...

  5. ACM - ICPC World Finals 2013 C Surely You Congest

    原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 题目翻译: 试题来源 ACM/ICPC World Fin ...

  6. PHPnow 升级后 PHP不支持GD、MySQL

    来自http://tunps.com/php-unsupport-gd-and-mysql-after-upgrade-phpnow 最近磁盘格式化误操作后,最近两天都在忙于数据恢复,现在才恢复正常. ...

  7. javascript数组详解

    1.数组的一些方法: <script type="text/javascript"> //var arr = [1,2,3,4]; //性能略高 var arr = n ...

  8. Java [Leetcode 319]Bulb Switcher

    题目描述: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...

  9. VirtualBox的工作原理&参考网上文章

    事先申明,我这里有好多东西都是看网上的,文末给出参考博客链接. 1.在设置里面为什么要选择桥接网络?baidu之后,了解到是虚拟机工作原理的不同,也就是说有好几种工作模式. bridged(桥接模式) ...

  10. Android-使用getIdentifier()获取资源Id

    使用getIdentifier()获取资源Id int i= getResources().getIdentifier("icon", "drawable", ...