question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Row update with DB Row Lock (SELECT FOR UPDATE)

See original GitHub issue

Problem

There’s an entire class of DB mutations that can be affected by race conditions. An example is incrementing a value by one.

So when updating a row, we need a way to acquire a DB row lock to prevent these types of race conditions.

Solution

I think this could be a good solution. If data is a function, then SELECT FOR UPDATE is used.

await prisma.counter.update({where: {id: 1}, data: counter => ({value: counter.value + 1})})

Alternatives

Or accept the above arguments with a new API like prisma.count.updateWithLock()

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:16
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

17reactions
Syttencommented, Jul 8, 2020

I kind of disagree with this closing this, row locking is important and one step in providing more control to dev since long running transactions are out of the question for now. That could allow chaining of a findOne and update where the update takes some data from the findOne.

5reactions
schicklingcommented, Mar 24, 2020

We’ve already drafted a Client API improved for this use case in this issue:

await prisma.users.findMany().update({ version: u => u.version.inc(5) })
await prisma.users.findMany().update({ version: u => u.version.dec(5) })
await prisma.users.findMany().update({ version: u => u.version.mul(5) })
await prisma.users.findMany().update({ version: u => u.version.div(5) })
await prisma.users.findMany().update({ version: u => u.version.mod(5) })
await prisma.users.findMany().update({ version: u => u.version.pow(5, 10) })
await prisma.users.findMany().update({ version: u => u.version.min(5, 10) })
await prisma.users.findMany().update({ version: u => u.version.max(5, 10) })

This won’t be implemented using a DB lock but by mapping to native atomic DB operations.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Lock a Row: SELECT FOR UPDATE - Oratable
In Oracle, adding a FOR UPDATE clause to a SELECT statement lets you lock the selected rows. Other users cannot lock or update...
Read more >
What is SELECT FOR UPDATE in SQL (with examples)?
SELECT FOR UPDATE is a SQL command that's useful in the context of transactional workloads. It allows you to “lock” the rows returned...
Read more >
Rows locks from select for update clause - Ask TOM
Rows locks from select for update clause Hi Tom,From my knowledge of oracle, i understand that SELECT FOR UPDATE clause acquires row locks....
Read more >
MySQL 8.0 Reference Manual :: 15.7.2.4 Locking Reads
A SELECT ... FOR UPDATE reads the latest available data, setting exclusive locks on each row it reads. Thus, it sets the same...
Read more >
Transaction locking and row versioning guide - SQL Server
Lost updates occur when two or more transactions select the same row and then update the row based on the value originally selected....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found