Commit b663d234 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Share one MySQL between all requests.

The old comments backend worked just fine with one shared connection, so
we shouldn't get quickly into troubles with this approach.

Previously the number of open connections just kept growing and growing
because they never got closed.  At least this problem is now gone -
there's just one open connection which we never close.
parent ffe1054a
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
var mysql = require('mysql');

/**
 * @singleton
 * A pool of database connections.
 */
var ConnectionPool = {
    connections: {},

    /**
     * Returns a connection with a given name, or creates it if it
     * doesn't exist yet.
     *
     * @param {String} name An arbitrary name for the connection.
     * @param {Object} cfg A config for mysql.createConnection:
     * @param {String} cfg.user
     * @param {String} cfg.password
     * @param {String} cfg.database
     * @param {String} cfg.host
     */
    get: function(name, cfg) {
        if (!this.connections[name]) {
            this.connections[name] = mysql.createConnection(cfg);
        }

        return this.connections[name];
    }
};

module.exports = ConnectionPool;
+4 −17
Original line number Diff line number Diff line
var mysql = require('mysql');

/**
 * A facade in front of node-mysql providing several methods in
 * addition to simple #query method that node-mysql provides.
 *
 * @constructor
 * Initializes the database connection with given config options.
 * Initializes the database facade with a connection object.
 *
 * @param {Object} cfg A config for mysql.createConnection:
 * @param {String} cfg.user
 * @param {String} cfg.password
 * @param {String} cfg.database
 * @param {String} cfg.host
 * @param {mysql.Connection} connection
 */
function DbFacade(cfg) {
    this.connection = mysql.createConnection(cfg);
function DbFacade(connection) {
    this.connection = connection;
}

DbFacade.prototype = {
@@ -75,13 +69,6 @@ DbFacade.prototype = {
        this.query(["UPDATE "+table+" SET ? WHERE id = ?"], [fieldsWithoutId, id], callback);
    },

    /**
     * Closes the database connection.
     */
    end: function() {
        this.connection.end();
    },

    /**
     * Replaces ? placeholders in SQL string.
     *
+5 −2
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ var Comments = require('./comments');
var Users = require('./users');
var Subscriptions = require('./subscriptions');
var ForumAuth = require('./forum_auth');
var ConnectionPool = require('./connection_pool');
var config = require('../config');

/**
@@ -35,7 +36,8 @@ TableFactory.prototype = {
     */
    users: function() {
        return this.cache("users", function() {
            var forumAuth = new ForumAuth(new DbFacade(config.forumDb));
            var connection = ConnectionPool.get("users", config.forumDb);
            var forumAuth = new ForumAuth(new DbFacade(connection));
            return new Users(this.database(), forumAuth);
        });
    },
@@ -52,7 +54,8 @@ TableFactory.prototype = {

    database: function() {
        return this.cache("database", function() {
            return new DbFacade(config.mysql);
            var connection = ConnectionPool.get("comments", config.mysql);
            return new DbFacade(connection);
        });
    },

+4 −3
Original line number Diff line number Diff line
describe("Comments", function() {
    var mysql = require("mysql");
    var Comments = require("../lib/comments");
    var DbFacade = require('../lib/db_facade');
    var connection;
    var comments;

    beforeEach(function() {
        connection = new DbFacade({
        connection = mysql.createConnection({
            host: 'localhost',
            user: '',
            password: '',
            database: 'comments_test'
        });

        comments = new Comments(connection, "ext-js-4");
        comments = new Comments(new DbFacade(connection), "ext-js-4");
    });

    afterEach(function() {
@@ -175,7 +176,7 @@ describe("Comments", function() {

    describe("when initializing Comments to other domain", function() {
        beforeEach(function() {
            comments = new Comments(connection, "touch-2");
            comments = new Comments(new DbFacade(connection), "touch-2");
        });

        it("#count gets total number of comments in the other domain", function(done) {
+3 −2
Original line number Diff line number Diff line
describe("Subscriptions", function() {
    var mysql = require("mysql");
    var Subscriptions = require("../lib/subscriptions");
    var DbFacade = require('../lib/db_facade');
    var connection;
    var subscriptions;

    beforeEach(function() {
        connection = new DbFacade({
        connection = mysql.createConnection({
            host: 'localhost',
            user: '',
            password: '',
            database: 'comments_test'
        });

        subscriptions = new Subscriptions(connection, "ext-js-4");
        subscriptions = new Subscriptions(new DbFacade(connection), "ext-js-4");
    });

    afterEach(function() {