[QUESTION] How do I check the status of a connection?
APTGithubAcct opened this issue · 3 comments
How do I check to see the current status of a connection? I ask because right now I used one of the minimal examples to connect to SQL and then accidentally hit the connect button a 2nd time - this caused this error:
ConnectionError:
.connect
can not be called on a Connection inLoggedIn
state.
So to adjust for this, I'd like to check the current status of the connection before attempting to connect again. Is this possible? If so, how would I do this?
Hi @APTGithubAcct, the states is more of a tedious internal handling for the whole connection process. You can use the debug log to log out the state changes in terminal, but the whole process should be automated and does not involve much user interactions.
Here is an example for enable debugging from tedious side:
var config = {
...
options: {
...
// Add this to your configuration file. You can pick and choose which ones to enable.
debug: {
packet: true,
data: true,
payload: true,
token: true
}
},
...
};
Then add this event listener to output the debug messages:
connection.on('debug', (msg) => {
console.log(msg);
});
For your case, only a single active connection can be formed at a time, so if you want to open a multiple connections simultaneously, then you could try this lib: node-mssql which has a connection pooling function build on top of tedious. If you want to open consecutive connections, then you have to wait until the pervious connection has ended by calling connection.close(), then open a new connection after that within the end `event handler:
connection.on('end', () => {
open a new connection here.
});
Hope this help with your question.
Hi @APTGithubAcct, the states is more of a tedious internal handling for the whole connection process. You can use the debug log to log out the state changes in terminal, but the whole process should be automated and does not involve much user interactions. Here is an example for enable debugging from tedious side:
var config = { ... options: { ... // Add this to your configuration file. You can pick and choose which ones to enable. debug: { packet: true, data: true, payload: true, token: true } }, ... };Then add this event listener to output the debug messages:
connection.on('debug', (msg) => { console.log(msg); });For your case, only a single active connection can be formed at a time, so if you want to open a multiple connections simultaneously, then you could try this lib: node-mssql which has a connection pooling function build on top of tedious. If you want to open consecutive connections, then you have to wait until the pervious connection has ended by calling connection.close(), then open a new connection after that within the end `event handler:
connection.on('end', () => { open a new connection here. });Hope this help with your question.
Would you say it's standard practice to open a connection, perform the SQL query, then close the connection? Or is it more normal to open a connection, perform any number of SQL queries, then close the connection?
Hi @APTGithubAcct , you can do multiple requests within the same connection, but they have to ben executed one by one. Each new request need to wait until the pervious request callback is executed, either with an error or with the result. Otherwise, you will get an "Requests can only be made in the LoggedIn state, not the SentClientRequest state" error since
only one query can be executed on a connection at a time.