Row update with DB Row Lock (SELECT FOR UPDATE)
See original GitHub issueProblem
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:
- Created 3 years ago
- Reactions:16
- Comments:8 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
We’ve already drafted a Client API improved for this use case in this issue:
This won’t be implemented using a DB lock but by mapping to native atomic DB operations.