Skip to content

Instantly share code, notes, and snippets.

@drenther
Last active March 13, 2019 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drenther/443f02b8cd1395909ed3471b0f4b39e4 to your computer and use it in GitHub Desktop.
Save drenther/443f02b8cd1395909ed3471b0f4b39e4 to your computer and use it in GitHub Desktop.
Example of a WriteConflict and concurrency lock based errors
async function updateUserAge(userName, session) {
// update the user
}
// SENARIO - 1
// this will throw a WriteConflict error because of Concurrency Lock
await Promise.all([
runInTransaction(async session => { await updateUserAge('John', session) }),
runInTransaction(async session => { await updateUserAge('John', session) }),
]);
// SCENARIO - 2
// this is also likely cause an error as they might concurrently try to edit the same entry
// even though they are in the same session
await runInTransction(async session => {
await Promise.all([
updateUserAge('John', session),
updateUserAge('John', session),
]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment