Skip to content

Commit

Permalink
Release connections instead of closing them. Also handle connection e…
Browse files Browse the repository at this point in the history
…rrors.
  • Loading branch information
Mathieu Cartoixa committed Mar 2, 2016
1 parent 84932ba commit 83ef93e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
27 changes: 11 additions & 16 deletions lib/connect-tedious.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,12 @@ module.exports = function(connect){
store.pool.acquire(function(err, db) {
if (err)
return fn(err);
initConnection(db);

var r = new tedious.Request(
'DELETE FROM ' + store.tableName + ' WHERE ' + store.expiresColumnName + '<SYSUTCDATETIME()',
function(err) {
debug.sql('Executed DELETE');
db.close();
db.release();
if (err)
return fn(err);
fn(null, true);
Expand Down Expand Up @@ -131,15 +130,17 @@ module.exports = function(connect){
this.sessColumnName=options.sessColumnName || '[Sess]';
this.expiresColumnName=options.expiresColumnName || '[Expires]';

this.pool=new tediousPool(
{
this.pool=new tediousPool({
name: 'connect-tedious',
min: options.minConnections,
max: options.maxConnections,
idleTimeout: options.idleTimeout || 30000
},
options.config
);
this.pool.on('error', function(err) {
console.error('ERROR '+err);
});

dbCleanup(this, function (err) {
if (err) console.log('ERROR ' + err);
Expand All @@ -165,13 +166,12 @@ module.exports = function(connect){
self.pool.acquire(function(err, db) {
if (err)
return fn(err);
initConnection(db);

var r = new tedious.Request(
'SELECT s.' + self.expiresColumnName + ', s.' + self.sessColumnName + ' FROM ' + self.tableName + ' s WHERE s.' + self.sidColumnName + '=@sid AND s.' + self.expiresColumnName + '>=SYSUTCDATETIME()',
function(err, rowCount) {
debug.sql('Executed SELECT');
db.close();
db.release();
if (err)
return fn(err);
if (!rowCount || rowCount!==1)
Expand Down Expand Up @@ -216,7 +216,6 @@ module.exports = function(connect){
self.pool.acquire(function(err, db) {
if (err)
return fn(err);
initConnection(db);

var duration = sess.cookie.maxAge || oneDay;
var r = new tedious.Request(
Expand All @@ -226,7 +225,7 @@ module.exports = function(connect){
' WHEN NOT MATCHED THEN INSERT (' + self.sidColumnName + ', ' + self.sessColumnName + ', ' + self.expiresColumnName + ') VALUES (@sid, @sess, DATEADD(ms, @duration, SYSUTCDATETIME()));',
function(err) {
debug.sql('Executed MERGE');
db.close();
db.release();
fn.apply(self, arguments);
}
);
Expand All @@ -251,13 +250,12 @@ module.exports = function(connect){
self.pool.acquire(function(err, db) {
if (err)
return fn(err);
initConnection(db);

var r = new tedious.Request(
'DELETE s FROM ' + self.tableName + ' s WHERE s.' + self.sidColumnName + '=@sid',
function(err) {
debug.sql('Executed DELETE');
db.close();
db.release();
if (err)
return fn(err);
return fn(err, true);
Expand All @@ -282,13 +280,12 @@ module.exports = function(connect){
self.pool.acquire(function(err, db) {
if (err)
return fn(err);
initConnection(db);

var r = new tedious.Request(
'SELECT @count=COUNT(*) FROM ' + self.tableName,
function(err, rowCount) {
debug.sql('Executed SELECT');
db.close();
db.release();
if (err)
return fn(err);
if (!rowCount || rowCount!==1)
Expand Down Expand Up @@ -320,13 +317,12 @@ module.exports = function(connect){
self.pool.acquire(function(err, db) {
if (err)
return fn(err);
initConnection(db);

var r = new tedious.Request(
'TRUNCATE TABLE ' + self.tableName,
function(err) {
debug.sql('Executed TRUNCATE');
db.close();
db.release();
if (err)
return fn(err);
fn(null, true);
Expand All @@ -352,15 +348,14 @@ module.exports = function(connect){
self.pool.acquire(function (err, db) {
if (err)
return fn(err);
initConnection(db);

var duration = sess.cookie.maxAge || oneDay;

var r = new tedious.Request(
'UPDATE ' + self.tableName + ' SET ' + self.expiresColumnName + '=DATEADD(ms, @duration, SYSUTCDATETIME()) WHERE ' + self.sidColumnName + '=@sid',
function (err) {
debug.sql('Executed UPDATE');
db.close();
db.release();
if (err)
return fn(err);
fn(null, true);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"keywords": ["sql-server", "tds", "connection", "session", "store"],
"dependencies": {
"node-connection-string-builder": "^0.0.1",
"tedious": "^1.13.1",
"tedious": "^1.13.2",
"tedious-connection-pool": "^0.3.9",
"debug": "^2.2.0"
},
Expand All @@ -25,5 +25,5 @@
{
"type": "MIT"
}
]
]
}

0 comments on commit 83ef93e

Please sign in to comment.