Introducing Introducing System.Transaction System.Transaction s s Nigel Watson Nigel Watson Architect Advisor Architect Advisor [email protected][email protected]Developer Platform Strategy Group Developer Platform Strategy Group Microsoft Microsoft DAT214
49
Embed
Introducing System.Transactions Nigel Watson Architect Advisor [email protected] Developer Platform Strategy Group Microsoft DAT214.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Not one, but two Transaction programming Not one, but two Transaction programming models in .NET 1.1models in .NET 1.1
Both models have disadvantagesBoth models have disadvantagesNeither superior to the other in every respectNeither superior to the other in every respect
.NET 2.0 unifies benefits of both models.NET 2.0 unifies benefits of both modelsA single, unified way to create transactional codeA single, unified way to create transactional code
Minimizing hand crafted codeMinimizing hand crafted code
Separate from hosting environment and instance Separate from hosting environment and instance managementmanagement
Who cares about transactions, Who cares about transactions, anyway? anyway?
Transactions in .NET 1.1Transactions in .NET 1.1
string connectionString = "…"; IDbConnection connection = new SqlConnection(connectionString);connection.Open();IDbCommand command = new SqlCommand();command.Connection = connection;IDbTransaction transaction;transaction = connection.BeginTransaction(); //Enlistingcommand.Transaction = transaction;try{ // Interact with database here transaction.Commit();}catch{ // Oops… transaction.Rollback(); //Abort transaction}finally{ connection.Close();}
Explicit Model in .NET 1.1Explicit Model in .NET 1.1
Explicit Model in .NET 1.1 Explicit Model in .NET 1.1
DBDB
ClientClient ObjObj TransactionTransaction
Nice and straight-forwardNice and straight-forward
DBDB
Explicit Model in .NET 1.1 Explicit Model in .NET 1.1
But, breaks with multiple objects…But, breaks with multiple objects…
ClientClient ObjObjTransactionTransaction
ObjObjObjObj
DBDB
Explicit Model in .NET 1.1 Explicit Model in .NET 1.1
… … and breaks even harder with multiple and breaks even harder with multiple objects and multiple resourcesobjects and multiple resources
DBDB
TransactionTransactionObjObj
ObjObjObjObj
DBDB
ClientClient
Distributed Transactions Distributed Transactions
Two or more parties or execution Two or more parties or execution contextscontexts
Impractical to manage in your codeImpractical to manage in your code
Need a dedicated 2pc monitorNeed a dedicated 2pc monitor
Managed on Windows by COM+ DTCManaged on Windows by COM+ DTCSystem serviceSystem service
Creates new transactionsCreates new transactions
Propagates transactions across machinesPropagates transactions across machines
Collects resources votesCollects resources votes
Instructs RMs to rollback or commitInstructs RMs to rollback or commit
Declarative model in .NET 1.1Declarative model in .NET 1.1
To use COM+ DTC from .NET 1.1, you To use COM+ DTC from .NET 1.1, you have two optionshave two options
Code against COM+ directly (via interop)Code against COM+ directly (via interop)
Use System.EnterpriseServicesUse System.EnterpriseServices
using System.EnterpriseServices;
[Transaction]public class MyComponent : ServicedComponent{ [AutoComplete] public void MyMethod() { // Interact with other serviced // components and resource managers }}
Declarative Model in .NET 1.1Declarative Model in .NET 1.1
ProblemsProblems
Derivation from Derivation from ServicedComponentServicedComponentPerformance penalty for non-distributed Performance penalty for non-distributed transactionstransactions
COM+ hosting modelCOM+ hosting model
No easy way for multiple threads to No easy way for multiple threads to participate in same transactionparticipate in same transaction
Transactions in .NET 1.1Transactions in .NET 1.1
So…. You are forced to choose:So…. You are forced to choose:Enterprise Services for distributedEnterprise Services for distributed
ADO.NET (maybe) for localADO.NET (maybe) for local
… … and this gets hard-coded into your appsand this gets hard-coded into your apps
.NET 2.0 Transactions.NET 2.0 Transactions
System.TransactionsSystem.Transactions
Single, unified development modelSingle, unified development modelSupported by ADO.NET 2.0 Supported by ADO.NET 2.0 Lightweight Transaction Manager (LTM)Lightweight Transaction Manager (LTM)
Manages ‘local’ transactionsManages ‘local’ transactionsSingle durable resource, no remotingSingle durable resource, no remoting
[Serializable]public sealed class CommittableTransaction : Transaction, IAsyncResult{ public void Commit(); public IAsyncResult BeginCommit(…); public void EndCommit(…);
Requires NewRequires New YesYes New Transaction New Transaction (will be the root)(will be the root)
SuppressSuppress YesYes No TransactionNo Transaction
Nested scopes Nested scopes
Other TransactionScope optionsOther TransactionScope options
TimeoutsTimeoutsDefault is 60 secondsDefault is 60 seconds
After timeout, transaction auto-abortsAfter timeout, transaction auto-aborts
Smallest timeout in nested ambient Smallest timeout in nested ambient transactions usedtransactions used
Use 0 for infinite, but… careful!Use 0 for infinite, but… careful!
Isolation levelIsolation levelDefault is SerializableDefault is Serializable
Departure from this is at own consistency Departure from this is at own consistency risk – make sure you know what you are risk – make sure you know what you are doing…doing…
Supports LTM iff single, local resource.Supports LTM iff single, local resource.
using System.EnterpriseServices; [Transaction] //Uses System.Transactions, gets promotion public class MyComponent : ServicedComponent{ [AutoComplete] public void MyMethod() {...}}
Enterprise Services Code in 2.0 Enterprise Services Code in 2.0
ConcurrencyConcurrency
Concurrency ManagementConcurrency Management
Transactions and concurrency? Transactions and concurrency? Uhhh…Uhhh…
One thread aborts the other commitsOne thread aborts the other commits
Ambient transaction stored in TLSAmbient transaction stored in TLSWill not propagate to worker threadsWill not propagate to worker threads
Creates a Creates a DependentTransaction DependentTransaction of the existing Transactionof the existing Transaction
Parent keeps tabs on clones, which can Parent keeps tabs on clones, which can be passed to other threadsbe passed to other threads
Must create unique clones for each Must create unique clones for each threadthread
cloneOptioncloneOption parameter to parameter to DependentClone()DependentClone()
RollbackIfNotCompleteRollbackIfNotComplete
BlockCommitUntilCompleteBlockCommitUntilComplete
Concurrent transactionsConcurrent transactions
Roll your own transactional Roll your own transactional typestypes
Create your own transactional typesCreate your own transactional types
Any object that implements Any object that implements IEnlistmentNotificationIEnlistmentNotification can enlist in can enlist in transactionstransactionsCall Call Transaction.EnlistVolatile()Transaction.EnlistVolatile() or or Transacation.EnlistDurable()Transacation.EnlistDurable() to be to be notified of important transaction lifecycle notified of important transaction lifecycle eventsevents
Consider extending implementation to Consider extending implementation to include include ISinglePhaseNotificationISinglePhaseNotification for for single-phase commit supportsingle-phase commit support
Transactional int class Transactional int class
SummarySummary
Existing .NET 1.1 transactional Existing .NET 1.1 transactional approaches may paint you into a cornerapproaches may paint you into a corner
.NET 2.0 introduces new unified .NET 2.0 introduces new unified transactional model that unifies explicit transactional model that unifies explicit and declarative approachesand declarative approaches
Writing reliable code much easier with Writing reliable code much easier with transactionstransactions
Can make pretty much anything Can make pretty much anything transactional in .NET 2.0transactional in .NET 2.0
We invite you to participate in ourWe invite you to participate in our online evaluationonline evaluation on CommNet,on CommNet,
accessible Friday onlyaccessible Friday only
If you choose to complete the evaluation online, If you choose to complete the evaluation online, there isthere is no need to complete the paper evaluationno need to complete the paper evaluation
Get a 48% Discount on MSDN Get a 48% Discount on MSDN Universal Now!Universal Now! For a limited time purchase a 12 month For a limited time purchase a 12 month
MSDN Universal Subscription for MSDN Universal Subscription for $3565+GST (RRP).$3565+GST (RRP).
You will receive updates as they are You will receive updates as they are released for SQL Server, BizTalk Server, released for SQL Server, BizTalk Server, Visual Studio, Exchange Server and Visual Studio, Exchange Server and Windows Server. Windows Server.
You will also receive early access to beta You will also receive early access to beta products such as Windows Vista and products such as Windows Vista and Office 12.Office 12.
Get in now so that when Visual Studio Get in now so that when Visual Studio Team System ships you will be upgraded Team System ships you will be upgraded at no cost to one of the new top tier at no cost to one of the new top tier subscriptions:subscriptions:
Visual Studio 2005 Team Edition for Visual Studio 2005 Team Edition for Software DevelopersSoftware DevelopersVisual Studio 2005 Team Edition for Visual Studio 2005 Team Edition for Software ArchitectsSoftware ArchitectsVisual Studio 2005 Team Edition for Visual Studio 2005 Team Edition for Software TestersSoftware Testers
For more details and to find your local For more details and to find your local reseller visit: reseller visit: www.microsoft.co.nz/buyMSDNwww.microsoft.co.nz/buyMSDN