Skip to content
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

[QUESTION] How do I check the status of a connection? #1568

Open
APTGithubAcct opened this issue Aug 10, 2023 · 3 comments
Open

[QUESTION] How do I check the status of a connection? #1568

APTGithubAcct opened this issue Aug 10, 2023 · 3 comments
Labels
Q&A For non-issues. General Q&A

Comments

@APTGithubAcct
Copy link

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 in LoggedIn 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?

@APTGithubAcct APTGithubAcct added the Q&A For non-issues. General Q&A label Aug 10, 2023
@APTGithubAcct APTGithubAcct changed the title [QUESTION] [QUESTION] How do I check the status of a connection? Aug 10, 2023
@MichaelSun90
Copy link
Contributor

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.

@APTGithubAcct
Copy link
Author

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?

@MichaelSun90
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Q&A For non-issues. General Q&A
Projects
None yet
Development

No branches or pull requests

2 participants