Skip to content

Instantly share code, notes, and snippets.

@kevinejohn
Last active August 29, 2015 14:21
Show Gist options
  • Save kevinejohn/1a52f9826320c3a49c10 to your computer and use it in GitHub Desktop.
Save kevinejohn/1a52f9826320c3a49c10 to your computer and use it in GitHub Desktop.
How do I row lock?
sequelize.transaction({
isolationLevel: Sequelize.Transaction.ISOLATION_LEVELS.SERIALIZABLE,
//autocommit: false, ???
}, function (t) {
return Config.findAll({
where: { assigned: false },
transaction: t,
//lock: t.LOCK.UPDATE, ???
//lock: t.LOCK.SHARE, ???
//lock: t.LOCK.KEY_SHARE ???
//lock: t.LOCK.NO_KEY_UPDATE ???
limit: 1,
}).then(function (configs) {
var config = configs[0];
config.updateAttributes({
assigned: true,
},{
where: { id: config.id },
transaction: t,
})
.then(function(updated_config) {
callback(null, updated_config);
})
}).catch(function (err) {
callback(err);
});
})
@BridgeAR
Copy link

Just comment one lock line out and use findOne e.g.:

      return Config.findOne({
        where: { assigned: false },
        transaction: t,
        lock: t.LOCK.UPDATE,
      }).then(function (config) {
        if (config) {
          ...
        }

That way the selected config set will be locked. Btw.: you should insert a return in line 16, otherwise you won't be able to catch errors that occur while updating, since you do not wait for the response.
I changed your call to a findOne as you inserted a limit: 1 in your findAll... but you have to check if there's a return value if you can't be certain that this entry exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment