使用连接池;退出检查优化
This commit is contained in:
@@ -5,9 +5,9 @@ const path = require('path');
|
||||
let globalConfig = JSON.parse(fs.readFileSync(path.join(__dirname, '../config.json'), 'utf8'));
|
||||
let pool = null;
|
||||
|
||||
function createPool(database) {
|
||||
function create({ database, connectionLimit = 10 }) {
|
||||
let config = {
|
||||
connectionLimit: 10, //连接数量,默认是10
|
||||
connectionLimit: connectionLimit, //连接数量,默认是10
|
||||
...globalConfig.mysql,
|
||||
database: database,
|
||||
};
|
||||
@@ -21,10 +21,10 @@ function createPool(database) {
|
||||
// console.log('获取连接', conn.threadId);
|
||||
// });
|
||||
|
||||
// //在连接池中建立新连接时,将触发该事件
|
||||
// pool.on('connection', function (conn) {
|
||||
// console.log('建立新连接', conn.threadId);
|
||||
// });
|
||||
//在连接池中建立新连接时,将触发该事件
|
||||
pool.on('connection', function (conn) {
|
||||
console.log('建立新连接', conn.threadId);
|
||||
});
|
||||
|
||||
// //等待可用连接时,将触发该事件
|
||||
// pool.on('enqueue', function () {
|
||||
@@ -38,49 +38,45 @@ function createPool(database) {
|
||||
}
|
||||
|
||||
async function query(sql, params) {
|
||||
if (!pool) {
|
||||
console.error('Database connection pool is not initialized yet.');
|
||||
return;
|
||||
}
|
||||
return await new Promise(function (resolve, reject) {
|
||||
//pool.query()方法可以自动的帮我们在连接池中获取可用连接
|
||||
if (!pool) {
|
||||
console.error('Database connection pool is not initialized yet.');
|
||||
resolve(data);
|
||||
return;
|
||||
}
|
||||
|
||||
// pool.query()方法可以自动的帮我们在连接池中获取可用连接
|
||||
pool.query(sql, params, function (err, data) {
|
||||
if (err) reject(err);
|
||||
// console.log(data);
|
||||
resolve(data);
|
||||
});
|
||||
|
||||
// //当然我们也可以手动获取可用连接
|
||||
// // 当然我们也可以手动获取可用连接
|
||||
// pool.getConnection(function (err, conn) {
|
||||
// if (err) {
|
||||
// throw err;
|
||||
// }
|
||||
// conn.query('select * from `order`', function (err, data) {
|
||||
// if (err) {
|
||||
// throw err;
|
||||
// }
|
||||
// data.forEach(function (value) {
|
||||
// console.log(value.id, value.order_id, value.user_id);
|
||||
// });
|
||||
|
||||
// //连接用完之后,需要释放,重新放回连接池中。
|
||||
// //注意这里并没有销毁该连接,该连接仍然可用,但需要重新获取
|
||||
// if (err) reject(err);
|
||||
// conn.query(sql, function (err, data) {
|
||||
// if (err) reject(err);
|
||||
// resolve(data);
|
||||
// // 连接用完之后,需要释放,重新放回连接池中。
|
||||
// // 注意这里并没有销毁该连接,该连接仍然可用,但需要重新获取
|
||||
// conn.release();
|
||||
// });
|
||||
// });
|
||||
});
|
||||
}
|
||||
|
||||
function close() {
|
||||
if (!pool) return;
|
||||
pool.end(function (err) {
|
||||
console.log('关闭连接池', err);
|
||||
async function close() {
|
||||
await new Promise((resolve, reject) => {
|
||||
if (!pool) return;
|
||||
pool.end(function (err) {
|
||||
console.log('关闭连接池', err);
|
||||
resolve(err);
|
||||
});
|
||||
});
|
||||
pool = null;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createPool: createPool,
|
||||
create: create,
|
||||
query: query,
|
||||
close: close,
|
||||
}
|
@@ -4,16 +4,16 @@ const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
let globalConfig = JSON.parse(fs.readFileSync(path.join(__dirname, '../config.json'), 'utf8'));
|
||||
let database = null;
|
||||
let databaseName = null;
|
||||
|
||||
function create(databaseName) {
|
||||
database = databaseName;
|
||||
function create({ database }) {
|
||||
databaseName = database;
|
||||
}
|
||||
|
||||
async function query(sql, params) {
|
||||
let config = {
|
||||
...globalConfig.mysql,
|
||||
database: database,
|
||||
database: databaseName,
|
||||
};
|
||||
// console.log(config);
|
||||
|
||||
@@ -34,6 +34,8 @@ async function query(sql, params) {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
setConnectionLimit: () => { },
|
||||
create: create,
|
||||
query: query,
|
||||
close: () => { },
|
||||
}
|
Reference in New Issue
Block a user