slimboy
slimboy

Reputation: 1653

Unlock Table from forUpdate lock

I'm using "for update" to prevent race issues in my selection. You mention that The locks are saved in a table called "tableName_lock" with single column called is_locked that you need to set to 0 in order to release the lock.

My table is called requests, so I created a table called "requests_lock" and created a column called is_locked. that table never gets updated when I use for update. Is there anything I'm missing here?

bookshelf.knex.transaction(function(trx) {

 var query = bookshelf.knex('requests').transacting(trx).forUpdate().select('requests.id', 'requests.CreatedTime').then(function (user_roles) {
            console.log('user_friends: %j', user_roles);  
}).then(trx.commit)
  .catch(trx.rollback);
});

Upvotes: 0

Views: 2893

Answers (1)

Mikael Lepistö
Mikael Lepistö

Reputation: 19728

SELECT FOR UPDATE locks the selected rows until the transaction ends https://www.postgresql.org/docs/9.0/static/sql-select.html#SQL-FOR-UPDATE-SHARE. The rows selected for update cannot be selected by any other db connection until the transaction, which locked the rows is ended.

There is no separate locking table used.

The migrationTableName_lock table mentioned in question is used only to prevent migrations from being ran from multiple different clients at the same time and it has nothing to do with .forUpdate().

Upvotes: 2

Related Questions