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
You could do that in TSQL via save points (
SAVE TRANon SQL Server), but frankly I don’t recommend it. You can’t do it viaTransactionScope, 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.