Wednesday, 28 October 2020

mysql pool.getConnection fails breaks the code

im new to nodejs so bare with me , im trying to to some connection heavy task like a game server i have a mysql poll and a function create_game which will use the poll to get a new connection

so i noticed sometimes the nodejs will stop working for no reason , i traced to problem in create_game function by putting numbers on each line like

 console.log(` @-> ${debug_id} :1 `);
 console.log(` @-> ${debug_id} : 2 `);

to see which line is breaking my code

var pool  = mysql.createPool({
    connectionLimit : 20 ,
    host            : myEnv.parsed.DB_HOST,
    user            : myEnv.parsed.DB_USERNAME,
    password        : myEnv.parsed.DB_PASSWORD,
    database        : myEnv.parsed.DB_DATABASE
});





function create_game( debug_id ) {

    console.log(` @-> ${debug_id} : 1 `);

    let promise =  new Promise(function(resolve , reject ) {
    
        console.log(` @-> ${debug_id} : 2 `);

        pool.getConnection(function(err, connection) {
            console.log(` @-> ${debug_id} : 3 `);
            if (err) {
                console.log(` @-> ${debug_id} : 3E `);
                reject(err.sqlMessage);
            }
            else
            {
                connection.beginTransaction(function (err) { 
                
                  /// do stufff 

                 console.log(` @-> ${debug_id} : success `);


                });

            }

        });
    })

    .then(function (res) {

        consoleLog('create success');
    })
    .catch(function (error) {
        consoleLog('///////////ERROR///////////');
        consoleLog(error);

     });

}

this is what i got in the console

 @-> 7 : 1
 @-> 7 : 2
 @-> 7 : 3
 @-> 7 : 4
 @-> 7 : 5
 @-> 7 : 6
 @-> 7 : 7
 @-> 7 : 8
 @-> 7 : 9
 @-> 7 : 10
 @-> 7 : 11
 @-> 7 : 12
 @-> 7 : success

 @-> 8 : 1
 @-> 8 : 2

so based on the output it seems pool.getConnection is failing i assume becuz it hit the connectionLimit : 20 at that point

so what im doing wrong ? why the code breaks .. i mean i do have a code to handle pool.getConnection fail in here so why im not getting 3 and 3E output ?

        pool.getConnection(function(err, connection) {
            console.log(` @-> ${debug_id} : 3 `);
            if (err) {
                console.log(` @-> ${debug_id} : 3E `);
                reject(err.sqlMessage);
            }

i still see the socket connections in terminal being made so the node script still works ... but all the database related operations will stop working after this without any error or output



from mysql pool.getConnection fails breaks the code

No comments:

Post a Comment