Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Deadlock issue


Deadlock issue

Author
Message
ioani
ioani
SSC Veteran
SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)

Group: General Forum Members
Points: 295 Visits: 414
I have 2 tables:


CREATE TABLE [Prescription].[Weight]
(
[SessionID] INT NOT NULL,
[TargetWeight] REAL NULL,
[Intake] REAL NULL,
[WeightLoss] REAL NULL,
....................................
);
ALTER TABLE [Prescription].[Weight]
ADD CONSTRAINT [PK_Weight] PRIMARY KEY CLUSTERED ([SessionID] ASC);

CREATE TABLE [SessionData].[Weight]
(
[SessionID] INT NOT NULL,
[Scale] REAL NULL,
[WeightLoss] REAL NULL,
......................................
);
ALTER TABLE [SessionData].[Weight]
ADD CONSTRAINT [PK_Weight] PRIMARY KEY CLUSTERED ([SessionID] ASC);



and 2 stored procedures one that save data in that 2 tables and one that get data from these tables:


CREATE PROCEDURE [SessionData].[spGetWeight]
@SessionID INT
AS
BEGIN
SELECT WO.SessionID,
WO.Scale,
WO.WeightLoss,
WP.TargetWeight
FROM [SessionData].[Weight] AS WO LEFT OUTER JOIN [Prescription].[Weight] WP ON(WO.SessionID = WP.SessionID)
WHERE WO.SessionID = @SessionID;
END


CREATE PROCEDURE [Prescription].[spSaveWeight]
@SessionID AS INT,
@TargetWeight AS REAL = NULL,
@Intake AS REAL = NULL,
@WeightLoss AS REAL = NULL
AS
BEGIN
BEGIN TRY
BEGIN TRANSACTION;

IF NOT EXISTS (SELECT 1 FROM [Prescription].[Weight] WHERE SessionID = @SessionID)
BEGIN
INSERT INTO [Prescription].[Weight]
(SessionID,
TargetWeight,
Intake,
WeightLoss
)
VALUES (@SessionID,
@TargetWeight,
@Intake,
@WeightLoss
);
END
ELSE
BEGIN
UPDATE [Prescription].[Weight]
SET TargetWeight = @TargetWeight,
Intake = @Intake,
PreTara = @WeightLoss
WHERE SessionID = @SessionID;
END

IF NOT EXISTS (SELECT 1 FROM [SessionData].[Weight] WHERE SessionID = @SessionID)
BEGIN
INSERT INTO [SessionData].[Weight] (SessionID, WeightLoss )
VALUES (@SessionID, @WeightLoss );
END
ELSE
BEGIN
UPDATE [SessionData].[Weight]
SET WeightLoss= @WeightLoss
WHERE SessionID = @SessionID;
END
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
-- log error
END CATCH
END



I get an deadlock error between the select from first procedure and the update of the [SessionData].[Weight] from the second procedure.
See the attachement.

How can I solve this problem?

Thanks,
ioani
Attachments
Weight.PNG (6 views, 35.00 KB)
sqlGDBA
sqlGDBA
SSC Journeyman
SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)

Group: General Forum Members
Points: 91 Visits: 288
Your code seems to be incomplete. I can see a begin try ... but no end to the try block, and no catch block either. A simple fix would be to look for error 1204(and 1222 if you want) and retry again from the calling proc/program. Another fix would be to do your selects with read uncommitted, if that is a possibility.
DiverKas
DiverKas
SSC Veteran
SSC Veteran (253 reputation)SSC Veteran (253 reputation)SSC Veteran (253 reputation)SSC Veteran (253 reputation)SSC Veteran (253 reputation)SSC Veteran (253 reputation)SSC Veteran (253 reputation)SSC Veteran (253 reputation)

Group: General Forum Members
Points: 253 Visits: 460
The problem I see, is there is no ROLLBACK should an error exist. Therefore you have uncommitted changes pending, which of course, will cause the GET procedure to deadlock. Thats a guess, as I have no way to test my theory where I am at currently.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search