The Wayback Machine - https://web.archive.org/web/20200601102227/https://github.com/mysqljs/mysql/issues/1981
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: Is there a way to check if a connection is connected? #1981

Open
john1625b opened this issue Apr 2, 2018 · 2 comments
Open

Question: Is there a way to check if a connection is connected? #1981

john1625b opened this issue Apr 2, 2018 · 2 comments
Labels

Comments

@john1625b
Copy link

@john1625b john1625b commented Apr 2, 2018

I am making a health check endpoint for kubernetes (google cloud's service to host containerized applications) for my mySql application.

right now I am using this package https://www.npmjs.com/package/health-check-mysql

But is there something built into the package itself, something like a isConnected method. ie:
connection.isConnected() ? throw new Error('not connected to mysql') : null

@dougwilson
Copy link
Member

@dougwilson dougwilson commented Apr 2, 2018

It's not documented (but if someone wants to write up some docs, that'd be awesome 👍), but each connection object has a state property. Connected would be connection.state === 'connected' || connection.state === 'authenticated'. But due to how the TCP sockets work in Node.js, this module is not notified if the TCP connection has been terminated (like if the MySQL server kills an idling connection) until a socket operation is attempted. This means that the only reliable way to know if the connection is good is to attempt an operation, like connection.ping.

@dougwilson dougwilson added the question label Apr 2, 2018
@dougwilson dougwilson added the docs label Mar 8, 2019
@bhupirawat57
Copy link

@bhupirawat57 bhupirawat57 commented Feb 20, 2020

yes, you can put if condition when connection is connect print connected otherwise not connected. but make sure you assign connection code to a variable and thn put if condition on a vaariable...

your connection code here..

if($a){
echo connected; }
else{
echo not connected; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.