Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 66 additions & 39 deletions lib/connect-db2.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,41 +111,41 @@ module.exports = function (session) {
/**
* Initialize Db2Store with the given `options`.
*
* @param {Object} opt
* @param {Object} [connection] an ibmdb connection.
* @param {Object} opt options
* @param {Object} [connectionPool] an initialised ibmdb connection Pool.
* @api public
*/

var Db2Store = function (opt, connection) {
var Db2Store = function (opt, connectionPool) {
if (!(this instanceof Db2Store)) {
throw new TypeError('Cannot call Db2Store constructor as a function');
}

var self = this;

this._options = extend(true, {}, defaultOptions, opt || {});
self._options = extend(true, {}, defaultOptions, opt || {});

Store.call(this, this._options);
Store.call(this, self._options);

if (connection) {
debug('Using supplied connection, connected: %s', connection.connected);
this._client = connection;
if (!this._client.connected) {
var err = new Error('The supplied db connection is not open');
if (connectionPool) {
debug('Using supplied connection pool of type %s', typeof connectionPool);
if (self._connPool.connected !== undefined) {
var err = new Error('Please supply an initialised IBM db connection Pool, require(\'ibm_db\').Pool instance');
throw err;
}
self._connPool = connectionPool;
} else {
var dsn = this._options.use_ssl === true ? this._options.ssldsn : this._options.dsn;
var dsn = self._options.use_ssl === true ? self._options.ssldsn : this._options.dsn;

if (!dsn) {
dsn = 'DRIVER={DB2};DATABASE=' + this._options.database +
';UID=' + this._options.user +
';PWD=' + this._options.password +
';HOSTNAME=' + this._options.host +
';PORT=' + this._options.port +
dsn = 'DRIVER={DB2};DATABASE=' + self._options.database +
';UID=' + self._options.user +
';PWD=' + self._options.password +
';HOSTNAME=' + self._options.host +
';PORT=' + self._options.port +
';PROTOCOL=TCPIP;';

if (this._options.use_ssl) {
if (self._options.use_ssl) {
dsn += 'Security=SSL;';
}
}
Expand All @@ -156,8 +156,18 @@ module.exports = function (session) {

//debug('Using dsn "%s"', dsn);

self._dsn = dsn;
try {
self._client = ibmdb.openSync(dsn);
debug('Initialising a new connection pool...');
self._connPool = new ibmdb.Pool(self._options);
var result = self._connPool.init(1, self._dsn);
debug('Connection pool init returned ', result);
if (!result) {
throw new Error('Failed to initialise connection pool; result= ' + result);
}
if (result instanceof Error) {
throw result;
}
} catch (err) {
debug('dashDB returned err', err);
throw err;
Expand All @@ -171,6 +181,29 @@ module.exports = function (session) {

util.inherits(Db2Store, Store);

/**
* Attempts to get a connection from the pool and run the supplied sql query against it
*
* @param {String} sql
* @param {Array} values
* @param {Function} cb
* @api private
*/
Db2Store.prototype._query = function (sql, values, cb) {
var store = this;
store._connPool.open(store._dsn, function (err, conn) {
if (err) {
debug('Failed to get open connection from pool, %s', sql, err);
return cb(err);
}

return conn.query(sql, values, function (err, results) {
conn.close();
return cb(err, results);
});
});
};

/**
* Attempt to fetch session by the given `sid`.
*
Expand All @@ -193,7 +226,7 @@ module.exports = function (session) {
store._options.schema.columnNames.session_id,
store._options.schema.columnNames.expires);

store._client.query(sql, [sid, expires], function (err, rows) {
store._query(sql, [sid, expires], function (err, rows) {

if (err) {
debug('Failed to get session');
Expand Down Expand Up @@ -246,7 +279,7 @@ module.exports = function (session) {
store._options.schema.tableName,
store._options.schema.columnNames.session_id);

store._client.query(sql, [sid], function (err, rows) {
store._query(sql, [sid], function (err, rows) {
if (err) {
debug('Failed to determine if session "%s" already exists', sid);
debug(err);
Expand Down Expand Up @@ -277,7 +310,7 @@ module.exports = function (session) {
debug('Session "%s" will be inserted', sid);
}

store._client.query(sql, params, function (err) {
store._query(sql, params, function (err) {
if (err) {
debug('Insert/Update failed for session "%s"', sid);
debug(err);
Expand Down Expand Up @@ -306,7 +339,7 @@ module.exports = function (session) {
store._options.schema.tableName,
store._options.schema.columnNames.session_id);

store._client.query(sql, [sid], function (err) {
store._query(sql, [sid], function (err) {
if (err) {
debug('Failed to destroy session data');
debug(err);
Expand Down Expand Up @@ -341,7 +374,7 @@ module.exports = function (session) {
store._options.schema.columnNames.expires,
store._options.schema.columnNames.session_id);

store._client.query(sql, [expires, sid], function (err) {
store._query(sql, [expires, sid], function (err) {
if (err) {
debug('Failed to touch session');
debug(err);
Expand All @@ -367,7 +400,7 @@ module.exports = function (session) {

var sql = util.format('SELECT COUNT(*) AS "length" FROM "%s"', store._options.schema.tableName);

store._client.query(sql, function (err, rows) {
store._query(sql, function (err, rows) {

if (err) {
debug('Failed to get number of sessions');
Expand Down Expand Up @@ -396,7 +429,7 @@ module.exports = function (session) {

var sql = util.format('DELETE FROM "%s"', store._options.schema.tableName);

store._client.query(sql, function (err) {
store._query(sql, function (err) {
if (err) {
debug('Failed to clear all sessions');
debug(err);
Expand Down Expand Up @@ -426,7 +459,7 @@ module.exports = function (session) {
store._options.schema.tableName,
store._options.schema.columnNames.expires);

store._client.query(sql, [expires], function (err) {
store._query(sql, [expires], function (err) {
if (err) {
debug('Failed to clear expired sessions');
debug(err);
Expand All @@ -438,7 +471,7 @@ module.exports = function (session) {
};

/**
* Close the underlying database connection.
* Close the underlying database connection pool and its connections.
*
* @param {Function} [fn]
* @api public
Expand All @@ -450,13 +483,7 @@ module.exports = function (session) {

debug('Closing session store');

if (!store._client.connected) {
return process.nextTick(function () {
fn();
});
}

store._client.close(function (err) {
store._connPool.close(function (err) {
if (err) {
debug(err);
return fn(err);
Expand All @@ -483,7 +510,7 @@ module.exports = function (session) {

var sql = util.format('SELECT COUNT(*) AS "count" FROM SYSCAT.TABLES WHERE "TABNAME" = ?');

store._client.query(sql, [store._options.schema.tableName], function (err, rows) {
store._query(sql, [store._options.schema.tableName], function (err, rows) {

if (err) {
debug('Failed to get %s table details', store._options.schema.tableName);
Expand Down Expand Up @@ -529,15 +556,15 @@ module.exports = function (session) {
store._options.schema.columnNames.expires
);

store._client.query(sql, function (err) {
store._query(sql, function (err) {

if (err) {
debug('Failed to create session table');
debug(err);
return fn(err);
}

store._client.query(idx);
store._query(idx);

return fn();
});
Expand Down Expand Up @@ -567,7 +594,7 @@ module.exports = function (session) {

var sql = util.format('DROP TABLE "%s"', store._options.schema.tableName);

store._client.query(sql, function (err) {
store._query(sql, function (err) {

if (err) {
debug('Failed to drop session table');
Expand All @@ -580,4 +607,4 @@ module.exports = function (session) {
};

return Db2Store;
};
};