views:

618

answers:

7

This is a significant edit from the original question, making it more concise and covering the points raised by existing answers...

Is it possible to have mulitple changes made to multiple tables, inside a single transaction, and rollback only some of the changes?

In the TSQL below, I would NOT want any of the changes made by "myLogSP" to ever be rolled back. But all changes made by the various myBusinessSPs should rollback if necessary.

BEGIN TRANSACTION  

    EXEC myLogSP

    EXEC @err = myBusinessSPa
    IF (@err <> 0) BEGIN ROLLBACK TRANSACTION RETURN -1 END

    EXEC myLogSP

    EXEC @err = myBusinessSPb
    IF (@err <> 0) BEGIN ROLLBACK TRANSACTION RETURN -1 END

    EXEC myLogSP

    EXEC @err = myBusinessSPc
    IF (@err <> 0) BEGIN ROLLBACK TRANSACTION RETURN -1 END

    EXEC myLogSP

COMMIT TRANSACTION
RETURN 0

The order is important, the myLogSPs must happen between and after the myBusinessSPs (the myLogSPs pick up on the changes made by the myBusinessSPs)

It is also important that all the myBusinessSPs happen inside one transaction to maintain database integrity, and allow all their changes to rollback if necessary.

It's as if I want the myLogSPs to behave as if they're not part of the transaction. It is just an inconvenient fact that they happen to be inside one (by virtue of needing to be called between the myBusinessSPs.)

EDIT:

Final answer is "no", the only option is to redesign the code. Either to using table variables for the logging (as variables don't get rolled back) or redesign the business logic to Not require Transactions...

A: 

Wouldn't the easy way be to move the log insertion outside the transaction?

I don't really have an answer for you for the table lock, I think you already have the answer, there will have to be a table lock because the identity column may roll back.

Moose
+5  A: 

Use SAVEPOINTs, e.g.

BEGIN TRANSACTION  

    EXEC myLogSP

    SAVE TRANSACTION savepointA
    EXEC @err = myBusinessSPa
    IF (@err <> 0) BEGIN
        ROLLBACK TRANSACTION savepointA
        COMMIT
        RETURN -1
    END

    EXEC myLogSP

    SAVE TRANSACTION savepointB
    EXEC @err = myBusinessSPb
    IF (@err <> 0) BEGIN
        ROLLBACK TRANSACTION savepointB
        COMMIT
        RETURN -1
    END

    EXEC myLogSP

    SAVE TRANSACTION savepointC
    EXEC @err = myBusinessSPc
    IF (@err <> 0) BEGIN
        ROLLBACK TRANSACTION savepointC
        COMMIT
        RETURN -1
    END

    EXEC myLogSP

COMMIT TRANSACTION


EDIT

Based on the information provided so far (and my understanding of it) it appears that you will have to re-engineer you logging SPs, either to use variables, or to use files, or to allow them to run 'after the fact' as follows:

BEGIN TRANSACTION  

    SAVE TRANSACTION savepointA
    EXEC @err = myBusinessSPa
    IF (@err <> 0) BEGIN
        ROLLBACK TRANSACTION savepointA
        EXEC myLogSPA -- the call to myBusinessSPa was attempted/failed
        COMMIT
        RETURN -1
    END

    SAVE TRANSACTION savepointB
    EXEC @err = myBusinessSPb
    IF (@err <> 0) BEGIN
        ROLLBACK TRANSACTION savepointB
        EXEC myLogSPA -- the call to myBusinessSPa originally succeeded
        EXEC myLogSPB -- the call to myBusinessSPb was attempted/failed
        COMMIT
        RETURN -1
    END

    SAVE TRANSACTION savepointC
    EXEC @err = myBusinessSPc
    IF (@err <> 0) BEGIN
        ROLLBACK TRANSACTION savepointC
        EXEC myLogSPA -- the call to myBusinessSPa originally succeeded
        EXEC myLogSPB -- the call to myBusinessSPb originally succeeded
        EXEC myLogSPC -- the call to myBusinessSPc was attempted/failed
        COMMIT
        RETURN -1
    END

    EXEC myLogSPA -- the call to myBusinessSPa succeeded
    EXEC myLogSPB -- the call to myBusinessSPb succeeded
    EXEC myLogSPC -- the call to myBusinessSPc succeeded

COMMIT TRANSACTION
vladr
I think you should remove the else statement, otherwise the first insert would never be committed in case of a rollback.
Jimmy Stenke
In my experience ROLLBACK TRANSACTION will rollback all open transactions, not just the current level of the nested transactions...
Dems
No, not those cases where you uses named transactions or save points, then it will roll back to that transaction or save point that you've named.
Jimmy Stenke
I have made an edit which explains better what I need. Thanks for the info on named transaction and save points, I'm sure I'll be using that soon. But I don't think this solves it for me...
Dems
that question is getting stupidly long, should I delete the question and post a new one with more (better) detail? leave it as is? or edit the question down to what I would otherwise post ina new question? (I don't know the etiquette *grin*)
Dems
Much clearer. :) Any better now? :)
vladr
That was my first thought when I saw SAVE TRANSACTION for the first time (thanks again, I didn't know about it). However (sorry) If I rollback after SavePointC I want the effects of myBusinessSPa and myBusinessSPb to also rollback. Without ANY of the myLog effects rolling back *cringe*
Dems
+1 for entertaining my inept drivels and being of most help so far :)
Dems
@Dems, how exactly do myLogSP and myBusinessSPXXX interact? Do you have control over what myLogSP is doing (i.e. get it to write its data into an array instead of going directly to table, and you flush the entire array to table at the very end after rollback savepoint but before commit?
vladr
...essentially HLGEM's avenue of investigation.
vladr
This is a generic question and is deliberately generalised. myBusinesSP can have any effect (insert, update, delete) on multiple tables. The myLogSPs update different tables based on the effects of the myBusinessSPs. Because this logging can be complex it is encapsulated into SPs modifying tables
Dems
Unfortunately your only way out appears to be reengineering the logging SPs and/or the logging mechanism (how the logging methods obtain their data.) You cannot break up your transaction as selectively as you wish, and inner transactions are no help either
vladr
(I quote, "Committing inner transactions is ignored by the SQL Server Database Engine")
vladr
This is what I thought, but hoped there was something out there I did not know. |A simple re-engineering won't be possible, the logging needs to know the state between the business SPs, information lost by the end of the transaction. Thanks for looking at it though...
Dems
The only re-engineering I can see is to engineer the business SPs to not require transactions. Difficult and not completely clean, but possible and less untidy that the alternatives...
Dems
A: 

move the BEGIN TRANSACTION statement to after the first insert.

MasterMax1313
+1  A: 

Use SAVEPOINTS and TRANSACTION ISOLATION LEVELS.

rick schott
in my case I don't think save points help. Can you elaborate on how I would use isolation levels to solve this?
Dems
"Also, is there a way to lock only the rows being inserted into the tables, rather than the whole table? (The tables have identity columns which is preventing this)"The Isolation levels can help with reading the data during the transaction:SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
rick schott
Also, I had the wrong link on my original comment for TRANSACTION ISOLATION LEVELS.
rick schott
+1  A: 

We have had luck with putting the log entries into table variables and then inserting to the real tables after the commit or rollback.

OK if you aren't on SQL Server 2008, then try this method. It's messy and a workaround but it should work. The #temp table and the table variable would have to be set up with the structure of what is returned by the sp.

create table #templog (fie1d1 int, field2 varchar(10))

declare @templog table (fie1d1 int, field2 varchar(10))

BEGIN TRANSACTION      
insert into #templog
Exec my_proc

insert into @templog (fie1d1, field2)
select t.* from #templog t 
left join @templog t2 on t.fie1d1 = t2.fie1d1 where t2.fie1d1 is null

insert into templog
values (1, 'test')

rollback tran
select * from #templog
select * from templog
select * from @templog
HLGEM
Heh... I saw that a new answer came in just as I was pasting my answer in and I thought, "I'll bet someone else just pointed out table variables". +1 :)
Tom H.
This is the only option I can see right now too. But it has implications. The inserts in to the logging tables are done by a SP, which maintains timestamps and other metadata. Copying that code into multiple places in multiple SPs is not 'ideal' :(
Dems
YOu can insert into a table variable from an sp in newer version of SQL Server. SQL Server 2000 you have to use temp tables instead. I haven't tried that, so I don't know if they stay in scope when the rollback occurs, but you could two step if need be, sc to temp table temp table to table varaible.
HLGEM
Asfar as I am aware, SQL 2008 allows table variables to be passed to SPs, and table variables are out of scope as far as TRANSACTIONS go. But I do not believe there is a way for a SQL 2005 SP to update a table variable from declare outside of the SPs scope...
Dems
DEMS see my edit for a workaround
HLGEM
unfortunately, due to creation of primary keys and foreign key relationships this is not quite as straight forward. I agree it is technically do-able, but it is very messy and this impacts on the clients ability to maintain it. Thus the desire to commit specific changes.
Dems
+1  A: 

You need to basically jump outside of the current context. There are a couple of ways to do that. One (which I have never tried) is to call the CLR to do the insert.

Perhaps a better way though is using the fact that table variables are not affected by transaction. For example:

CREATE TABLE dbo.Test_Transactions
(
     my_string VARCHAR(20) NOT NULL
)
GO

DECLARE
     @tbl TABLE (my_string VARCHAR(20) NOT NULL)

BEGIN TRANSACTION

INSERT INTO dbo.Test_Transactions (my_string) VALUES ('test point one')

INSERT INTO @tbl (my_string) VALUES ('test point two')

INSERT INTO dbo.Test_Transactions (my_string) VALUES ('test point three')

ROLLBACK TRANSACTION

INSERT INTO dbo.Test_Transactions (my_string) select my_string from @tbl

SELECT * FROM dbo.Test_Transactions
SELECT * FROM @tbl
GO
Tom H.
This is the only option I can see right now too. But it has implications. The inserts in to the logging tables are done by a SP, which maintains timestamps and other metadata. Copying that code into multiple places in multiple SPs is not 'ideal' :(
Dems
A: 

Perhaps you could put the inserts/updates to the business tables in their own atomic transaction t1 and wrap each of these transactions in another transaction t2 that executes the log table update and t1 (the business table updates) without any rollbacks. For example:

BEGIN TRANSACTION t2
     <insert to log>
     <execute stored procedure p1>
END TRANSACTION t2

CREATE PROCEDURE p1
AS
     BEGIN TRANSACTION t1
         <insert to business tables>
         <rollback t1 on error>
     END TRANSACTION t1

I believe that when you rollback t1 in the stored procedure this will leave the calling transaction t2 unaffected.

Daniel
I can't move the log insert out side of the transaction. Some log inserts happen between/after different business logic changes, and that order can not be changed...
Dems
Hmmm... well couldn't you make each business table change atomic so that it only performs one insert/update and then wrap that in another transaction as I suggested above?
Daniel
Each businessSP depends upon the previous one, and all must happen before the transaction can close. In short, the order can not be changed, and none of them can be taken out of the transaction :(
Dems