NiCk Newman
NiCk Newman

Reputation: 1776

NodeJS Mysql Leak

Here is my code. I first used a simple try/catch block as I figured that was the issue, so then I switched to a callback based error handler:

var addblockeduser = function(x,y,next) {
    // if error condition?
    if ( y === 0 ) {
        // "throw" the error safely by calling the completion callback
        // with the first argument being the error
        next(new Error("Can't divide by zero"))
    }
    else {
        // no error occured, continue on
        next(null, x/y)

        db.query('SELECT 1', function(err){
                db.end();
        });


    }
}

addblockeduser(10, 0,    function(err, result){
    // did an error occur?
    if ( err ) {
        // handle the error safely
        console.log('4/2=err', err)
    }
    else {
        // no error occured, continue on
        console.log('4/2='+result)
    }
})

When I run this, I get the error that is called 'Cannot divide by zero'. BUT, when I go and check my MYSQL process list, it shows:

enter image description here

These random sleep processes that appear each time the code is executed. (If I run the code 20 times 20 new 'sleep' processes will appear) I am even using db.end() to close the connection, and that is still not working, any idea?

Upvotes: 0

Views: 587

Answers (1)

YlinaGreed
YlinaGreed

Reputation: 249

As far as I see, you never call "db.end()" in case of "y = 0", but only in the "else", maybe that's your problem here ?

Upvotes: 1

Related Questions