question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why does sequelize create new connection for each query?

See original GitHub issue

It seems, sequelize creates new connection for each query instead of using existing ones from pool.

What you are doing?

I’m sending sleep queries in every two seconds.

var sequelize = new Sequelize(options.database, options.user, options.password, {
    host: options.host,
    dialect: 'mysql',
    port: options.port,
    pool: {
        max: 10,
        min: 2,
        idle: 10000
    }
});

setInterval(function () {
    sequelize.query('SELECT SLEEP(1);');
}, 2000);

What do you expect to happen?

I would except all these queries to get the same connection from the pool

What is actually happening?

When I check the mysql processes, I see different connection ids for each sleep query.

| 12973820 | admin         | 10.179.195.173:34544 | FieldServices | Query   |    0 | User sleep | SELECT SLEEP(1)  |         0 |             0 |
| 12973822 | admin         | 10.179.195.173:34545 | FieldServices | Query   |    0 | User sleep | SELECT SLEEP(1)  |         0 |             0 |
| 12973818 | admin         | 10.179.195.173:34545 | FieldServices | Query   |    1 | User sleep | SELECT SLEEP(1)  |         0 |             0 |

But when I use default mysql pool, all my queries use the same connection:

var mysql = require('mysql');
var pool  = mysql.createPool({
    connectionLimit : 10,
    host            : !options.tunnel ? options.host : "127.0.0.1",
    user            : options.user,
    password        : options.password,
    database        : options.database,
    port: !options.tunnel ? options.port : config.get('tunnel.' + options.tunnel).localPort,
});

setInterval(function () {
    pool.query('SELECT SLEEP(1);', function (err, rows, fields) {
        if (err) throw err;
        console.log('sleep');
    });
}, 2000);
| 12975966 | admin         | 10.179.195.173:36196 | FieldServices | Query   |    1 | User sleep | SELECT SLEEP(1)  |         0 |             0 |
| 12975966 | admin         | 10.179.195.173:36196 | FieldServices | Query   |    0 | User sleep | SELECT SLEEP(1)  |         0 |             0 |
| 12975966 | admin         | 10.179.195.173:36196 | FieldServices | Query   |    0 | User sleep | SELECT SLEEP(1)  |         0 |             0 |

Dialect: mysql Database version: 5.5.40 Sequelize version: 4.0.0-1

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:16 (9 by maintainers)

github_iconTop GitHub Comments

12reactions
felixfbeckercommented, Sep 8, 2016

Connections are cycled round-robin style, so it’s not always connection A that gets 90% of the queries while connection B, C, D idle mostly.

8reactions
felixfbeckercommented, Sep 13, 2016

Imo tracking bugs for drivers and printing bugs for every version is out of scope for sequelize. The first thing you should do when you encounter a bug is npm update 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connection Pool - Sequelize
If you're connecting to the database from multiple processes, you'll have to create one instance per process, but each instance should have a...
Read more >
Does Sequelize open a new connection to the database for ...
If the app needs more connections and the current number of connections is less than max, sequelize will attempt to create a new...
Read more >
How To Use Sequelize with Node.js and MySQL - DigitalOcean
Sequelize is a Node.js-based Object Relational Mapper that makes it easy to work with MySQL, MariaDB, SQLite, PostgreSQL databases, and more ...
Read more >
The Ultimate Guide To Get Started With Sequelize ORM
Model synchronization is the process to tell Sequelize to automatically perform an SQL query to the database and check that the columns ...
Read more >
MySQL Connection With Node.js Using Sequelize and Express
Larger SQL databases will be challenging to manage but with the help of sequelize, you can recover them. Sequelize is an object-relational mapper...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found