CodeFoodPixels / node-promise-mysql

A wrapper for mysqljs/mysql that wraps function calls with Bluebird promises.
MIT License
338 stars 64 forks source link

Node process never exits if connection.end() isn't called #51

Closed tomsaleeba closed 7 years ago

tomsaleeba commented 7 years ago

Steps:

mysql.createConnection({ host: process.env.DBURL, port: process.env.DBPORT, user: process.env.DBUSER, password: process.env.DBPASS, database: process.env.DBNAME }).then(function(conn){ connection = conn return connection.query('select 1;') }).then(function(rows){ //connection.end() // doco example doesn't show calling end() console.log(rows) })



Expected:
The query executes, data is available in the then(), the data is logged out to the console and the Node process exits.

Actual:
Most of that happens but the Node process never exits. I have different behaviour depending on the version of Node on my Linux machine:
v6.10.3:
I can't control+c the process, I need to `kill` the pid. Also, nothing is written to the console until you kill the process then it all appears.
v7.10.0:
The output is written to the console as it should (as the code is executed presumably) and I can control+c to end Node but the process still never exits on its own.
If you uncomment that `connection.end()` call, it all works as it should.

There's not necessaryily anything wrong with `node-promise-mysql`, it's just my fault for not thinking to call `.end()`. Maybe the doco just needs updating to indicate that calling end() is required to save other people trying to track down this issue. It wasn't particularly obvious to me but then I am a n00b Node programmer.

Happy to create a PR if you'd like. Thanks for the great module BTW.
tomsaleeba commented 7 years ago

Alternatively you can also close the connection after the query is done but then you can't use that same connection further down the chain. Might not suit everyone.

'use strict'
var mysql = require('promise-mysql');

mysql.createConnection({
    host: process.env.DBURL,
    port: process.env.DBPORT,
    user: process.env.DBUSER,
    password: process.env.DBPASS,
    database: process.env.DBNAME
}).then(function(conn){
    let result = conn.query('select 1;')
    conn.end()
    return result
}).then(function(rows){
    console.log(rows)
})
CodeFoodPixels commented 7 years ago

Thanks for letting me know! If you want to make a PR to update the docs then go ahead, otherwise I'll try and update it myself we at some point.

On Wed, 24 May 2017, 07:10 Tom Saleeba, notifications@github.com wrote:

Alternatively you can also close the connection after the query is done but then you can't use that same connection further down the chain. Might not suit everyone.

'use strict' var mysql = require('promise-mysql');

mysql.createConnection({ host: process.env.DBURL, port: process.env.DBPORT, user: process.env.DBUSER, password: process.env.DBPASS, database: process.env.DBNAME }).then(function(conn){ let result = conn.query('select 1;') conn.end() return result }).then(function(rows){ console.log(rows) })

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/lukeb-uk/node-promise-mysql/issues/51#issuecomment-303619283, or mute the thread https://github.com/notifications/unsubscribe-auth/AEjy1BOgsKfaS2kJLjd3xuqzwOj3amC0ks5r87u1gaJpZM4NkmnO .

tomsaleeba commented 7 years ago

PR created :+1:

CodeFoodPixels commented 7 years ago

Fixed in #52