-
Notifications
You must be signed in to change notification settings - Fork 468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Idle connections not being closed by the pool #457
Comments
Are you checking for an exception presence inside catch block? |
Yes, it does not get into the catch block |
Ok, I have tested it on the same versions and it works. Could you please try to use non-async-await syntax? Just to confirm it has nothing to do with connection problems. |
The results from the db are being displayed to stdout, so I think it has nothing with the connection, but I will try anyway. |
Ahh, sorry, I didn't understand. Now its clear where the problem is - the pool doesn't close idle connections. With v4 I have updated underlying node-pool dependency to latest version and didn't noticed that the behavior has changed and it is not longer releasing idle connections by default. I will fix that. |
That is ok. I will change the issue title since it has nothing with the async/await syntax. Thank you! |
Fixed in 4.0.4. Anyway, the fix doesn't close the process when all connections are closed. You need to explicitly call |
nothing is fixed...
this example reporoduce the problem.. this mean there is no pool at all |
I'm using 4.0.4 and can confirm calling connectionPool.close() closes the connections.. eventually. In my case, we're using AWS lambda functions to pull data from the DB. So waiting for the connection pool to terminate is a no go. I worked around the issue my modifying the pool configuration after instantiating the ConnectionPool. This ensures our lambda function will execute quickly and will not hang around after data was returned. Note: If you are using the non-async-await syntax, instantiating ConnectionPool will also trigger connect. So you'll have to provide a config object at instantiation time. My Implementation: export class DatabaseService {
readonly connection: ConnectionPool;
constructor(dbUri: string) {
this.connection = new ConnectionPool(dbUri);
this.connection['config'] = Object.assign(
this.connection['config'] || {},
{
pool: {
max: 1,
min: 0,
evictionRunIntervalMillis: 500,
idleTimeoutMillis: 500,
testOnBorrow: true,
},
},
);
async connect(): Promise<ConnectionPool> {
// not yet connected, start the connection.
if (!this.connection.connected && !this.connection.connecting) {
await this.connection.connect();
}
return this.connection;
}
async disconnect(): Promise<boolean> {
try {
await this.connection.close();
return true;
} catch (e) {
console.error('Error while disconnecting from database:', e);
return false;
}
}
...
}
``` |
I dont need to close connections. I need to work with tem like with pool. Pool have to manage them by itself, but he doesnt. Dont limit them, dont wait until some of them gets released to use it with next query, dont close them by timeout when they gets released |
I'm having similar issues on 4.0.4. I have an mssql server running in docker:
Here is the smallest script that I was able to reproduce the issue with:
I also appended some logging as follows: base.js
tedious.js
I get this output when running the script:
The issue might be even easier to replicate if you append additional queries to Promise.all block. Below is output when 10 insert operations were scheduled concurrently.
It seems that some connections are being closed too late. I also attached
However, if I wait 15000ms before calling close after the requests, the script runs and stops successfully.
|
I'm still seeing this exact problem in 4.1.0. The connection pool causes my test to hang after passing and Jest never exits. Adding describe('mssql', () => {
it('works without confusing Jest', async () => {
const pool = await sql.connect(config)
const result = await sql.query`select * from new_evaluationBase;`
pool.close()
expect(result.recordset.length).toEqual(3)
})
}) |
I have the same problem with the 4.10 version. I am using the connection pool when triggering a good amount of async queries. And after I return the results from the db, I explicitly close the connection pool but still the connections used remain active and the MSSQL server shows all the unclosed connection. For a heavy use, this makes the Db server to reach the max available connections and the Node.JS server can not connect anymore . Is there any fix I can use? |
What about using a lower https://github.com/tediousjs/node-mssql#general-same-for-all-drivers
|
Does not help to lower idleTimeoutMillis
…On Thu, May 10, 2018 at 5:02 PM, Will Morgan ***@***.***> wrote:
What about using a lower idleTimeoutMillis?
https://github.com/tediousjs/node-mssql#general-same-for-all-drivers
pool.idleTimeoutMillis - The Number of milliseconds before closing an
unused connection (default: 30000).
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#457 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ARIl6Hsq2VGCfPL_m31qGXzSq0g3kcHVks5txKrmgaJpZM4NGtLQ>
.
--
Mordechai Kohn
|
Not to rehash this, but still having to call |
I managed to fix my problems with the pool and not properly closing the connections, using the Bluebird Promises disposer. Here is a code snippet:
And then use it:
Is basically creating a connectionPool and closing it at the end of the usage. With other implementations I tried before, I had loads of unclosed connections in the server and on the long run it made the server to crash. In this way you can make sure that you close all the connections. Is not the most viable solution, but is a workaround. |
If you must create a connection pool for every connection request then surely you want to set Or the better option, which is not pooling at all. |
Yes, indeed, the config is set to |
More recent versions of node-mssql have improved lifecycle management which I think should resolve this issue, so I'm closing it. Please upgrade to v4.2.2 (most recent at time of writing). If this doesn't improve things then please feel free to reopen. Particularly this could have been fixed by #683. |
Facing this issue for a while in our mocha tests using mssql v4.3.0.
But ConnectionPool is closed after call
This happens in irregular base but it has impact to our tests.
|
Yeah, I still have pain with tests on mocha - I just ensure I close every connection that gets created and I do that by keeping track of connections. It is a bad developer experience, though... @willmorgan any ideas? |
After digging deeper I think this issue is related to |
@vlapo is there a workaround we can apply to this library to mitigate the above issue? |
I dont think so. If my deduction is right it is internal problem of library. |
I am also experiencing this issue. Quite surprised it's still open, kinda defeats the purpose of using a connection pool. |
@dhensby You were looking into this the other day, remind me what you found? Was it Tedious, the pool, or this library? |
OK - so with a bit better understanding of the issue as raised here, I'm not sure there's a bug(??) From what I understand people want the connection pool to either (1) close itself after a request is complete OR they want (2) the connection pool to automagically close itself when the process is complete.
I was experiencing the problem with mocha test suites not exiting because processes were still running (connections were open), unfortunately the only way to resolve this was to follow the mocha docs and properly manage your connections (ie: closing them in the
|
I'm going to close this because I don't think there's much we can do about this, it's a developer expectation problem and not a bug. If I've mis-understood, please let me know |
pool.end() worked for me |
Connection Pool is not working, i am using the latest tedious 6.6.0 as well as mssql npm 6.0.0, but still i am facing the same issue, could anybody help me to fix this, any issue in my below code: if i close pool.close() in finally, after that it is not connecting again, when the request comes from front end: const connPool = new sql.ConnectionPool(dbConfig).connect(); |
@gsaravanakumar932 it seems like the pool is working. You're able to create a pool, connect to the DB, execute queries and close it. If you close the pool and then continue to attempt to use it, then you will have problems because the pool will be closed. You need to manage your connection pool better, either rely on the global pool we provide in |
I am able to create connectionpool, but it is not closing auto, if the request is done. Below is my express router example code, router.get('/getCategoryDetails', cache5mins, (req, res) => {
const query = shareQuery.getAllCategories(req);
executeSql(query, function(data, err) {
if (err) { res.status(500).send(err); return; }
res.status(200).json(data);
});
}); DBconfig, const dbConfig = {
user: process.env.DB_USER_ID,
password: process.env.DB_PWD,
server: process.env.DB_SERVER,
database: process.env.DB_NAME,
port: Number(process.env.DB_PORT),
requestTimeout: 500000,
options: {
encrypt: false // Use this if you're on Windows Azure
},
pool: {
min: 10,
idleTimeoutMillis: 30000
}
}; this is my common connectionpool code, const pool = new sql.ConnectionPool(dbConfig)
const connPool = pool.connect();
pool.on('error', err => {
// ... error handler
})
const executeSql = function(query, callback) {
connPool
.then((pool) => {
return pool.request().query(query);
})
.then(result => {
callback(result.recordset);
})
.catch(err => {
callback(null, err);
});
}; Could you please suggest me where to close the connection in the above code, as part of best practices ? |
If i write like below, const executeSql = function(query, callback) {
pool.connect()
.then((pool) => {
return pool.request().query(query);
})
.then(result => {
callback(result.recordset);
})
.catch(err => {
callback(null, err);
}).finally(() => {
pool.close();
});
}; I am getting error like below,
|
As a comment, your callback arguments are non-standard. The conventional way to use a callback is: (error, ...args) => {
if (error) {
// handle error case
}
// handle success case
} You'd then do: const connectionPool = (new sql.ConnectionPool(config)).connect();
function executeSql(query) {
return connectionPool.then(pool => {
return pool.request().query(query);
}).then(result => {
setImmediate(callback, null, result);
}).catch(error => {
setImmediate(callback, error);
});
} However, you're mixing promises and callbacks for some reason. The mssql library supports callbacks (though this is still a not very nice mix of callbacks and promises): const connectionPool = (new sql.ConnectionPool(config)).connect();
function executeSql(query) {
connectionPool.then(pool => {
pool.request().query(query, callback);
}).catch(callback); or you can use promises in your express router: router.get('/getCategoryDetails', cache5mins, (req, res) => {
const query = shareQuery.getAllCategories(req);
executeSql(query).then(data => {
res.status(200).json(data);
}).catch(err => {
res.status(500).send(err);
});
}); and const connectionPool = (new sql.ConnectionPool(config)).connect();
function executeSql(query) {
return connectionPool.then(pool => {
return pool.request().query(query);
});
} lastly, because you're reusing variable names, you're making things less clear and your call to |
@dhensby awesome. This is expected. |
Based on the docs quick example, I am trying to run a simple query as follows:
After logging the results, the process remains stucked until I press CTRL+C.
The problem is happening in the
await sql.query
part - it is returning a promise that seems to resolve, so I cannot figure out what is happening and why the process do not finish and keeps waiting for something.Node 7.9.0
node-mssql 4.0.2
The text was updated successfully, but these errors were encountered: