{"id":797,"date":"2024-02-23T12:08:43","date_gmt":"2024-02-23T06:38:43","guid":{"rendered":"http:\/\/codetheory.in\/?p=797---9a18c7d2-d845-42f6-845f-c7af992bf27b"},"modified":"2024-02-23T12:08:43","modified_gmt":"2024-02-23T06:38:43","slug":"fixing-node-mysql-error-cannot-enqueue-handshake-after-invoking-quit","status":"publish","type":"post","link":"https:\/\/codetheory.in\/fixing-node-mysql-error-cannot-enqueue-handshake-after-invoking-quit\/","title":{"rendered":"Fixing Node Mysql “Error: Cannot enqueue Handshake after invoking quit.”"},"content":{"rendered":"

TL;DR<\/strong> You need to establish a new connection by calling the createConnection<\/code> method after every disconnection.<\/p>\n

<\/p>\n

I was using the popular Mysql module for Node.js which is node-mysql<\/a> and everytime, after the first page load was encountering a weird error with full stack trace.<\/p>\n

\r\nError: Cannot enqueue Handshake after invoking quit.\r\n    at Protocol._validateEnqueue ......\r\n    ...\r\n    ...\r\n<\/pre>\n

Basically, the problem is that, if you’ve terminated an existing connection object then that cannot be re-connected afterwards.<\/p>\n

So the solution is to call the createConnection<\/code> method (followed by connect()<\/code>) to create a new connection whenever you have terminated the old\/existing connection.<\/p>\n

If you’re using the node-postgres<\/a> module for postgresql support in Node, then you’ll have to do something similar, i.e., call the connect<\/code> method with the connection string to create new connections if you terminate the old connection (during the previous process\/request) by calling pg.end()<\/code> or client.end()<\/code>.<\/p>\n

Note:<\/strong> If you’re serving web requests, then you shouldn’t be ending connections on every request. Just create a connection on server startup and use the connection\/client object to query all the time. You can listen on the error<\/code> event to handle server disconnection and for reconnecting purposes. Full code here<\/a>.<\/p>\n

You can do something similar with the postgres module’s clients if you’re creating them manually using pg.Client<\/code>. Although when using the built-in pool approach to serve web requests, call pg.connect()<\/code> on every request and you should be fine. Just make sure you’ve attached an event handler on the error<\/code> event to prevent Node from crashing if the postgres server stops\/restarts for some reason.<\/p>\n

Calling pg.connect()<\/code> will retrieve an existing pooled client or create a new one ONLY if all the pooled clients are busy and the pool is NOT full.<\/p>\n","protected":false},"excerpt":{"rendered":"

TL;DR You need to establish a new connection by calling the createConnection method after every disconnection.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"_links":{"self":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/797"}],"collection":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/comments?post=797"}],"version-history":[{"count":8,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/797\/revisions"}],"predecessor-version":[{"id":184713,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/797\/revisions\/184713"}],"wp:attachment":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/media?parent=797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/categories?post=797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/tags?post=797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}