Jump To …

mysql.js

lib/
var mysql = require("mysql")
, pool = require("generic-pool")
, mysqlConf = require("./dbConfig").mysql;

function replaceClientOnDisconnect(client) {
  client.on("error", function (err) {
    if (!err.fatal) {
      return;
    }

    console.log("trying to recycle an old connection");

    if (err.code !== "PROTOCOL_CONNECTION_LOST") {
      throw err;
    }

client.config is actually a ConnectionConfig instance, not the original configuration. For most situations this is fine, but if you are doing something more advanced with your connection configuration, then you should check carefully as to whether this is actually going to do what you think it should do.

      client = mysql.createConnection(client.config);
      replaceClientOnDisconnect(client);
      connection.connect(function (error) {
        if (error) {

Well, we tried. The database has probably fallen over. That's fairly fatal for most applications, so we might as call it a day and go home.

            process.exit(1);
          }
      });
  });
}
mysqlConf.createConnection = function() {
  var conn = mysql.createConnection(mysqlConf);
  replaceClientOnDisconnect(conn);
  return conn;
};
exports.clientPool = mysql.createPool(mysqlConf);
exports.clientPool.acquire = exports.clientPool.getConnection;

exports.clientPool = pool.Pool({ name: "MySQL", create: function (callback) { var client = mysql.createConnection(mysqlConf); client.connect(function (error) { if (!error) { replaceClientOnDisconnect(client); } callback(error, client); }); }, destroy: function(client) { client.end(); }, // Maximum number of concurrent clients. max: 11, // Minimum number of connections ready in the pool. // If set, then make sure to drain() on process shutdown. min: 1, // How long a resource can stay idle before being removed. idleTimeoutMillis: 25000, // Use console.log if true, but it can also be function (message, level). log : false });

If a minimum number of clients is set, then process.exit() can hang unless the following listener is set. process.on("exit", function() { exports.clientPool.drain(function () { exports.clientPool.destroyAllNow(); }); });

generated Tue Apr 30 2013 17:31:02 GMT-0400 (EDT)
Modfinder