Skip to content

Instantly share code, notes, and snippets.

@B4nan
Last active June 17, 2019 21:27
Show Gist options
  • Select an option

  • Save B4nan/5c0fb52c31fe2af55bfb3279e0cdff27 to your computer and use it in GitHub Desktop.

Select an option

Save B4nan/5c0fb52c31fe2af55bfb3279e0cdff27 to your computer and use it in GitHub Desktop.
Perssimistic locking in MikroORM
await em.transactional(async _em => {
await _em.findOne(Author, id, { lockMode: LockMode.PESSIMISTIC_WRITE });
});
// START TRANSACTION
// SELECT `e0`.* FROM `author` AS `e0` WHERE `e0`.`id` = ? FOR UPDATE
// COMMIT
const author = orm.em.findOne(Author, id);
// ...
await orm.em.transactional(async em => {
await em.lock(author, LockMode.PESSIMISTIC_READ);
});
// SELECT `e0`.* FROM `author` AS `e0` WHERE `e0`.`id` = ?
// START TRANSACTION
// SELECT 1 FROM `author` AS `e0` WHERE `e0`.`id` = ? LOCK IN SHARE MODE
// COMMIT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment