当前位置:网站首页>【日记】mysql数据库连接池
【日记】mysql数据库连接池
2022-08-04 17:18:00 【ttyt1217】
今天看了nodejs用mysql连接池的相关资料,
因为服务通讯中建立连接和关闭连接的耗时还是挺大的,
所以使用连接池来提前建立一些连接放在里面维持着,进行数据库访问的时候去池子里取用,
用完不需关闭只要释放回池子里,这样就能减少时间开销。
连接池的建立:
let pool = mysql.createPool(config.dbinfo);
取用连接池的连接执行:
//db.js
let query = function (sqlstr, param, callback) {
pool.query(sqlstr, param, callback);
};
exports.query = query;
//exec.js
let query = require('./db').query;
let sqlselect = 'SELECT * FROM testtable;';
query(sqlselect, null, function (err, result) {
res.send(result);
});
以上取用不用考虑连接的建立与释放,query方法内部已经封装好了:
查看mysql源码的Pool.js可知 pool.query() 是 以下操作的一个捷径:
pool.getConnection() -> connection.query() -> connection.release()
但要注意:每次执行pool.query()所取用的connection可能不是同一个。
参考资料:
我们为什么要使用数据库连接池?
https://www.zhihu.com/question/349816338
Github mysqljs/mysql
https://github.com/mysqljs/mysql#release
边栏推荐
- 下一代 AutoAI:从模型为中心,到数据为中心
- The second step through MySQL in four steps: MySQL index learning
- 8月5日,麒麟信安邀您相约鲲鹏开发者创享日·长沙站!
- 水能自发变成“消毒水”,83岁斯坦福教授:揭示冬天容易得流感的部分原因...
- LeetCode Question of the Day - 1403. Minimum Subsequence in Non-Increasing Order
- R语言ggpubr包的ggtexttable函数可视化表格数据(直接绘制表格图或者在图像中添加表格数据)、使用ggarrange函数将表格数据和可视化图像组合起来(表格数据在可视化图像下方)
- mysqlbinlog 超过500g自动删除,保留7个,求大深给个版本
- yarn详细入门教程
- 基于clipboard.js对复制组件的封装
- 小程序+自定义插件的混合模式
猜你喜欢
】 【 LeetCode daily one problem - 540. The order of a single element of the array
《机器学习的随机矩阵方法》
mmdetection/mmdetection3d多机多卡训练
Codeforces Round #811 (Div. 3)
从云计算到函数计算
Boost library study notes (1) Installation and configuration
荣耀互联对外开放,赋能智能硬件合作伙伴,促进全场景生态产品融合
How to convert an int attribute into a string in the json format returned by the Go language gin framework?
开发一套高容错分布式系统
泰坦尼克号沉船数据之美——起于悲剧,止于浪漫
随机推荐
Learning to Explore - Setting the Foreground Color for Fonts
SRM供应商协同管理系统功能介绍
SAP 电商云 Spartacus UI 页面布局的设计原理
WEB 渗透之逻辑漏洞
Digital-intelligent supply chain management system for chemical manufacturing industry: build a smart supply system and empower enterprises to improve production efficiency
【LeetCode每日一题】——374.猜数字大小
To eliminate asynchronous callbacks, it has to be async-await
The use of QCompleter for Qt auto-completion
浅谈运用低代码技术如何实现物流企业的降本增效
软件基础的理论
LeetCode Question of the Day - 1403. Minimum Subsequence in Non-Increasing Order
php如何查询字符串以什么开头
Cesium快速上手0-Cesium安装与基本介绍
如何模拟后台API调用场景,很细!
WEB 渗透之越权
泰坦尼克号沉船数据之美——起于悲剧,止于浪漫
(一)、线性表的顺序存储结构链式存储结构
JVM内存和垃圾回收-08.方法区
.NET云原生应用发展论坛--8月7日邀你一起云上探索
Clearance sword refers to Offer——The sword refers to Offer II 010. and the sub-array of k