Transaction (Process ID) was deadlocked errors


Transaction (Process ID) was deadlocked errors



I am having an issue with my code and the numerous SQL calls that it makes with deadlocks. I pasted the code into PasteBin here: https://pastebin.com/p1YDkKsB. Can someone help me out here? It happens most often in the CheckClanActivity task here:


using(SqlCommand cmd = new SqlCommand(string.Format("select * from ClanMembers where MembershipId={0} and IsActive = 1", entry.Player.DestinyUserInfo.MembershipId), conn))



, but it also happens all over the place.



Edit:
Okay, the involved SQL statements are as follows:


if not exists(select * from ClanMembers where MembershipId={0}) begin insert into ClanMembers(ID, MembershipId, BattleNetId, ClanId, DateLastPlayed, IsActive, LastUpdated) select ISNULL(MAX(ID) + 1, 0),{0},'{1}',{2},'{3}', 1, GETDATE() from ClanMembers end else begin update ClanMembers set DateLastPlayed='{3}', LastUpdated=GETDATE() where MembershipId={0} end

if not exists(select * from ClanMemberCharacters where CharacterId={1}) begin insert into ClanMemberCharacters(ID,MembershipId,CharacterId) select ISNULL(MAX(ID)+1,0),{0},{1} from ClanMemberCharacters end

select c.* from ClanMemberCharacters c join ClanMembers m on m.MembershipId = c.MembershipId where m.IsActive = 1 ORDER BY m.MembershipId desc

select * from ActivityHistory where InstanceId = {0}

if not exists(select * from ActivityHistory where InstanceId = {0}) begin insert into ActivityHistory(InstanceId,MembershipId,CharacterId,GameMode,ActivityDate,ReferenceId,DirectorActivityHash,IsPrivate,ClanActivity,ClanActivityCount) values( {0},{1},{2},'{3}','{4}',{5},{6},{7},{8},{9} ) END

select * from ClanMembers where IsActive = 1

Select * from ClanMembers where IsActive = 0

update ClanMembers set IsActive = 1 where MembershipId = {0}

select * from ClanMembers where MembershipId={0} and IsActive = 1





Yeah. Deadlock graphs? ALL involved statements, execution plans, table structures please.
– TomTom
Jun 30 at 18:31





I updated the question with all the SQL statements being used, but I dont have any deadlock graphs to share as the host I'm using doesn't allow this access. As far as the execution plans are concerned, the order the statements are in is the order in which they execute.
– mikew
Jun 30 at 18:44





Well, then grab a book and start a tace and get the graphs beacuse they tell you what is deadlocking. SImple as that. Btw., awfull code to use ISNULL to get a new ID instead of using the sql server integrated identity columns, or sequences. Back to documentation.
– TomTom
Jun 30 at 19:10




1 Answer
1



I suspect the problem is the nesting of SqlConnections with the same connection string one within the other. Suggestions:


SqlConnection


using


if (conn.State == ConnectionState.Closed) { conn.Open(); }


conn.Open();


SqlConnection


conn.Close()


using


SqlDataAdapter


using


throw;


ex.ToString()


ex.Message


string.Format





Thanks for this response. I'm pretty sure these suggestions have solved my issue, but there's a lot of data this app goes through, so I'll find out in a few hours.
– mikew
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API