Nodejs and Mysql problems

Asked

Viewed 196 times

1

Hello I’m having problems with over-connecting with node-mysql2 using a connection pool. I would like to know if there is a better practice regarding implementation, and what is the best driver to use with nodejs and mysql?

Note: My application has a longpolling and many users.

  • Michael, the answer was right in your question?

1 answer

1

The most commonly used module I believe is the mysql

I wear it like this:

var mysql = require('mysql');
var pool = mysql.createPool({
    connectionLimit: 100,
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: 'nomedaBD',
    debug: false,
    charset: 'utf8_unicode_ci'
});

function query (query, data, callback) {
    if (typeof data == 'function') {
        callback = data;
        data = [];
    }
    pool.getConnection(function (err, connection) {
        if (err) return onError(connection, err, callback);
        connection.query(query, data || [], function (err, rows, fields) {
            if (err) return onError(connection, err, callback);
            connection.release();
            callback.call(this, null, rows, fields);
        });
    });
}

The plugin keeps the BD connection open and has a pool to manage connections as in the example above.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.