node-mysql2
node-mysql2 copied to clipboard
Missing "connection state"
"connection.state" seems to be available, I wanted to test https://stackoverflow.com/questions/34542902/nodejs-mysql-how-to-know-connection-is-release-or-not
Can you describe your use case? Why do you need this state flag?
I use it one time by day (will change in the future). I don't know howto avoid this error
Promise { <pending> }
CRON for sending power to DATABASE done...
/home/pi/ProjetJS/velbusLib/node_modules/mysql2/promise.js:93
const localErr = new Error();
^
Error: Can't add new command when connection is in closed state
at PromiseConnection.query (/home/pi/ProjetJS/velbusLib/node_modules/mysql2/promise.js:93:22)
"closed" is not the same as "idle pool connection", closed means that underlying tcp connection is closed because of COM_CLOSE client command ( via connection.end() ) or server closing its side of connection
I'd recommend reviewing how you handle errors so that your code is discarding connection after fatal error, but state is based on this check: https://github.com/sidorares/node-mysql2/blob/3a250e51d20507be3293bbdeedbcb50811d23a3e/lib/connection.js#L168 you can probably replicate it in your code with something like conn.addCommand === conn.__addCommandClosedState ( this is not documented api, might be changed in the future )
I have a Jest unit test that does this with mysql:
expect( iConn.state ).toBe( 'authenticated' );
What should I use instead with mysql2?
I have a Jest unit test that does this with mysql:
expect( iConn.state ).toBe( 'authenticated' );What should I use instead with mysql2?
Digging through the code I determined that connection.authorized === true works as a replacement for connection.state === "authorized".