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


Is a doomed transaction inevitable if deadlock occurs in a trigger (SQL Server 2008 R2)?


Is a doomed transaction inevitable if deadlock occurs in a trigger (SQL Server 2008 R2)?

Author
Message
davidsatz
davidsatz
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 181
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
Cadavre
Cadavre
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2598 Visits: 8437
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.


Forever 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


Craig Wilkinson - Software Engineer
LinkedIn
davidsatz
davidsatz
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 181
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
ScottPletcher
ScottPletcher
Hall of Fame
Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)

Group: General Forum Members
Points: 3956 Visits: 6692
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)

Prosecutor James Blackburn, in closing argument in the "Fatal Vision" murders trial: "If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them."
davidsatz
davidsatz
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 181
So I am not hearing anyone disagree with my conclusion that there is no way to accomplish this...Ermm
Cadavre
Cadavre
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2598 Visits: 8437
davidsatz (2/28/2013)
So I am not hearing anyone disagree with my conclusion that there is no way to accomplish this...Ermm


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.


Forever 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


Craig Wilkinson - Software Engineer
LinkedIn
Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3046 Visits: 5478
davidsatz (2/28/2013)
So I am not hearing anyone disagree with my conclusion that there is no way to accomplish this...Ermm


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!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
davidsatz
davidsatz
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 181
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
ScottPletcher
ScottPletcher
Hall of Fame
Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)

Group: General Forum Members
Points: 3956 Visits: 6692
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...Ermm


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)

Prosecutor James Blackburn, in closing argument in the "Fatal Vision" murders trial: "If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them."
Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3046 Visits: 5478
...
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!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
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