I have table that holds the connection and disconnection times of the clients.
ID int
ClientID int
BeginDate datetime
EndDate datetime
When a client is connected, a Session
record is created with its EndDate
is null.
INSERT Session(ClientID, BeginDate, EndDate)
VALUES(@ClientID, GETDATE(), null)
When the client is disconnected, I update the session so it's EndDate
contains the disconnection time.
UPDATE Session
SET EndDate = GETDATE()
WHERE Id = @SessionID
But this doesn't always get called.
So, to make sure the same client doesn't have more than one sessions at the same time, I run this query before inserting a new session.
UPDATE Session
SET EndDate = GETDATE()
WHERE ClientID = @ClientID
and EndDate is null
Which causes a deadlock when more than one clients connect at the same time, according to the deadlock graph.
I don't understand why this causes a dead-lock,
I don't even understand why that query needs a lock.
What am I doing wrong?