Corrected lock problem with postgres transactions

This commit is contained in:
Thomas FORGIONE 2015-08-20 10:42:30 +02:00
parent d13465e71f
commit 4dfd241064
1 changed files with 20 additions and 10 deletions

View File

@ -535,8 +535,12 @@ DBReq.ExpCreator = function(user_id, finishAction) {
// Connect to db // Connect to db
var self = this; var self = this;
pg.connect(pgc.url, function(err, client, release) { pg.connect(pgc.url, function(err, client, release) {
self.client = client; self.client = client;
self.release = release; self.release = release;
// Start transaction and lock table
self.client.query("BEGIN; LOCK CoinCombination; LOCK Experiment;");
self.execute(); self.execute();
}); });
}; };
@ -701,17 +705,23 @@ DBReq.ExpCreator.prototype.execute = function() {
* Release the DB connection and call the callback * Release the DB connection and call the callback
*/ */
DBReq.ExpCreator.prototype.finish = function() { DBReq.ExpCreator.prototype.finish = function() {
this.release(); var self = this;
this.client = null;
this.release = null;
this.finishAction( // Commit, and then release
this.finalResult.exp_id, this.client.query("COMMIT;", function() {
this.finalResult.coin_combination_id,
this.finalResult.scene_id, self.release();
this.finalResult.recommendation_style, self.client = null;
this.finalResult.coins self.release = null;
);
self.finishAction(
self.finalResult.exp_id,
self.finalResult.coin_combination_id,
self.finalResult.scene_id,
self.finalResult.recommendation_style,
self.finalResult.coins
);
});
}; };
/** /**