Skip to content

Commit

Permalink
Beginings of pooling functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
pekim committed Oct 3, 2012
1 parent a1ef606 commit 1919496
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
25 changes: 20 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
var Connection = require('tedious').Connection;
var util = require('util');

function PooledConnection(config) {
function PooledConnection(pool, config) {
Connection.call(this, config);
this.pool = pool;
}

util.inherits(PooledConnection, Connection);

PooledConnection.prototype.close = function() {
this._close();
this.emit('end');
this.pool.connectionAvailable(this);
}

PooledConnection.prototype._close = function() {
Connection.prototype.close.call(this);
}

function ConnectionPool(config) {
function ConnectionPool(poolConfig, connectionConfig) {
var pool = this;

pool.config = poolConfig;
pool.activeConnections = [];
pool.availableConnections = [];

pool.connectionAvailable = function(connection) {
this.activeConnections.splice(this.activeConnections.indexOf(connection), 1);
};

return {
getConnection: function() {
return new PooledConnection(config);
Connection: function() {
var connection = new PooledConnection(pool, connectionConfig);
pool.activeConnections.push(connection);

return connection;
}
};
};
Expand Down
15 changes: 9 additions & 6 deletions test/connection-pool.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var assert = require('assert')
var ConnectionPool = require('../index');

var config = {
var connectionConfig = {
userName: 'test',
password: 'test',
server: '192.168.1.212',
Expand All @@ -11,22 +11,25 @@ describe('ConnectionPool', function() {
describe('one connection', function() {
it('should connect', function(done) {

// var connection = new Connection(config);
// var connection = new Connection(connectionConfig);

// connection.on('connect', function(err) {
// done();
// });

var cp = new ConnectionPool(config);
var connection = cp.getConnection();
// console.log(connection);
var cp = new ConnectionPool({size: 2}, connectionConfig);
var Connection = cp.Connection;
//var connection = cp.getConnection();
var connection = new Connection();
//console.log(connection.pool);

connection.on('connect', function(err) {
connection.close();
// done();
});

connection.on('end', function(err) {
console.log('end')
// console.log('end')
done();
});
})
Expand Down

0 comments on commit 1919496

Please sign in to comment.