22
www.orbitone.com Raas van Gaverestraat 83 B-9000 GENT, Belgium E-mail [email protected] Website www.orbitone.com Tel. +32 9 265 74 20 Fax +32 9 265 74 10 VAT BE 456.457.353 Bank 442-7059001-50 (KBC) 27 April, 2009 WCF Transactions by Tom Pester

WCF Transactions

Embed Size (px)

DESCRIPTION

In-depth presentation about Transactions in WCF

Citation preview

Page 1: WCF Transactions

www.orbitone.com

Raas van Gaverestraat 83B-9000 GENT, Belgium E-mail [email protected] Website www.orbitone.com

Tel. +32 9 265 74 20Fax +32 9 265 74 10VAT BE 456.457.353Bank 442-7059001-50 (KBC)

27 April, 2009 WCF Transactionsby Tom Pester

Page 2: WCF Transactions

27 April, 2009WCF Transactions, by Tom Pester2

ACID

Transactions must have the ACID properties. ACID stands for:

o Atomico Consistento Isolatedo Durable

Page 3: WCF Transactions

27 April, 2009WCF Transactions, by Tom Pester3

Two-phase commit protocol

Phase 1The coordinator asks each resource manager to prepare to commit.Each resource manager responds (votes) to commit or abort the transaction.

The coordinator collects all votes and makes a decision to commit or abort the entire transaction.

Page 4: WCF Transactions

27 April, 2009WCF Transactions, by Tom Pester4

Two-phase commit protocol

Phase 2The coordinator asks each resource manager to commit or abort based on this decision.

If the resource manager is asked to commit, it acknowledges completion of the activity. If asked to abort, it rolls back the activity.

The coordinator waits for acknowledgment from all resource managers that the transaction was successfully committed.

Page 5: WCF Transactions

27 April, 2009WCF Transactions, by Tom Pester5

TransactionScope

using (TransactionScope scope = new TransactionScope( )){ Operation1( ); Operation2( ); scope.Complete( );}

Page 6: WCF Transactions

27 April, 2009WCF Transactions, by Tom Pester6

Lightweight Transaction Manager

Any number of volatile resource managers can be enlisted.

Only a single durable resource manager can be enlisted.

No application domain or process boundaries can be crossed.

Page 7: WCF Transactions

27 April, 2009WCF Transactions, by Tom Pester7

Kernel Transaction Manager

Windows Vista introduced the Kernel Transaction Manager (KTM) to manage the resource managers associated with its Transactional Registry (TxR) and its transaction filesystem, called Transactional NTFS (TxF).

When the downstream code within a TransactionScope enlists one of these resource managers, the LTM is promoted to KTM automatically.

o Any number of volatile resource managers can be enlisted.

o Only a single durable or kernel resource manager can be enlisted.

o No application domain or process boundaries can be crossed.

Page 8: WCF Transactions

27 April, 2009WCF Transactions, by Tom Pester8

Distributed Transaction Coordinator

o An application, process, or machine boundary is crossed.

o More than one durable resource manager is enlisted

Page 9: WCF Transactions

27 April, 2009WCF Transactions, by Tom Pester9

WS-AtomicTransaction and WS-Coordination protocols

WS-AtomicTransaction (WS-AT) and WS-Coordination (WS-COOR) are interoperable protocols that enable message-based distributed transactions over HTTP and across platform boundaries

Page 10: WCF Transactions

27 April, 2009WCF Transactions, by Tom Pester10

Enabling WS-AT

Page 11: WCF Transactions

27 April, 2009WCF Transactions, by Tom Pester11

Regasm.exe /codebase wsatui.dll

Page 12: WCF Transactions

27 April, 2009WCF Transactions, by Tom Pester12

Transactions and System Tiers

Page 13: WCF Transactions

27 April, 2009WCF Transactions, by Tom Pester13

Configuring WCF Transactions

Transaction flow between clients and services is a function ofothe binding configuration, othe service contract requirements oand the behavior of the service operation being invoked.

Page 14: WCF Transactions

27 April, 2009WCF Transactions, by Tom Pester14

Enabling transaction flow

For a client transaction to flow across a service boundary, the binding must support transaction flow, which immediately limits you to one of these standard bindings: NetNamedPipeBinding

oNetTcpBindingoWSHttpBinding,oWSDualHttpBindingoand WSFederationHttpBinding.

<wsHttpBinding> <binding name="wsHttpTxTransactionFlow="true" /></wsHttpBinding>

Page 15: WCF Transactions

27 April, 2009WCF Transactions, by Tom Pester15

Transaction flow options

The TransactionFlowAttribute oNotAllowedoMandatoryoAllowed

[ServiceContract()]public interface ICountersService{ [OperationContract] [TransactionFlow(TransactionFlowOption.Allowed)] void ResetCounters( );

Page 16: WCF Transactions

27 April, 2009WCF Transactions, by Tom Pester16

Service operations and transactions

Although the TransactionFlowOption does influence the presence of a client transaction, it does not guarantee that a service operation will use the transaction.

In the implementation of a service contract, each operation must opt-in to support transactions. TransactionScopeRequired property of the OperationBehaviorAttribute

[OperationBehavior(TransactionScopeRequired=true)]public void ResetCounters( ){...}

Page 17: WCF Transactions

27 April, 2009WCF Transactions, by Tom Pester17

TransactionScopeRequired

When set to false, which is the default setting, the service will never join a client transaction, nor will it automatically create a new transaction. That means that if the service code requires a transaction, it would have to do so manually using a TransactionScope block.

When set to true, if a client transaction is flowed to the service, the service operation will join that transaction. If a client transaction is not flowed, a new transaction is created for the service operation to execute in.

Page 18: WCF Transactions

27 April, 2009WCF Transactions, by Tom Pester18

DistributedIdentifier

An indicator that the service is participating in the client transaction is when the DistributedIdentifier of the current transaction is set:

Transaction.Current.TransactionInformation.DistributedIdentifier

Page 19: WCF Transactions

27 April, 2009WCF Transactions, by Tom Pester19

TransactionAutoComplete

Another property of the OperationBehaviorAttribute that affects how transactions are handled is the TransactionAutoComplete property. By default, this property is set to true, which means that the consistency bit is set to true automatically if no exceptions are thrown

[OperationBehavior(TransactionScopeRequired=true,TransactionAutoComplete=false)]

Page 20: WCF Transactions

27 April, 2009WCF Transactions, by Tom Pester20

Controlling IsolationLevel

The System.Transactions namespace defines an IsolationLevel enumeration that defines the following isolation options

o Chaoso ReadCommittedo ReadUncommittedo RepeatableReado Serializableo Snapshoto Unspecified

The last of these settings, Unspecified, is the default.

Page 21: WCF Transactions

27 April, 2009WCF Transactions, by Tom Pester21

TransactionOptions

TransactionOptions options = new TransactionOptions( );options.IsolationLevel = IsolationLevel.Serializable;

using (TransactionScope scope = newTransactionScope(TransactionScopeOption.Required, options)){ m_proxy.SetCounter1(int.Parse(this.txtCounter.Text)); m_proxy.SetCounter2(int.Parse(this.txtCounter.Text)); scope.Complete( );}

Page 22: WCF Transactions

www.orbitone.com

22 WCF Transactions, by Tom Pester27 April, 2009