Updating 2 tables in same order by 2 different process yield deadlock
0
votes
0
answers
91
views
For some reason, we have multiple processes that get a request to update some tables (legacy code).
Let's say we have 2 process that simultaneously updating MyTable1 and MyTable2 in a transaction.
UPDATE MyTable1 SET ... WHERE Id=@p
UPDATE MyTable2 SET ... WHERE Id=@p
We have PK in Id column. Both processes are updating the above tables in same order as above. I am just trying to understand why we have deadlock in this case, as if one process update MyTable1 then row get locked and second process will be in waiting state, no?
Here is deadlock log,
unknown
unknown
(@p1 int,@p0 int)SET NOCOUNT ON;
UPDATE [MyTable1] SET [MyColumn1] = @p0
WHERE [Id] = @p1;
SELECT @@ROWCOUNT;
unknown
unknown
(@p1 int,@p0 int)SET NOCOUNT ON;
UPDATE [MyTable2] SET [MyColumn1] = @p0
WHERE [Id] = @p1;
SELECT @@ROWCOUNT;
unknown
unknown
(@p1 int,@p0 int)SET NOCOUNT ON;
UPDATE [MyTable2] SET [MyColumn1] = @p0
WHERE [Id] = @p1;
SELECT @@ROWCOUNT;
unknown
unknown
(@p1 int,@p0 int)SET NOCOUNT ON;
UPDATE [MyTable1] SET [MyColumn1] = @p0
WHERE [Id] = @p1;
SELECT @@ROWCOUNT;
My Code is simple as that,
public class MyConsumer : IConsumer
{
private readonly MyService _myService;
public AddUpdateHubLinkConsumer(MyService myService)
{
_myService= myService;
}
public async Task Consume(ConsumeContext context)
{
await _myService.SaveMyChanges(context.Message);
}
}
public class MyService
{
public async Task SaveMyChanges(MyModel model)
{
// My work there
await myDbContext.SaveMyChanges();
}
}
Asked by Imran Qadir Baksh - Baloch
(1319 rep)
Jul 5, 2023, 12:28 PM
Last activity: Jul 8, 2023, 08:05 AM
Last activity: Jul 8, 2023, 08:05 AM