Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Is a doomed transaction inevitable if deadlock occurs in a trigger (SQL Server 2008 R2)? Expand / Collapse
Author
Message
Posted Wednesday, February 27, 2013 9:37 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, November 17, 2014 7:03 AM
Points: 10, Visits: 159
Hi all,

Below are two scenarios, however, what I am trying to accomplish is to catch deadlock errors and re-try the DML that was chosen as the deadlock victim. Does anyone know of way to accomplish what I am trying to accomplish without a doomed transaction given the constraints? I have seen many posts that this appears to be a limitation of the dbengine, but am looking for confirmation.

In both scenarios, I have a SProc - let's called it "X" - that is called mostly from within triggers (that performs some denormalized calculations and cannot be changed at this time) - in which deadlocks are often occurring.

Scenario 1
•In SProc X, I want to catch any 1205 errors, and re-try the query that receives the 1205 error
•However, whenever I re-try the query I receive an error that the transaction is "The current transaction cannot be committed and cannot support operations that write to the log file. Roll back the transaction"

Scenario 2
•In SProc Y, that runs DML statements that will fire triggers that call SProc X, I want to catch any 1205 errors, and re-try the query that receives the 1205 error
•If SProc Y is called outside any transaction, I can begin a new transaction and rollback if a deadlock occurred and successfully retry the DML
•However, if SProc Y is called inside a transaction, I need to perform a SAVE TRAN and rolback to the savepoint. Whenever I re-try the query I receive an error that the transaction is "The current transaction cannot be committed and cannot be rolled back to a savepoint. Roll back the entire transaction.". For my logic to work, I cannot rollback the entire transaction, so SProc Y only performs as expected outside a transaction

TIA,

Dave
Post #1424611
Posted Wednesday, February 27, 2013 11:08 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, November 21, 2014 6:35 AM
Points: 2,386, Visits: 7,610
Read up on XACT_ABORT. XACT_ABORT is by default set to OFF in most instances for code in SQL Server, but inside a trigger it is set to ON by default.


Not a DBA, just trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1424658
Posted Wednesday, February 27, 2013 12:46 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, November 17, 2014 7:03 AM
Points: 10, Visits: 159
Cadavre (2/27/2013)
Read up on XACT_ABORT. XACT_ABORT is by default set to OFF in most instances for code in SQL Server, but inside a trigger it is set to ON by default.
thanks Cadavre however I have tried setting SET XACT_ABORT OFF in the trigger as well as in the SProc
Post #1424704
Posted Wednesday, February 27, 2013 1:35 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 10:06 AM
Points: 2,268, Visits: 3,425
If XACT_STATE() returns -1 [uncommitable transaction], you can't do anything else except rollback the current transaction. That is a logical restriction within SQL Server itself.

In that case, you would have to re-issue the earlier statement(s) in the transaction to get them to apply again after the rollback.


SQL DBA,SQL Server MVP('07, '08, '09)

Carl Sagan said: "There is no such thing as a dumb question." Sagan obviously never watched a congressional hearing!
Post #1424730
Posted Thursday, February 28, 2013 7:28 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, November 17, 2014 7:03 AM
Points: 10, Visits: 159
So I am not hearing anyone disagree with my conclusion that there is no way to accomplish this...
Post #1425040
Posted Thursday, February 28, 2013 8:00 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, November 21, 2014 6:35 AM
Points: 2,386, Visits: 7,610
davidsatz (2/28/2013)
So I am not hearing anyone disagree with my conclusion that there is no way to accomplish this...


I do disagree, we have deadlock retry logic on our product. The logic works by passing the error number out from the trigger and into the sproc, then rolling back to the savepoint if XACT_STATE() <> -1 otherwise the transaction is doomed so rollback the entire transaction.



Not a DBA, just trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1425052
Posted Thursday, February 28, 2013 8:08 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 8:23 AM
Points: 2,873, Visits: 5,189
davidsatz (2/28/2013)
So I am not hearing anyone disagree with my conclusion that there is no way to accomplish this...


I'm not exactly sure what you are looking to do, it would be helpful if you would provide some DDL to have a look (follow the link at the bottom to my signature - it explains what kind of details are expected from OP).
Anyway... If you are asking about possibility to log error which causes rallback from within a trigger, the answer is - it is possible. Here is small demonstration:

create table _test (id int, hundreddevidebyid int)
go
create table _test_log (logid int identity(1,1), error varchar(2000))
go
create trigger tr_test on _test for insert
as
begin

begin try
update t
set hundreddevidebyid = 100/i.id
from _test t
join inserted i on i.id = t.id
end try
begin catch
declare @err varchar(2000) = error_message();
rollback;
insert _test_log select @err;
end catch

end

insert _test (id) select 50;
insert _test (id) select 25;
insert _test (id) select 0;
select * from _test;
select * from _test_log;




You will see, that the third INSERT fails as it will cause division by 0, but the error is logged into the table.
Now, if you amend the trigger and comment out the ROLLBACK , the logging will stop working.
So the rule is: if you want to log errors within a trigger then your trigger should have error-handling with explicit ROLLBACK...


_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1425060
Posted Thursday, February 28, 2013 2:42 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, November 17, 2014 7:03 AM
Points: 10, Visits: 159
thanks you for all the help - today is crazy day at work - tomorrow I will try to post some portion of the SQL involved to help clarify
Post #1425288
Posted Thursday, February 28, 2013 2:57 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 10:06 AM
Points: 2,268, Visits: 3,425
Eugene Elutin (2/28/2013)
davidsatz (2/28/2013)
So I am not hearing anyone disagree with my conclusion that there is no way to accomplish this...


I'm not exactly sure what you are looking to do, it would be helpful if you would provide some DDL to have a look (follow the link at the bottom to my signature - it explains what kind of details are expected from OP).
Anyway... If you are asking about possibility to log error which causes rallback from within a trigger, the answer is - it is possible. Here is small demonstration:

create table _test (id int, hundreddevidebyid int)
go
create table _test_log (logid int identity(1,1), error varchar(2000))
go
create trigger tr_test on _test for insert
as
begin

begin try
update t
set hundreddevidebyid = 100/i.id
from _test t
join inserted i on i.id = t.id
end try
begin catch
declare @err varchar(2000) = error_message();
rollback;
insert _test_log select @err;
end catch

end

insert _test (id) select 50;
insert _test (id) select 25;
insert _test (id) select 0;
select * from _test;
select * from _test_log;




You will see, that the third INSERT fails as it will cause division by 0, but the error is logged into the table.
Now, if you amend the trigger and comment out the ROLLBACK , the logging will stop working.
So the rule is: if you want to log errors within a trigger then your trigger should have error-handling with explicit ROLLBACK...



He wants his original SQL statement to complete, but I'm not sure that's possible once you've encountered a constraint error. A relational transaction is, by definition, "all or none": so SQL can't just apply the "good" rows and ignore the "bad" ones.


SQL DBA,SQL Server MVP('07, '08, '09)

Carl Sagan said: "There is no such thing as a dumb question." Sagan obviously never watched a congressional hearing!
Post #1425293
Posted Friday, March 1, 2013 3:48 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 8:23 AM
Points: 2,873, Visits: 5,189
...
He wants his original SQL statement to complete, but I'm not sure that's possible once you've encountered a constraint error. A relational transaction is, by definition, "all or none": so SQL can't just apply the "good" rows and ignore the "bad" ones.


If so, then it's impossible. You are absolutely right, SQL cannot commit "good part" of a query and roll-back only "bad one". It's call transaction atomicity rule - all or nothing!


_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1425423
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse