topic description
after the connection pool connection is successful, the connection timed out when performing the database operation. How to configure to return an error directly instead of trying to reconnect all the time
related codes
const to = require("await-to-js").default;
const sql = require("mssql");
const timeOut = require("../utils/timeOut");
const config = {
user: "sa",
password: "123",
server: "127.0.0.1",
database: "test",
port: 1433,
appName: "node-test",
connectionTimeout: 5000, //timeoutms 15000
requestTimeout: 5000,//timeoutms15000
//parseJSON: true, //jsonjson obj
options: {
encrypt: false // ,Windows Azure,true
},
pool: {
max: 100,
min: 0,
idleTimeoutMillis: 30000 //ms30000
}
};
(async () => {
let err, pool, result;
for (; ;) {
[err, pool] = await to(new sql.ConnectionPool(config).connect());
if (!err) {
pool.on("error", err => {
console.error(err.message);
});
console.log(``);
console.log(`SQL:${config.server}`);
break;
} else if (pool !== undefined) {
pool.close();
}
console.error(err.message);
console.error(`5...`);
await timeOut(config.connectionTimeout);
}
// SQL Server
await timeOut(15000);
[err, result] = await to(pool.query`SELECT 1`);
//
console.log(err, result);
})();