当前位置:网站首页>【日记】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
边栏推荐
猜你喜欢

安装win11提示开启安全模式如何解决

15 days to upgrade to fight monsters and become a virtual fashion creator

如何模拟后台API调用场景,很细!

学习探索-给字体设置前景色

shell函数内如何调用另一个函数

shell脚本详解-------循环语句wuile循环和until循环

How to convert an int attribute into a string in the json format returned by the Go language gin framework?
![【 Gazebo introductory tutorial] speak the second model library into robot modeling and visualization (editor) model](/img/db/44a1ac5338879c9e6edd933c28c0af.png)
【 Gazebo introductory tutorial] speak the second model library into robot modeling and visualization (editor) model

御神楽的学习记录之基于FPGA的AHT10温湿度数据采集

【LeetCode Daily Question】——374. Guess the size of the number
随机推荐
不需要服务器,教你仅用30行代码搞定实时健康码识别
Json的FastJson与Jackson
从云计算到函数计算
MySQL学习笔记-4.数据更新时的性能问题
Nacos集群搭建
如何模拟后台API调用场景,很细!
麒麟信安石勇博士荣获openEuler社区年度开源贡献之星
在VMD上可视化hdf5格式的分子轨迹文件
全球电子产品需求萎靡:三星越南工厂大幅压缩产能,减少工人工作日
WEB 渗透之SSTI 模板注入
JSP的Web监听器(Listener)
SAP ABAP SteammPunk 蒸汽朋克的最新进展 - 嵌入式蒸汽朋克
JVM内存和垃圾回收-08.方法区
arm交叉编译
R语言缺失时间序列的填充及合并:补齐时间序列数据中所有缺失的时间索引、使用merge函数合并日期补齐之后的时间序列数据和另外一个时间序列数据(补齐左侧数据)
软件基础的理论
IDEA以多端口启动同一个服务项目
hi, 请问下这是什么问题, 我看官网的example就是mysql的, 咋提示不支持?
乐享购(分享购)的模式:优势、亮点、收益
ctfshow 萌新web1-21