.NET Transactions Training

1,244 views

Published on

http://www.Intertech.com

This slide deck is from an Intertech presentation on .NET Transactions delivered at Microsoft.

Published in: Technology
1 Comment
1 Like
Statistics
Notes
  • IT Pathshala, Noida-Sector-63, offers Guaranteed JOB with .NET Training Programs.

    PAY US ONLY IF YOU GET A JOB!

    CALL US - +91-9871350999 or +91-8800394970

    Technology Centre Address: H-110, Sector-63, Noida (UP)

    Program Takeaways:

    - GUARANTEED JOB in your technology domain
    - Live Major Project development
    - Experience Letter on Training Completion
    - Certification on Training Completion
    - Project Source Code available in CD

    Program Highlights:

    - 100% Guaranteed Job – Don’t pay us if you don’t get a JOB!
    - Industry Ready Courses
    - LIVE International Project Experience
    - Exposure to corporate culture at our IT services facility
    - Networking & Interaction with Industry Experts
    - Training Provided by a reputed IT Company
    - Live Project Source Code made available in CD
    - Technology certification and experience letter at program completion
    - Online Study Content

    CALL US - +91-9871350999 or +91-8800394970

    Our website: www.itpathshala.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
1,244
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
50
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

.NET Transactions Training

  1. 1. Session Overview Review transactions in .NET 1.1 – Review some transaction terminology – See the current issues with SqlTransaction and Enterprise Services Study the new features in .NET 2.0 – Understand the role of the Lightweight Transaction Manager (LTM) – See how transaction promotion works – Learn how to manually work with transactions – Learn how to simplify transaction programming with TransactionScope Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 5
  2. 2. The Importance of Transactions Transactions are the cornerstone of any reliable app – Ensure ACID characteristics when updating critical information – Abstract complex compensation logic using simple APIs: e.g. Commit/Rollback, SetComplete/SetAbort Transactions are used in a variety of contexts: – Updating a single data source – Updating several data sources (distributed Tx) – Grouping multiple messages that must all be processed in the correct order (MSMQ) Service-orientation brings additional Tx needs. – quot;Services are autonomousquot; assumes reliability and correctness Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 6
  3. 3. Transaction Participants Distributed Tx Coordinator (DTC) Lightweight Transaction Manager (LTM) Etc. Transaction Transaction Manager Manager SQL Server 2005 Oracle MSMQ Transacted Hashtable Application Application Resource Resource Manager Manager Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 7
  4. 4. Why Not Tx Everywhere? Transactions are expensive – Tx management causes overhead – Require locks on resources and thus inhibits scalability – Distributed transactions require expensive two-phase commit mechanisms Transaction code can be messy – Try/Catch/Finally required to handle exceptions and rollback – Transaction logic is mixed with domain logic Enterprise Services can clean up the code, but … – Course grained: objects are transactional, not methods – Performance: uses DTC even against a single database – Must derive from ServicedComponent – Retains some COM-like hassles: registration, deployment, etc. Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 8
  5. 5. Other Transaction Issues Different technologies have different Tx APIs: – TSQL – ADO.NET Transactions (e.g. SqlTransaction) – MSMQ: MessageQueueTransaction – Multi-resource updates require Enterprise Services: ContextUtil, AutoComplete, etc. Some updateable resources have no Tx support: – File system updates – In memory (volatile) collection objects: ArrayList, Hashtable, etc. Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 9
  6. 6. Tx Scenario 1: Single DB Update static void SingleDBUpdate() { using (SqlConnection cnn = new SqlConnection(ConnString)) { cnn.Open(); SqlTransaction tx = cnn.BeginTransaction(); // Must associate tx with each SQL command. SqlCommand update1 = new SqlCommand(quot;DELETE ...quot;, cnn, tx); SqlCommand update2 = new SqlCommand(quot;INSERT ...quot;, cnn, tx); try { update1.ExecuteNonQuery(); update2.ExecuteNonQuery(); tx.Commit(); } catch { tx.Rollback(); } } } Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 10
  7. 7. Tx Scenario 2: Multi DB Update [Transaction(TransactionOption.Required)] public class Mover : ServicedComponent { [AutoComplete] public void Move() using(SqlConnectionRelatively clean code but … { Relatively clean code but … cnnDB1 = new SqlConnection(Database1), cnnDB2 = new SqlConnection(Database2)) { • Must derive from ServicedComponent • Must derive from ServicedComponent SqlCommand cmdDeleteDB1be in strong named assembly • Must be in strong named assembly • Must = new SqlCommand(quot;DELETE ...quot;, in COM+ • Must register in COM+ cnnDB1); SqlCommand cmdInsertDB2 register • Must = new SqlCommand(quot;INSERT ...quot;, from single DB scenario • Very different cnnDB2); • Very different from single DB scenario // ADO.NET connections automatically enlist into // the DTC transaction cnnDB1.Open(); cnnDB2.Open(); cmdDeleteDB1.ExecuteNonQuery(); cmdInsertDB2.ExecuteNonQuery(); } } } Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 11
  8. 8. Tx Scenario 3: DB + Volatile Update [Transaction(TransactionOption.Required)] public class Mover : ServicedComponent { [AutoComplete] public void Move(object key) { object val = hashTable[key]; hashTable.Remove(key); using(SqlConnection cnnDB2 = new SqlConnection(Database2)) { // Insert value from hash table into DB SqlCommand cmdInsertDB2 = new SqlCommand(quot;INSERT ...quot;, cnnDB2); cnnDB2.Open(); cmdInsertDB2.ExecuteNonQuery(); } What happens if the DB update fails? What happens if the DB update fails? } } The hashtable value is lost! The hashtable value is lost! Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 12
  9. 9. Transactions in .NET 2.0 .NET 2.0 Transaction goals: – Provide a simple, common programming model for all Tx scenarios – Provide fast Tx management of volatile resources – Reduce initial Tx overhead by defaulting to a minimal manager – Automatically promote to a distributed Tx manager (e.g. DTC) when needed – Use an efficient single phase enlistment for single resources – Simplify creation of custom resource managers (e.g. roll your own transacted Hashtable). Make transactional programming ubiquitous! Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 13
  10. 10. .NET 2.0 Transaction Overview Declarative Transactions Declarative Transactions Indigo System.Transactions System.Transactions Implicit Tx Implicit Tx Manual Tx Manual Tx TransactionScope TransactionScope CommittableTransaction CommittableTransaction .NET 2.0 Lightweight Transaction Manager Lightweight Transaction Manager Transactional FS Distributed Tx Participates Transactional FS Distributed Tx (Longhorn?) Coordinator when needed (Longhorn?) Coordinator Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 14
  11. 11. Lightweight Transaction Manager The Lightweight Transaction Manager (LTM) is – The common starting point for all Txs – A full-fledged Tx manager for volatile resources – Extremely fast: minimal overhead when using volatile resource in same app domain LTM provides a quot;pay as you goquot; mechanism – Promotes the Tx to other managers as needed – Uses Promotable Single Phase Enlistment (PSPE) when interacting with a single remote resource manager – Uses DTC for a single remote RM that does not support PSPE – Uses DTC for multiple remote RMs Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 15
  12. 12. LTM: Dynamic Transaction Promotions The LTM promotes Txs automatically When an application starts a transaction: – LTM creates and owns the Tx – User touches an RM, RM auto enlists – If RM is volatile, LTM continues to own Tx – If RM is durable PSPE (like SQL Server 2005), LTM transfers ownership to RM – If 2nd RM enlists, or if RM is remote, LTM transfers ownership to DTC Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 16
  13. 13. LTM: Dynamic Transaction Promotions Start Transaction Start Transaction LTM LTM Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 17
  14. 14. LTM: Dynamic Transaction Promotions TxHashTable.Add() LTM LTM Start Transaction Enlist Volatile TxHashTable.Add(…) Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 18
  15. 15. LTM: Dynamic Transaction Promotions SqlExecute(cmd1) LTM LTM SQL Server SQL Server Start Transaction TxHashTable.Add(…) 2005 2005 SqlExecute(cmd1) Enlist durable and PSPE Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 19
  16. 16. LTM: Dynamic Transaction Promotions SqlExecute(cmd2) LTM LTM SQL Server SQL Server Start Transaction TxHashTable.Add(…) 2005 2005 SqlExecute(cmd1) SqlExecute(cmd2) DTC DTC Enlist durable Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 20
  17. 17. Performance: LTM vs. Enterprise Services 159509.67 160000 153201.00 148116.17 Enterprise Services 139601.33 140000 System.Transactions 125694.17 Transactions per second 120000 100000 80000 Test Computer: 60000 •256 MB RAM •1 x 0.757 GHz 40000 20000 480.60 544.27 622.17 809.29 915.18 0 1 2 4 8 16 • Radically reduced intra-app • ~3 microseconds to start/commit a domain transaction overhead null transaction on this processor Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 21
  18. 18. .NET 2.0 Transaction Overview Declarative Transactions Declarative Transactions Indigo System.Transactions System.Transactions Implicit Tx Implicit Tx Manual Tx Manual Tx TransactionScope TransactionScope CommittableTransaction CommittableTransaction .NET 2.0 Lightweight Transaction Manager Lightweight Transaction Manager Transactional FS Distributed Tx Participates Transactional FS Distributed Tx (Longhorn?) Coordinator when needed (Longhorn?) Coordinator Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 22
  19. 19. .NET 2.0 Manual Transactions The Transaction Class [Serializable] public class Transaction : ITransaction, IDisposable, ISerializable { // Properties public static Transaction Current { get; set; } public IsolationLevel IsolationLevel { get; } public TransactionInformation TransactionInformation { get; } // Methods public Transaction Clone(); public void Rollback(Exception e); } Here's Rollback, but where is Commit? Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 23
  20. 20. .NET 2.0 Manual Transactions The CommittableTransaction Class [Serializable] public sealed class CommittableTransaction : Transaction { public void Commit(); public IAsyncResult BeginCommit( AsyncCallback asyncCallback, object asyncState); public void EndCommit(IAsyncResult asyncResult); public CommittableTransaction(); public CommittableTransaction(TimeSpan timeout); public CommittableTransaction(TransactionOptions options); } Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 24
  21. 21. .NET 2.0 Manual Transactions TransactionOptions [Serializable] public sealed class CommittableTransaction : Transaction public struct TransactionOptions { { public void Commit(); public TimeSpan Timeout { get; set; } public IsolationLevel IsolationLevel { get; set; } public IAsyncResult BeginCommit( }AsyncCallback asyncCallback, object asyncState); public void EndCommit(IAsyncResult asyncResult); public CommittableTransaction(); public CommittableTransaction(TimeSpan timeout); public CommittableTransaction(TransactionOptions options); } Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 25
  22. 22. .NET 2.0 Manual Transactions - Example static void Main(string[] args) { CommittableTransaction cTx = new CommittableTransaction(); // Set the ambient Tx. Call Clone so that only we can // commit the Tx. Transaction.Current = cTx.Clone(); try { Clone returns an ITransaction Task1(); reference. Therefore, the Tx Task2(); cannot be committed using this cTx.Commit(); object. } catch { cTx.Rollback(); } } Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 26
  23. 23. .NET 2.0 Transaction Overview Declarative Transactions Declarative Transactions Indigo System.Transactions System.Transactions Implicit Tx Implicit Tx Manual Tx Manual Tx TransactionScope TransactionScope CommittableTransaction CommittableTransaction .NET 2.0 Lightweight Transaction Manager Lightweight Transaction Manager Transactional NTFS Distributed Tx Participates Transactional NTFS Distributed Tx (Longhorn?) Coordinator when needed (Longhorn?) Coordinator Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 27
  24. 24. .NET 2.0 Implicit Transactions Most developers will use implicit transactions – TransactionScope + quot;usingquot; establishes a transaction context – Call Complete() to indicate that you are happy with results – On Dispose, TransactionScope will commit if you called Complete(), or abort otherwise. You can nest TransactionScopes – Similar to serviced component, you can choose to use the current transaction, or create a new one Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 28
  25. 25. .NET 2.0 Implicit Transactions TransactionScope public class TransactionScope : IDisposable { public TransactionScope(); public TransactionScope(TransactionScopeOption scopeOption); public TransactionScope(Transaction transactionToUse, TimeSpan scopeTimeout); public TransactionScope(Transaction transactionToUse); public TransactionScope(TransactionScopeOption scopeOption, TimeSpan scopeTimeout); public TransactionScope(TransactionScopeOption scopeOption, TransactionOptions transactionOptions); public void Complete(); public void Dispose(); } Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 29
  26. 26. .NET 2.0 Implicit Transactions TransactionScopeOption public class TransactionScope : IDisposable { public TransactionScope(); public TransactionScope(TransactionScopeOption scopeOption); public TransactionScope(Transaction transactionToUse, TimeSpan scopeTimeout); public TransactionScope(Transaction transactionToUse); public TransactionScope(TransactionScopeOption scopeOption, public enum TransactionScopeOption TimeSpan scopeTimeout); { public TransactionScope(TransactionScopeOption scopeOption, Required, TransactionOptions transactionOptions); RequiresNew, public void Complete(); Suppress } public void Dispose(); Suppresses ambient Tx in } scope. RMs will not enlist. Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 30
  27. 27. Using TransactionScope Default TxScopeOption is quot;Requiredquot; static void RunImplicitTx() { using (TransactionScope ts = new TransactionScope()) { Update1(); Any System.Tx aware RMs auto Update2(); enlist in ambient Tx Update3(); ts.Complete(); } If we get here, we are happy } Calls TxScope.Dispose() Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 31
  28. 28. Using TransactionScope TransactionScope.Dispose(): pseudo-code If (User did not call Complete()) Rollback current Tx else if (this scope created current Tx)) Commit current Tx else Call Complete() on current Tx, Pop Tx stack Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 32
  29. 29. Nesting Transaction Scopes using (TxScope ts = new TxScope()) { ... } Pu Dependent Tx Dependent Tx using (TxScope ts = sh new TxScope()) Push Dependent Tx { ... } sh Dependent Tx Pu Committable Tx Committable Tx using (TxScope ts = new TxScope()) { ... } Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 33
  30. 30. Nested Scopes – Happy Scenario using (TxScope ts = new TxScope()) Complete() { ... } Dependent Tx Dependent Tx using (TxScope ts = new TxScope()) Dependent Tx { ... } Dependent Tx Committable Tx Committable Tx using (TxScope ts = new TxScope()) { ... } ts.Complete() Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 34
  31. 31. Nested Scopes – Happy Scenario using (TxScope ts = new TxScope()) { ... } Complete() using (TxScope ts = new TxScope()) Dependent Tx { ... } Dependent Tx ts.Complete() Committable Tx Committable Tx Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 35
  32. 32. Nested Scopes – Happy Scenario using (TxScope ts = new TxScope()) { ... } ts.Complete() Commit() Committable Tx Updates performed under this Tx and all dependent Txs are committed. Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 36
  33. 33. Nested Scopes and Requires New using (TxScope ts = new TxScope()) { ... } Pu Dependent Tx Dependent Tx using (TxScope ts = sh new TxScope( Push Committable Tx RequiresNew)) Committable Tx sh { ... } Pu Committable Tx Committable Tx using (TxScope ts = new TxScope()) { ... } Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 37
  34. 34. Nested Scopes and Requires New using (TxScope ts = new TxScope()) Complete() { ... } Dependent Tx Dependent Tx using (TxScope ts = new TxScope( Committable Tx RequiresNew)) Committable Tx { ... } Committable Tx Committable Tx using (TxScope ts = new TxScope()) { ... } ts.Complete() Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 38
  35. 35. Nested Scopes and Requires New using (TxScope ts = new TxScope()) { ... } Commit() using (TxScope ts = new TxScope( Committable Tx RequiresNew)) Committable Tx { ... } Committable Tx ts.Complete() Committable Tx Updates performed under this Tx and all dependent Txs are committed. Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 39
  36. 36. Nested Scopes and Requires New using (TxScope ts = new TxScope()) { ... } ts.Complete() Commit() Committable Tx Committable Tx Updates performed under this Tx are committed. Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 40
  37. 37. Tx Scenario 1: Single DB Update static void SingleDBUpdate() { using (SqlConnection cnn = new SqlConnection(ConnString)) { cnn.Open(); SqlTransaction tx = cnn.BeginTransaction(); // Must associate tx with each SQL command. SqlCommand update1 = new SqlCommand(quot;DELETE ...quot;, cnn, tx); SqlCommand update2 = new SqlCommand(quot;INSERT ...quot;, cnn, tx); try { update1.ExecuteNonQuery(); update2.ExecuteNonQuery(); tx.Commit(); } catch { tx.Rollback(); } } } Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 41
  38. 38. Tx Scenario 1 using .NET 2.0 static void SingleDBUpdate() { using (TransactionScope ts = new TransactionScope()) { using (SqlConnection cnn = new SqlConnection(ConnString)) { cnn.Open(); SqlCommand update1 = new SqlCommand(quot;DELETE ...quot;, cnn); SqlCommand update2 = new SqlCommand(quot;INSERT ...quot;, cnn); update1.ExecuteNonQuery(); update2.ExecuteNonQuery(); ts.Complete(); } No need to Begin/Commit/Rollback Tx } } No try/catch/finally No special ctor used to associate commands with Tx No performance penalty when using PSPE (SQL 2005) Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 42
  39. 39. Tx Scenario 2: Multi DB Update [Transaction(TransactionOption.Required)] public class Mover : ServicedComponent { [AutoComplete] public void Move() { Remember these issues … Remember these issues … using(SqlConnection cnnDB1 = new SqlConnection(Database1), • Must derive from ServicedComponent cnnDB2 = new SqlConnection(Database2)) • Must derive from ServicedComponent { SqlCommand cmdDeleteDB1 be in strong named assembly • Must be in strong named assembly • Must = new SqlCommand(quot;DELETE ...quot;, in COM+ • Must register cnnDB1); • Must register in COM+ SqlCommand cmdInsertDB2 = • Very different from single DB scenario • Very different from single DB scenario new SqlCommand(quot;INSERT ...quot;, cnnDB2); // ADO.NET connections automatically enlist into // the DTC transaction cnnDB1.Open(); cnnDB2.Open(); cmdDeleteDB1.ExecuteNonQuery(); cmdInsertDB2.ExecuteNonQuery(); } } } Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 43
  40. 40. Tx Scenario 2 Using .NET 2.0 public class Mover { public void Move() { using (TransactionScope ts = new TransactionScope()) { using (SqlConnection cnnDB1 = new SqlConnection(Database1), cnnDB2 = new SqlConnection(Database2)) { SqlCommand cmdDeleteDB1 = new SqlCommand(quot;DELETE ...quot;, cnnDB1); SqlCommand cmdInsertDB2 = new SqlCommand(quot;INSERT ...quot;, cnnDB2); cnnDB1.Open(); cnnDB2.Open(); cmdDeleteDB1.ExecuteNonQuery(); cmdInsertDB2.ExecuteNonQuery(); ts.Complete(); } } } } Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 44
  41. 41. Tx Scenario 2 Using .NET 2.0 public class Mover { public void Move() { using (TransactionScope ts = new TransactionScope()) { using (SqlConnection cnnDB1 = new SqlConnection(Database1), cnnDB2 = new SqlConnection(Database2)) { SqlCommand cmdDeleteDB1 = new SqlCommand(quot;DELETE ...quot;, cnnDB1); No need to derive from ServicedComponent SqlCommand cmdInsertDB2 = new SqlCommand(quot;INSERT ...quot;, cnnDB2); Assembly does not need to be strong named cnnDB1.Open(); cnnDB2.Open(); No special registration requirements cmdDeleteDB1.ExecuteNonQuery(); cmdInsertDB2.ExecuteNonQuery(); Code is not much different from the single DB scenario ts.Complete(); } } No performance penalty compared to ES (still uses DTC) } } Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 45
  42. 42. Tx Scenario 3: DB + Volatile Update [Transaction(TransactionOption.Required)] public class Mover : ServicedComponent { [AutoComplete] public void Move(object key) { object val = hashTable[key]; hashTable.Remove(key); using(SqlConnection cnnDB2 = new SqlConnection(Database2)) { // Insert value from hash table into DB SqlCommand cmdInsertDB2 = new SqlCommand(quot;INSERT ...quot;, cnnDB2); cnnDB2.Open(); cmdInsertDB2.ExecuteNonQuery(); } } What happens if the DB update fails? What happens if the DB update fails? } The hashtable value is lost! The hashtable value is lost! Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 46
  43. 43. Tx Scenario 3 Using .NET 2.0 public class VolatileToDBMover { // Does not exist, but not too hard to implement TransactedHashtable txHashtable = new TransactedHashtable(); public void Move(object key) { using (TransactionScope ts = new TransactionScope()) { object val = txHashtable[key]; txHashtable.Remove(key); using (SqlConnection cnnDB2 = new SqlConnection(Database2)) { // Insert value from hash table into DB SqlCommand cmdInsertDB2 = new SqlCommand(quot;INSERT ...quot;, cnnDB2); cnnDB2.Open(); cmdInsertDB2.ExecuteNonQuery(); ts.Complete(); } } } } Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 47
  44. 44. Tx Scenario 3 Using .NET 2.0 public class VolatileToDBMover { // Does not exist, but not too hard to implement TransactedHashtable txHashtable = new TransactedHashtable(); public void Move(object key) { using (TransactionScope ts = new TransactionScope()) { object val = txHashtable[key]; txHashtable.Remove(key); using (SqlConnection cnnDB2 = new SqlConnection(Database2)) { // Insert value from hash table into DB SqlCommand Transacted hash table enlists in ambient Tx cmdInsertDB2 = new SqlCommand(quot;INSERT ...quot;, cnnDB2); Remove is rolled back if the Tx fails cnnDB2.Open(); cmdInsertDB2.ExecuteNonQuery(); ts.Complete(); } } } } Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 48
  45. 45. 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 More help is coming in Longhorn – Transactional NTFS, Windows Logging System (WinLS) – System.Transactions.Recovery, System.Transactions.Isolation Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 49
  46. 46. Best Practices Avoid using manual transactions; use TransactionScope instead Don't create a TransactionScope outside of a using block – Not disposing a TransactionScope can leave pending Tx locks in the database For best performance, consider using SqlTransaction against a single SQL Server 2000 (no DTC) Use System.Transactions for SQL Server 2005 and multi DB updates For a common, simple Tx programming model use TransactionScope Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 50
  47. 47. Summary Huge Tx improvements in .NET 2.0 – Simple programming model – Starts fast with the LTM; stays in the LTM if volatile RM – Automatically promotes transaction when needed – Support for building custom RMs More transaction features on the way – Declarative transactions in Indigo – Base and helper classes for easier custom RM development (System.Transactions.Isolation, System.Transactions.Recovery) – More OS support: Transactional NTFS, Transactional Registry, Windows Logging System Use more transactions in your applications! – Transactions are not just for databases anymore Questions? Comments? tbarnaby@IntertechTraining.com Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 51
  48. 48. About Intertech Training Developer training delivered by the experts Our site Your site On the Web Web Course Live – Virtual training delivered live by our expert instructors – Ask questions, work on labs, never leave your desk – All you need is a browser and a phone (headset recommended) www.IntertechTraining.com Copyright © Intertech Training 2005 • www.intertechtraining.com • 800-966-9884 • Slide 52

×