当前位置:网站首页>[node] node uses MySQL connection pool
[node] node uses MySQL connection pool
2022-06-22 00:16:00 【Silent tubule】
Step on pit No easy , also Greek at various position Big Guy the a One Next \textcolor{gray}{ It's not easy to step on the pit , I also hope you guys can support } Step on pit No easy , also Greek at various position Big Guy the a One Next
individual people Lord page : \textcolor{green}{ Personal home page :} individual people Lord page : Silent tubule
individual people network standing : \textcolor{green}{ Personal website :} individual people network standing : Silent tubule
Technology Technique hand over flow Q Q Group : : 837051545 \textcolor{green}{ Technical communication QQ Group ::837051545} Technology Technique hand over flow QQ Group ::837051545
spot Fabulous , you Of recognize can yes I gen do Of dynamic force ! \textcolor{green}{ give the thumbs-up , Your recognition is the driving force of my creation !} spot Fabulous , you Of recognize can yes I gen do Of dynamic force !
️ closed hidden , you Of green look at yes I No force Of Fang towards ! \textcolor{green}{ Collection , Your favor is the direction of my efforts !} closed hidden , you Of green look at yes I No force Of Fang towards !
️ review On , you Of It means see yes I Into the Step Of goods rich ! \textcolor{green}{ Comment on , Your opinion is the wealth of my progress !} review On , you Of It means see yes I Into the Step Of goods rich !
If you don't understand, you can leave a message , I should see a reply
If there is a mistake , Your advice are most welcome
1. What is? mysql Connection pool
The database connection pool is a database connection object with sufficient number when the program starts , A pool composed of , from node The program dynamically calls the objects in the pool , Connect , Use , Release , Shut down etc
2. Why use mysql Connection pool
Why use mysql Connection pool , Instead of a single connection mysql, Use on link , Close after use , Isn't that convenient ?
Okay ~~~~ At the beginning, I also referred to the online code for convenience , Use a single link mysql Of , But there's a problem : After I connected, I closed , During this period , I have another program to execute mysql, But during this period, another program thought it was a link mysql The success of , Just as he was about to execute , The last program has put mysql Disconnect the , This results in an error ( Need to reconnect mysql), The interface on the client side is red ~~~
3.node Use mysql Connection pool
install mysql
npm -install -g node-mysql
Use mysql Connection pool
//*- coding = utf-8 -*-
//@Time : 2022-01-17 1:25
//@Author : silence
//@File : userService.js
//@web : www.php-china.com
//@Software: WebStorm
// The database class
const mysql = require("mysql"),// Need to install mysql expand
config = require("../utils/config")// The configuration file
class Model {
// Connection object static properties -- Static attributes are generated and destroyed simultaneously with class creation
static conn = null;
constructor() {
}
// Static method of linking database
static connection() {
let host = config.baseConfig.host;
let user = config.baseConfig.user;
let password = config.baseConfig.password;
let database = config.baseConfig.database;
//mysql Connection pool
Model.conn = mysql.createPool({
host,//" Database server address ",
port: "3306", // MySQL Database port number
database,// " Database name ",
user,//" User name to connect to the database ",
password,//" Password to connect to the database ",
connectionLimit: 20,//" Specify the maximum number of links in the connection pool , The default is 10",
multipleStatements: true,//" Whether to run and execute multiple sql sentence , The default value is false"
})
console.log(` Database connection successful `);
}
/** * Operation of database -- Open transaction * @param sql Operation statement executed * @param params to sql An array of parameters assigned to the placeholder of the statement * @returns {Promise<unknown>} */
static query(sql, params = []) {
return new Promise((resolve, reject) => {
this.connection();
Model.conn.getConnection((err,conn) => {
if (err) {
console.log(` Database connection failed :${
err.message}`);
}else{
conn.query(sql, params, (err, res) => {
if (err) {
console.log(' Database query failed ', err)
reject(err)
} else {
resolve(res)
}
})
conn.release();// Release the connection
}
})
})
}
}
module.exports = Model;
call mysql Connection pool
const model = require('../../index');
const utilsTool = require('../../../utils/utils');
class Index extends model{
/** * Get a list of all appointments for the next day */
static test(curEndTime,timeType){
return new Promise((resolve,reject)=>{
let sql = `SELECT * FROM xxx WHERE xxx > ? AND timeType=?`;
this.query(sql,[curEndTime,timeType]).then(res=>{
resolve(res);
}).catch(err=>{
console.log(` error message :${
err.message}`);
reject(err);
})
})
}
}
边栏推荐
- 【微信小程序】微信小程序使用表单的一些坑和注意事项
- buuctf misc zip
- 数据库每日一题---第19天:排名靠前的旅行者
- What if the program input point cannot be located in the dynamic link library
- Component value transfer: child components transfer data to parent components
- spacy.load(“en_core_web_sm“)###OSError: [E050] Can‘t find model ‘en_core_web_sm‘.
- redis主从复制(九)
- Six little-known SQL technologies in SQL tutorial can help you save 100 hours per month
- SQL tutorial: five SQL skills that data scientists need to master
- 组件传值:兄弟间传值(非父子组件进行传值)
猜你喜欢

组件传值:子组件向父组件传递数据

青春无言│用技术定格毕业季最美好的回忆

Based on vscode platformio under Arduino framework, one project is configured with two compatibility modes of different development boards

重载和重写的区别

Recruitment brochure for traditional product manager international qualification certification (NPDP) in the second half of 2022

Reddit product director: a necessary NFT member practical guide for Web3 creators

Buuctf misc weak password

JVM makes wheels

metersphere与jenkins的持续集成

WMS warehouse management system source code
随机推荐
Reddit product director: a necessary NFT member practical guide for Web3 creators
【node】node使用mysql连接池
Win11 hotspot connection successful but no network? Solution of win11 mobile hotspot and network conflict
诚邀 Elastic Stack 开发者成为 CSDN Elastic 云社区管理员
转载:网络加载框架 - Retrofit
The third "invalidation" of the prospectus of Yiteng pharmaceutical in Hong Kong: the listing was substantially delayed, Sequoia and other shareholders
从云鲸率先布局上门服务,看其如何用服务冲破行业“封锁线”
青春无言│用技术定格毕业季最美好的回忆
The ranking list of programming languages has been published in June, and this language should be "gods"
CVPR2022 | 弱监督多标签分类中的损失问题
以父之名活动攻略(可以薅羊毛啦)
火线沙龙第26期-多云安全专场
Unity network development (I)
Google AI big model lamda can one day replace the search engine? Google researcher: search can be re imagined as a two-way dialogue between users and languages
微博关闭发布多个兼职诈骗信息违规账号:如何打击数据造假灰产
Xshell连接虚拟机只能输入public key解决方案【亲测】
标志位生成
katalon Recoder常用命令
数据库每日一题---第19天:排名靠前的旅行者
SQL tutorial: five SQL skills that data scientists need to master