Setting the timeout with Knex.js for MSSQL - node.js

I use knex.js in a node environment to run a web server that makes sql calls. I have a query that takes over 30 seconds to complete, but when it's run through knex, the default timeout seems to be 15 seconds, so I get the following timeout error:
RequestError: Timeout: Request failed to complete in 15000ms
...
How do I change the timeout for mssql queries? The official doc has an example of setting timeout on a specific query with .timeout() but this feature doesn't work with mssql. I've also tried everything in this github issue without any luck. After trying all of that, I have this messy looking connection config:
const connection = require('knex')({
client: 'mssql',
connection: {
host : process.env.NODE_ENV == 'production' ? '172.18.1.66' : 'localhost',
user : secrets.user,
password : secrets.password,
database : 'EdgeView',
dialect: "mssql",
options: {
'enableArithAbort': true,
'requestTimeout': 150000,
'idleTimeoutMillis': 150000
},
pool: {
max: 10,
min: 0,
idleTimeoutMillis: 150000
},
dialectOptions:{
requestTimeout: 300000,
options: {
"requestTimeout": 300000
}
}
}
});
The error did not change, none of these timeout values seems to have had an impact.
The query it's self is just a raw query:
let res = connection.raw(GETSQLSTRING(args));

The answer was simply this:
const connection = require('knex')({
client: 'mssql',
connection: {
host : 'edge-sql',
user : secrets.user,
password : secrets.password,
requestTimeout: 600000,
database : 'EdgeView'
}
});
Thanks to BGPHiJACK for pointing me towards better documentation.

Related

Nodejs mssql server connection throws an error

I want to connect my mssql database from nodejs.
I am using sequelize and tedious module for this.
My connection configs like this
const sequelize = new Sequelize(
"mydb",
"username", "password", {
host:config_data.host,
"dialect":"mssql",
"port":1433
dialectOptions: {
instanceName: "MSSQLSERVER"
},
},
);
When I tried to run script, it throws an error.
(node:14584) UnhandledPromiseRejectionWarning: SequelizeConnectionError: Failed to connect to 192.168.10.220:1433 - Could not connect (sequence)
I apply all steps in configuration manager.
Enabled TCP/IP
Started Server SQL browser
Added 1433 port to Firewall
There is also additional error when host is "localhost".(Currently writed IP address)
(node:13552) UnhandledPromiseRejectionWarning: SequelizeConnectionError: Failed to connect to localhost:1433
- 4292:error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol:c:\ws\deps\openssl\openssl\ssl\statem\statem_lib.c:1947:
I need to help, is there someone have any idea ?
You need to set encrypt to false in your Sequelize inside options.
const sequelize = new Sequelize(
"mydb",
"username", "password", {
host:config_data.host,
"dialect":"mssql",
"port":1433,
"options": {
encrypt: false,
enableArithAbort: false
},
dialectOptions: {
instanceName: "MSSQLSERVER"
},
},
);
The issue is because of TLS protocol mismatch between Source & Destination server. In my case my App server was Ubuntu 20.04 & my SQL Server(Express 2012) was on Windows server. I also tried to downgrade TLS protocols on Ubuntu but nothing worked. So finally I disabled tls encryption in Sequelize.
Make sure options are given as expected by Sequelize
const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
host: dbConfig.HOST,
port: dbConfig.PORT,
dialect: dbConfig.dialect,
pool: {
max: dbConfig.pool.max,
min: dbConfig.pool.min,
acquire: dbConfig.pool.acquire,
idle: dbConfig.pool.idle,
},
// The below options are important to supress ssl issue on
// AWS EC2 ubuntu when db server is on windows. There is TLS protocol issue
// Which by using these options we disable tls encryption
dialectOptions: {
// Observe the need for this nested `options` field for MSSQL
options: {
encrypt: false,
enableArithAbort: false
}
}
});

sequelize connection timeout with cloud sql proxy in node.js

Running a local config, the connection works fine. I've installed and have a cloud_sql_proxy running and ready for new connections.
Here's my sequelize connection code:
const sequelize = new Sequelize(DB_NAME, DB_USER, DB_PASS, {
dialect: 'mysql',
host: `/cloudsql/${CLOUD_SQL_CONNECTION_NAME}`,
pool: {
max: 30,
min: 0,
idle: 10000,
acquire: 1000000,
},
dialectOptions: {
connectTimeout: 100000
}
});
At some point I set my max connection, acquire, and connectTimeout variables to absurdly high numbers, but still no luck, but I started getting a specific IP address located in London on port 3306.
I was able to connect to the cloud db by modifying my config to the following:
const sequelize = new Sequelize(DB_NAME, DB_USER, DB_PASS, {
dialect: 'mysql',
host: DB_HOST,
timestamps: false,
pool: {
max: 5,
min: 0,
idle: 10000
},
});
The difference is I pointed the host to the actual db IP address instead of the /cloudsql/${CLOUD_SQL_CONNECTION_NAME} path.

Syntax for defining 'pool' in KNEX/BookshelfJS

Firstly, I'm new to everything here... and new to StackOverflow, so apologies in advance for being a newbie and I'm ready for my thrashing... LOL.
We use a Heroku.addon for Postgres and utilize/reference environment variables globally to access the right database.
We have a config.js file in our application's root directory like:
db: process.env.DB_URL || {
client: 'pg',
connection: {
database: 'db_name',
user: 'user_name'
}
},
Would someone here be able to guide me on how to integrate code that initializes 'custom pool' information into this setup like the example found on
[http://knexjs.org/#Installation-pooling][http://knexjs.org/#Installation-pooling]
var knex = require('knex')({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
},
pool: { min: 0, max: 7 }
});
On Heroku, process.env.DB_URL is a complex URL that is similar to:
postgres://(redacted)#ec5-87-1-47-54.compute-1.amazonaws.com:5432/d8n2e9ebd0q9it
So, I am hoping there is a clean way to also pass 'custom pool' information as well either here or in another file/location.
The database is referenced throughout the backend of our application via Bookshelf/Knex. The reference to bookshelf looks similar to:
var knex = require('knex')(config.db);
var bookshelf = require('bookshelf')(knex);
You might be able to do it by reading just connection details from env variable, like this:
db: {
client: 'pg',
connection: process.env.DB_URL || {
database: 'db_name',
user: 'user_name'
},
pool: { min:5, max:20 }
},

Mongoose Replicaset Connection Not Working

I am trying to connect to AWS MongoDB instances and its not even attempting to connect:
This is the connection URI:
mongodb://<usr>:<pwd>#host:port,host2:port2/db
And this is the code I am using to connect.
var mongoose = require('mongoose');
var dbOptions = { server: { socketOptions: { keepAlive: 300000, connectTimeoutMS: 30000 } },
replset: { socketOptions: { keepAlive: 300000, connectTimeoutMS : 30000 } } };
mongoose.connect(uri, dbOptions);
I also added logs inside events.. none of the events are firing. If I remove host2:port2 it works. So issue seems to be with the connection string. But connection string looks totally fine.. but still its not working.. Any idea why?

multiple connection to mongodb in node.js

In below code we can connect MongoDB
var options = {
db: { native_parser: true },
server: { poolSize: 5 },
replset: { rs_name: 'myReplicaSetName' },
user: 'myUserName',
pass: 'myPassword'
}
mongoose.connect(uri, options);
Here poolSize is 5. So that 5 parallel connection can be perform on request.
But I see if we try to create second connect node gives error that I'm trying to create connection which is not closed. So at the same time one connection can do perform for one application.
So what is meaning of poolSize is 5 and how it perform?
I need a solution and a way to increase pool size when my system is scale up.
Thanks in advanced.
Mongoose (or rather the mongodb driver it uses) will automatically manage the number of connections to the MongoDB server. You should call mongoose.connect() just once.
If you need a larger number of connections, all you have to do is increase the poolSize property. However, since you're using a replicate set, you should set replset.poolSize instead of server.poolSize:
var options = {
db: { native_parser: true },
replset: { rs_name: 'myReplicaSetName', poolSize : POOLSIZE },
user: 'myUserName',
pass: 'myPassword'
}

Resources