Download presentation
Presentation is loading. Please wait.
2
DEV 305 Introducing System.Transactions
Sarvashrestha Paliwal Tarun Anand ISV Technical Evangelist Microsoft Most Valuable Professional
3
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
4
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
5
Transactions in .NET 1.1
6
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
7
Transactions in .NET 1.1 Gets even more complex with multiple objects and multiple resources Transaction Client Obj Obj Obj DB DB DB
8
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
9
Transactions in .NET 1.1 What happens if the DB update fails?
The hashtable value is lost!
10
.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
11
.NET 2.0 Transaction Will use distributed transaction when
Remote calls Multiple durable resources Uses the old DTC with minor changes
12
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
13
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 }
14
Transactions Objects Additional functionality of Transaction Enlist RM
Set isolation level Obtain transaction information Clone transaction Completion event
15
Transactions Objects CommittableTransaction used to commit transaction
Additional functionality Asynchronous commit [Serializable] public sealed class CommittableTransaction : Transaction,IAsyncResult { public void Commit(); //Other members }
16
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() {...} }
17
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 }
18
TransactionScope Provides ambient transaction to code section
Creates transaction Sets Transaction.Current Disposable object Transaction ends once disposed Requires committable transaction
19
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(); }
20
Ambient Transaction
21
TransactionScope Can only call Complete() once
InvalidOperationException After Complete() is called, cannot access Transaction.Current Can access again after Dispose()
22
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
23
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
24
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();
25
Transaction Flow Management
Scopes can nest indirectly //Indirect scope nesting. void RootMethod() { using(TransactionScope scope = new TransactionScope()) SomeMethod(); scope.Complete(); } void SomeMethod()
26
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) {…}
27
Transaction Flow Management
Default is TransactionScopeOption.Required TransactionScopeOption controls transaction flow into nested scope Decided at construction time
28
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
29
Transaction Flow
30
Transaction Flow Management
Transaction A No Ambient Transaction Code block {Required} 1 {Required} 2 {Suppress} 4 Transaction B {RequiresNew} 3
31
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
32
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
33
Developing Resource Manager
34
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
35
Your Feedback is Important!
Please Fill Out a Survey for This Session on CommNet
36
© 2005 Microsoft Corporation. All rights reserved.
This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.