DEV 305 Introducing System.Transactions Sarvashrestha Paliwal Tarun Anand ISV Technical Evangelist Microsoft Most Valuable Professional
Session Overview Transactions in .NET 1.1 Introducing new features in .NET 2.0 Understand the role of the Lightweight Transaction Manager (LTM) Transaction Manager Promotion Transactions Objects TransactionScope Transaction Flow Developing a Resource Manager
Why System.Transactions Transaction programming models in .NET 1.1 Explicit transaction management Declarative transaction management Both models have disadvantages Neither superior to the other in every respect .NET 2.0 unifies benefits of both models Minimizing overhead Minimizing hand crafted code Separate from hosting environment and instance management
Transactions in .NET 1.1
Transactions in .NET 1.1 Pros Cons Transaction Client Obj DB Straightforward Cons Most-suitable for a single object interacting with a single database Transaction Client Obj DB
Transactions in .NET 1.1 Gets even more complex with multiple objects and multiple resources Transaction Client Obj Obj Obj DB DB DB
Transactions in .NET 1.1 Relatively clean code but … Must derive from ServicedComponent Must be in strong named assembly Must register in COM+ Very different from single DB scenario
Transactions in .NET 1.1 What happens if the DB update fails? The hashtable value is lost!
.NET 2.0 Transaction One uniform programming model Will use local transaction when Transaction inside a single app domain Transaction involves at most a single durable RM Uses only intra-app domain calls
.NET 2.0 Transaction Will use distributed transaction when Remote calls Multiple durable resources Uses the old DTC with minor changes
Transaction Manager Promotion Every System.Transactions transaction starts as local transaction Single object interacts with single durable resource Only requires local transaction Yields best throughput and performance Transaction promoted when Transaction flows to another object in another app domain Enlisting another durable RM
Transactions Objects Transaction represents transaction for all transaction managers Disposable Transaction used to abort [Serializable] public class Transaction : IDisposable,ISerializable { public void Rollback(); //Abort the transaction public void Dispose(); //Other members }
Transactions Objects Additional functionality of Transaction Enlist RM Set isolation level Obtain transaction information Clone transaction Completion event
Transactions Objects CommittableTransaction used to commit transaction Additional functionality Asynchronous commit [Serializable] public sealed class CommittableTransaction : Transaction,IAsyncResult { public void Commit(); //Other members }
Declarative Model in .NET 2.0 Requires ES out of the box ADO.NET is aware of System.Transactions ES code is same as in .NET 1.1 Existing components benefit automatically Maintains productivity advantages Eliminating performance penalty using System.EnterpriseServices; [Transaction] //Uses System.Transactions, gets promotion public class MyComponent : ServicedComponent { [AutoComplete] public void MyMethod() {...} }
Explicit Model in .NET 2.0 Via TransactionScope public class TransactionScope : IDisposable { public void Complete(); public void Dispose(); public TransactionScope(); public TransactionScope(Transaction transactionToUse); public TransactionScope(TransactionScopeOption scopeOption); public TransactionScope(TransactionScopeOption scopeOption, TransactionOptions transactionOptions); TimeSpan scopeTimeout); //Additional constructors }
TransactionScope Provides ambient transaction to code section Creates transaction Sets Transaction.Current Disposable object Transaction ends once disposed Requires committable transaction
TransactionScope Complete() sets consistency bit to true Scope maintains consistency bit Defaults to false Transaction aborts if consistency bit is false Complete() sets consistency bit to true Transaction will try to commit No way to set back to false using(TransactionScope scope = new TransactionScope()) { /* Perform transactional work here */ //No errors - commit transaction scope.Complete(); }
Ambient Transaction
TransactionScope Can only call Complete() once InvalidOperationException After Complete() is called, cannot access Transaction.Current Can access again after Dispose()
TransactionScope Dispose() restores original ambient If code takes long time to complete: May be indicative of transactional deadlock Default timeout is 60 seconds Transaction will abort automatically If Dispose() never called: Transaction aborts once timeout is expired
TransactionScope Dispose() throws TransactionAbortedException if fails to commit Can alert user or log error Usually better to let exception propagate up No need to abort in catch block
Transaction Flow Management Scopes can nest directly //Direct scope nesting using(TransactionScope scope1 = new TransactionScope()) { using(TransactionScope scope2 = new TransactionScope()) /* Perform transactional work here, vote on scope2 */ scope2.Complete(); } scope1.Complete();
Transaction Flow Management Scopes can nest indirectly //Indirect scope nesting. void RootMethod() { using(TransactionScope scope = new TransactionScope()) SomeMethod(); scope.Complete(); } void SomeMethod()
Transaction Flow Management Top-most scope is called root scope TransactionScope has constructors that accept TransactionScopeOption public enum TransactionScopeOption { Required, RequiresNew, Suppress } TransactionScope scope; scope = new TransactionScope(TransactionScopeOption.Required); using(scope) {…}
Transaction Flow Management Default is TransactionScopeOption.Required TransactionScopeOption controls transaction flow into nested scope Decided at construction time
Transaction Flow Management Flow decided based on ambient transaction and TransactionScopeOption TransactionScopeOption Ambient Transaction The scope will take part in Required No New Transaction (will be the root) Requires New Suppress No Transaction Yes
Transaction Flow
Transaction Flow Management Transaction A No Ambient Transaction Code block {Required} 1 {Required} 2 {Suppress} 4 Transaction B {RequiresNew} 3
Developing a Resource Manager In the future, MS will provide transaction aware versions of System.Collections, System.IO, etc. Other managed data providers (not just SqlClient) But, .NET 2.0 provides facilities for implementing a custom RM Implement IEnlistmentNotification for auto enlistment, rollback and commit behavior
Developing a Resource Manager Distributed Tx Coordinator (DTC) Lightweight Transaction Manager (LTM) Etc. Transaction Manager SQL Server 2005 Oracle MSMQ Transacted Hashtable Resource Manager Application
Developing Resource Manager
Summary Huge Transactions improvements in .NET 2.0 Simple programming model Start fast with the LTM; stays in the LTM if volatile RM Automatically promotes transaction when needed Support for building custom RMs Use more transactions in your applications! Transactions are not just for databases anymore
Your Feedback is Important! Please Fill Out a Survey for This Session on CommNet
© 2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.