❔ updating row sometimes times out EF core 7
this has been confusing me lately, i was going thru my logs and i noticed sometimes my endpoint returns a 500.
I have looked thru the logs and found the query which is causing it.
its not every time but sometimes. the reason why I use this EF Linq query is because the servers rows get hit sometimes at the same time and the hitsReceived needs to be thread safe (locking).
this is an error from serilog
im using postgresql
13 Replies
sounds like a deadlocking issue of some kind
hitsReceived needs to be thread safe (locking)how are you achieving this?
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.Using a executeUpdateAsync
what is "a executeUpdateAsync"?
It updates the rows via the sql query.
https://learn.microsoft.com/en-us/ef/core/saving/execute-insert-update-delete
ExecuteUpdateAsync()
is not thread-safe
so, noThen what you suggest to make it thread safe
depends what your goal is
Distributed locks even work if multiple instances of the same app are running on different VMs. You can read more here: https://redis.io/docs/manual/patterns/distributed-locks/
But I think that in your case distributed lock is not needed. You could lock the database by using transactions
That would be the best approach, as it is done on database level
I’ll try to implement the transaction for that specific update query and see if it decreases my error rate
You can also take a look at the isolation levels and use the one you need: https://learn.microsoft.com/en-us/dotnet/api/system.data.isolationlevel?view=net-7.0&redirectedfrom=MSDN
IsolationLevel Enum (System.Data)
Specifies the transaction locking behavior for the connection.