views:

221

answers:

4

I need to implement nested transactions in .NET using ADO.NET.

The situation is as follows:

--> Start Process (Begin Transaction)       

   --> Do DB things       

   --> Begin Transaction for step 1
   --> Step 1
   --> Commit transaction for step 1

   --> Begin transaction for step 2
   --> Step 2
   --> Rollback transaction for step 2

   --> etc ...

   --> Do DB things       


--> End Process(Commit or Rollback ALL commited steps --> a.k.a the process)

Can that be done with transaction scopes? Could anyone post an example?

In addition I'd need the process to work for SQL Server 2005 AND Oracle 10g databases... will transaction scopes work with both database engines?

Edit: Note this situation might happen:

Step1 is commited, Step2 is rolled back Step3 is commited.

The process is commited

(Step1 and Step3 do store data into the database, step2 does not)

On the other hand...

Step1 is commited, Step2 is rolled back Step3 is commited.

The process is rolled back.

the NO DATA IS COMMITED to the database

Note: No DB schema or domain values available

+2  A: 

This article talks about savepoints and nested transactions.

http://msdn.microsoft.com/en-us/library/ms971557.aspx

Aseem Gautam
Thanks! The Jet provider is the only Microsoft OLE DB provider that supports nested transactions. :-(
manza_jurjur
A: 

If you are want ALL steps to be committed or NONE then wouldn't one transaction be more appropriate? You can pass an existing transaction object to the constructor of an ADO.Net Command object and thus perform multiple updates within the scope of a single transaction.

Simon
True, but this is a different question, thanks
manza_jurjur
+3  A: 

You could do that in TSQL via save points (SAVE TRAN on SQL Server), but frankly I don't recommend it. You can't do it via TransactionScope, as any abort is terminal (the entire transaction is rolled back as soon as any transaction in the tree indicates failure).

Personally: check the data first, and only perform valid actions. If it fails, that is terminal - roll it back. Possibly separate the work into atomic units that can be truly committed (or rolled back) in isolation.

Marc Gravell
The possible errors are due to foreign key being violated not due to data values that can be previously checked, whether step2 fails or not, step3 can still be commited. Thanks about TSQL but I need a solution over ADO.NET (I have to work with Oracle aswell)
manza_jurjur
@manza_jurjur - sorry, but I don't accept that foreign keys can't be validated in advance. And while SqlTransaction has a `Save()` method, AFAIK that is not the case for Oracle. You can't necessarily have what doesn't exist.
Marc Gravell
Sure, but in this case I don't know the DB schema and domain values
manza_jurjur
+2  A: 

On Oracle:

BEGIN    
  SAVEPOINT STEP1;
  -- do some things
  IF your_criteria_for_commit_is_needed THEN
    NULL; -- do nothing
  ELSE
    ROLLBACK TO SAVEPOINT STEP1;
  END IF;

  SAVEPOINT STEP2;
  -- do some other things
  IF your_other_criteria_for_commit_is_needed THEN
    NULL; -- do nothing
  ELSE
    ROLLBACK TO SAVEPOINT STEP2;
  END IF;

  -- SOME NUMBER OF OTHER STEPS

  IF your_criteria_for_all_step_commit_is_needed THEN
    COMMIT; -- commit all changes to DB
  ELSE
    ROLLBACK; -- rollback all changes
  END IF;
END;
/
jva
I think the whole idea of savepoints within a transaction dubious: when part of the transaction fails then the whole transaction ought to be rolled back. If it doesn't matter whether Step2 succeeds or fails why bother with Step2 at all? However, if your Unit Of Work is specified in such a wack fashion this is definitely the way to implement it.
APC
Great for PL-SQL but Could I do this over ADO.NET?
manza_jurjur