Skip to content

Commit 679fb05

Browse files
author
Janny
authored
Merge pull request #197 from zbarbuto/#109-pool-release-fix
#109 pool release fix
2 parents 3295dc2 + b618133 commit 679fb05

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

lib/transaction.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function mixinTransaction(PostgreSQL) {
2121
debug('Begin a transaction with isolation level: %s', isolationLevel);
2222
this.pg.connect(function(err, connection, done) {
2323
if (err) return cb(err);
24+
connection.autorelease = done;
2425
connection.query('BEGIN TRANSACTION ISOLATION LEVEL ' + isolationLevel,
2526
function(err) {
2627
if (err) return cb(err);
@@ -63,15 +64,15 @@ function mixinTransaction(PostgreSQL) {
6364
};
6465

6566
PostgreSQL.prototype.releaseConnection = function(connection, err) {
66-
if (typeof connection.release === 'function') {
67-
connection.release(err);
68-
connection.release = null;
67+
if (typeof connection.autorelease === 'function') {
68+
connection.autorelease(err);
69+
connection.autorelease = null;
6970
} else {
7071
var pool = this.pg;
7172
if (err) {
72-
pool.destroy(connection);
73+
pool.pool.destroy(connection);
7374
} else {
74-
pool.release(connection);
75+
pool.pool.release(connection);
7576
}
7677
}
7778
};

test/postgresql.transaction.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,41 @@ describe('transactions', function() {
5858
};
5959
}
6060

61+
describe('bulk', function() {
62+
it('should work with bulk transactions', function(done) {
63+
var completed = 0;
64+
var concurrent = 20;
65+
for (var i = 0; i <= concurrent; i++) {
66+
var post = {title: 'tb' + i, content: 'cb' + i};
67+
var create = createPostInTx(post);
68+
Transaction.begin(db.connector, Transaction.SERIALIZABLE,
69+
function(err, tx) {
70+
if (err) return done(err);
71+
Post.create(post, {transaction: tx},
72+
function(err, p) {
73+
if (err) {
74+
done(err);
75+
} else {
76+
tx.commit(function(err) {
77+
if (err) {
78+
done(err);
79+
}
80+
completed++;
81+
checkResults();
82+
});
83+
}
84+
});
85+
});
86+
}
87+
88+
function checkResults() {
89+
if (completed === concurrent) {
90+
done();
91+
}
92+
}
93+
});
94+
});
95+
6196
describe('commit', function() {
6297
var post = {title: 't1', content: 'c1'};
6398
before(createPostInTx(post));

0 commit comments

Comments
 (0)