I have multiple servers running the same application (distributed container). That application modifies a SQL Server table, so what it does, it checks one table for a flag (e.g. status = open
), blocks some of those (there is a top(10) for performance concerns) and handles those open rows. When finished it sets the flag accordingly (highly simplified)
The issue I'm running in right now is, that randomly some server crash with
Transaction was deadlocked on lock resources with another process and has been chosen as the deadlock victim
I assume this is a SQL Server error. So my question: When starting with
using (var connection = new SqlConnection(connectionString))
And dapper, like
connection.Query("my Query");
Are all queries treated like in one SQL Transaction up until the using ends? Does this here BeginTransaction solve the problem or is everything being treated as a nested transaction?
It is really hard to test, my guess is, that this issue arises every 10th execution or so. The whole procedure requires to handle HTTP requests to another server that might last longer or shorter, so the whole procedure can take up to some hundred milliseconds or several seconds (I have no influence on that).
Update
So the app (doesn't help to show the real code) does something like
connection.Query(query1);
doOtherStuff
connection.Query(query2);
doOtherStuff
connection.Query(query3);
doOtherStuff
This other stuff is async (sometimes), however, each query can be executed isolated and does not require to be in a closed transaction.