1 / 36

DEV 305 Introducing System.Transactions

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)

duena
Download Presentation

DEV 305 Introducing System.Transactions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DEV 305 Introducing System.Transactions Sarvashrestha Paliwal Tarun AnandISV Technical EvangelistMicrosoft Most Valuable Professional

  2. 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

  3. 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

  4. Transactions in .NET 1.1

  5. Transactions in .NET 1.1 • Pros • Straightforward • Cons • Most-suitable for a single object interacting with a single database Transaction Obj Client DB

  6. Obj Obj Transactions in .NET 1.1 • Gets even more complex with multiple objects and multiple resources Transaction Client Obj DB DB DB

  7. 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

  8. Transactions in .NET 1.1 What happens if the DB update fails? The hashtable value is lost!

  9. .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

  10. .NET 2.0 Transaction • Will use distributed transaction when • Remote calls • Multiple durable resources • Uses the old DTC with minor changes

  11. 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

  12. Transactions Objects • Transactionrepresents 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 }

  13. Transactions Objects • Additional functionality of Transaction • Enlist RM • Set isolation level • Obtain transaction information • Clone transaction • Completion event

  14. Transactions Objects • CommittableTransaction used to commit transaction • Additional functionality • Asynchronous commit [Serializable] public sealed class CommittableTransaction : Transaction,IAsyncResult { public void Commit(); //Other members }

  15. 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() {...} }

  16. 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); public TransactionScope(TransactionScopeOption scopeOption, TimeSpan scopeTimeout); //Additional constructors }

  17. TransactionScope • Provides ambient transaction to code section • Creates transaction • Sets Transaction.Current • Disposable object • Transaction ends once disposed • Requires committable transaction

  18. TransactionScope • 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(); }

  19. Ambient Transaction

  20. TransactionScope • Can only call Complete() once • InvalidOperationException • After Complete() is called, cannot access Transaction.Current • InvalidOperationException • Can access again after Dispose()

  21. 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

  22. 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

  23. 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(); }

  24. Transaction Flow Management • Scopes can nest indirectly //Indirect scope nesting. void RootMethod() { using(TransactionScope scope = new TransactionScope()) { SomeMethod(); scope.Complete(); } } void SomeMethod() { using(TransactionScope scope = new TransactionScope()) { scope.Complete(); } }

  25. 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) {…}

  26. Transaction Flow Management • Default is TransactionScopeOption.Required • TransactionScopeOption controls transaction flow into nested scope • Decided at construction time

  27. Transaction Flow Management • Flow decided based on ambient transaction and TransactionScopeOption

  28. Transaction Flow

  29. 1 2 3 4 {Required} {Required} {RequiresNew} {Suppress} Transaction Flow Management Transaction A No AmbientTransactionCode block Transaction B

  30. 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

  31. Resource Manager Developing a Resource Manager Distributed Tx Coordinator (DTC) Lightweight Transaction Manager (LTM) Etc. Transaction Manager SQL Server 2005 Oracle MSMQ Transacted Hashtable Application

  32. Developing Resource Manager

  33. 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

  34. Your Feedbackis Important! Please Fill Out a Survey forThis Session on CommNet

  35. © 2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

More Related