Skip to content

#109 pool release fix #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 21, 2017
Merged
Show file tree
Hide file tree
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
11 changes: 6 additions & 5 deletions lib/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function mixinTransaction(PostgreSQL) {
debug('Begin a transaction with isolation level: %s', isolationLevel);
this.pg.connect(function(err, connection, done) {
if (err) return cb(err);
connection.autorelease = done;
connection.query('BEGIN TRANSACTION ISOLATION LEVEL ' + isolationLevel,
function(err) {
if (err) return cb(err);
Expand Down Expand Up @@ -63,15 +64,15 @@ function mixinTransaction(PostgreSQL) {
};

PostgreSQL.prototype.releaseConnection = function(connection, err) {
if (typeof connection.release === 'function') {
connection.release(err);
connection.release = null;
if (typeof connection.autorelease === 'function') {
connection.autorelease(err);
connection.autorelease = null;
} else {
var pool = this.pg;
if (err) {
pool.destroy(connection);
pool.pool.destroy(connection);
} else {
pool.release(connection);
pool.pool.release(connection);
}
}
};
Expand Down
35 changes: 35 additions & 0 deletions test/postgresql.transaction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,41 @@ describe('transactions', function() {
};
}

describe('bulk', function() {
it('should work with bulk transactions', function(done) {
var completed = 0;
var concurrent = 20;
for (var i = 0; i <= concurrent; i++) {
var post = {title: 'tb' + i, content: 'cb' + i};
var create = createPostInTx(post);
Transaction.begin(db.connector, Transaction.SERIALIZABLE,
function(err, tx) {
if (err) return done(err);
Post.create(post, {transaction: tx},
function(err, p) {
if (err) {
done(err);
} else {
tx.commit(function(err) {
if (err) {
done(err);
}
completed++;
checkResults();
});
}
});
});
}

function checkResults() {
if (completed === concurrent) {
done();
}
}
});
});

describe('commit', function() {
var post = {title: 't1', content: 'c1'};
before(createPostInTx(post));
Expand Down