I have an API via which I am performing series of insert and then delete in table1. But sometimes when I am sending too many request it is ending up in Deadlock.
the table1 has nonclustered unique index on column : cid [varchar(24)], sum_value [varchar(48)] and entry_time_string[varchar(50)]
Following is my jdbc url :
jdbc:sqlserver://localhost:1433;database=srik_2024;integratedSecurity=false;applicationName=srik;sendStringParametersAsUnicode=false;encrypt=true;trustServerCertificate=true;responseBuffering=adaptive;selectMethod=direct
Following are the set of insert stmt created via java code during execution under single transaction executed one after other:
insert into table1(table1_id, cid, sum_value, entry_time, entry_time_string, buffer_entry_flag, user)
values('m240527090938173704245f7', 'm22112317542685511944168', '2582013865', '2024-05-27 09:02:13.991', '2024-05-27 09:02:1', 0, 'SRIK')
insert into table1(table1_id, cid, sum_value, entry_time, entry_time_string, buffer_entry_flag, user)
values('m240527090938175704255f7', 'm22112317542685511944168', '2582013865', '2024-05-27 09:02:13.991', '2024-05-27 09:02:0', 1, 'SRIK')
insert into table1(table1_id, cid, sum_value, entry_time, entry_time_string, buffer_entry_flag, user)
values('m240527090938175704265f7', 'm22112317542685511944168', '2582013865', '2024-05-27 09:02:13.991', '2024-05-27 09:02:2', 1, 'SRIK')
delete from table1
where table1_id in ('m240527090938175704255f7', 'm240527090938175704265f7')
But when we make multiple request at same time then all request would generate query as shown in above format with different sum_value but mostly same cid and entry_time_string.
one option I could think is do retry to committransaction under catch block by if there is any better way I can achieve this .
Note: I am doing this multiple insert in order to prevent duplicate request triggered during same interval of time of 10 sec
java code :
I have tried catching the exception which occurs due to deadlock based on sql error code and do an retry for an attempt of 5 but wanted to check if I can do anything better than that