Question

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?

Was it helpful?

Solution

Updating EndDate every time before inserting one does not seems to be a good idea. You might lock table even when there is no update done. You should check if there any session where endate is null for a give client before the update. You can use NoLock hint to read un-commited data.

Doing this check will minimise the lock caused by this query. Prevention is better then cure:)

OTHER TIPS

well an update-statement makes X-locks, for this statement maybe even a page- or a table-lock, you can try selecting the id's of the open sessions and executing an update for these with an in-clause after that, which is more likely to lock only those rows. it depends on your isolation level as well.

way can't there be more than one session open, what if (and i'm guessing it is a web-application) a user opens the site with 2 different web-browsers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top