Last active
August 29, 2015 14:21
-
-
Save kevinejohn/1a52f9826320c3a49c10 to your computer and use it in GitHub Desktop.
How do I row lock?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just comment one lock line out and use findOne e.g.:
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.