Support SELECT FOR UPDATE / UPDLOCK (pessimistic concurrency)
See original GitHub issueI want to garantee that only one Api Request (Database Transaction) can modify an entity at a given time. this could be done by selecting a row with “SELECT FOR UPDATE” inside a transaction.
Something like:
context.Books
.Where(b => b.Id == 1)
.SelectForUpdate(skipLocked: true)
.FirstOrDefault();
Would generate something like:
SELECT *
FROM books b
WHERE b.id = 1
FOR UPDATE SKIP LOCKED
LIMIT 1";
I think that EF itself does not need to track at any level that the entity was selected with a table hint, it only need to be able to express the SQL.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:26
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Managing concurrency when using SELECT-UPDATE ...
I'm pretty sure that adding a WITH (UPDLOCK, HOLDLOCK) to the SELECT will do the trick. The SERIALIZABLE transaction isolation level would seems ......
Read more >c# - Pessimistic locking in EF code first
The answer is: it is not supported yet in Entity Framework. ... This lock is compatible with SELECT but not with UPDATE or...
Read more >SELECT FOR UPDATE - 4Js
Microsoft™ SQL SERVER allows individual and exclusive row locking by using the (UPDLOCK) hint after the table names in the FROM clause: SELECT...
Read more >Need help: Locking hint (UPDLOCK)
If I use the locking hint UPDLOCK in the SELECT statement within a ... values and then hits the "update" key, then that...
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
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
Cross-database investigation on
SELECT FOR UPDATE
The
FOR UPDATE
clause makes the database lock rows which are selected by the query; other transactions which query a locked row withFOR UPDATE
(or UPDATE it) will wait until the first transaction ends.Comparison to optimistic concurrency:
PostgreSQL
Documentation
SELECT ... FOR SHARE
which blocks updates but allows otherFOR SHARE
locks (read-write lock).SQL Server
No SELECT FOR UPDATE syntax - the UPDLOCK table hint is used instead (documentation);
MariaDB
MariaDB documentation MySQL documentation
SQL: identical to PostgreSQL above
IN SHARE MODE
for read/write locking, like PostgreSQLFOR SHARE
.SQLite
Not supported (no concurrency/row locking)
Oracle
Documentation
Firebird
Documentation (some interaction with
WITH LOCK
feature)Based on the above, we could consider doing the following:
ForUpdate()
.FOR UPDATE
by default, with SQL Server overriding to generateUPDLOCK
instead.context.Blogs.Include(b => b.Posts).ForUpdate())
. This may require additional work in #26858.ForShare()
and other operators./cc @maumar
Another use-case: atomically update something with ExecuteUpdate. This can be done today with optimistic locking, but that requires retrying when the update fails; that means some sort of random delay + backoff strategy is needed, where pessimistic locking can solve this much more easily.