Mysql: Remove query callback when release the connection

Created on 28 Jul 2017  路  4Comments  路  Source: mysqljs/mysql

I don't know if is it a good practice to define custom pools for specific operations in the application or not.
Here is an example

const mysql = require('mysql');
const pools = {};

function getPool(name) {

  if (!pools[name])
    pools[name] = mysql.createPool({
      host: 'localhost',
      user: 'root',
      connectionLimit: 5,
    });

  return pools[name];

}

class TestClss {

  constructor(i) {
    this.fetched = false;
    this.i = i;
  }

  start() {
    getPool("test-pool").query("SELECT SLEEP(1);", () => {
      console.log("this.i", this.i);
      this.fetched = true;
    })
  }

}

function start() {
  for (let i = 0; i < 10; i++) {
    let obj = new TestClss(i);
    obj.start();
  }
}

start();

After all queries has been finished I took a snapshot of heap and found that 5 TestClss objects weren't garbage collected because it is has binding with query callback -> poolConnection -> pool -> pools(global variable)

screenshot from 2017-07-28 08-07-11

I know that it will be kept until next query then replaced with new callback but the problem If I set the connection limit to large number, and has multiple pools, it will be alot of objects without beeing garbage collected.

Thanks.

bug

All 4 comments

Ah, I see. I believe this can be fixed, I'm taking a look.

Yea, can be fixed. The easiest fix is a change to a private property, which shouldn't be an issue (though can revisit later if it is for a different fix). But yea, I was able to reproduce and will push up a fix in just a bit to the repo.

Ok, sorry for the delay. I just pushed up the fix. Please feel free to test it out and let me know if the issue wasn't fixed. I'll release it as a patch even if I don't hear back, so don't feel pressed to test :)

@abou7mied just pushed in 2.14.1

Was this page helpful?
0 / 5 - 0 ratings

Related issues

EdoardoPedrotti picture EdoardoPedrotti  路  3Comments

winzig picture winzig  路  4Comments

PeppeL-G picture PeppeL-G  路  3Comments

macias picture macias  路  3Comments

johnrc picture johnrc  路  3Comments