How Do I...Use explicit transactions in my application?
If you want to explicitly create a transaction, use the CommittableTransaction class instead of the TransactionScope class.
There are important differences between CommittableTransaction objects and TransactionScope objects:
- Creating a CommittableTransaction does not set the current transaction. This means a transacted resource you might perform an action on will not automatically be part of the current transaction. You will need to manually pass the transaction you created to the transacted resource.
- When using CommittableTransactions, you must call commit or rollback.
- Exceptions do not automatically rollback transactions. Rollback must be called manually if a failure occurred when an exception is thrown.
1. In it's simplest form, create a new CommittableTransaction
CommittableTransaction tx = new CommittableTransaction();
C#
2. To commit the transaction, call the commit method
C#
3. Or to rollback the transaction, call the rollback method
C#
Here is a full example:
[This sample can be found at M:\web\users\Sites\AspnetQuickStart\v2.0\QuickStart\howto\samples\Transactions\CommittableTx
To build this sample, open the SDK command prompt and navigate to the above path. Build the sample using the build tool msbuild
passing the solution file as the first parameter: msbuild mySample.sln. The compiled executable will be found in the sub directory \bin
directory.]
Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2005 Microsoft Corporation. All rights reserved.
|