Transactions
· A transaction is a collection or group of one or more units of operation executed as a whole.
· It provides way to logically group single piece of work and execute them as a single unit.
· In addition, WCF allows client applications to create transactions and to propagate transactions across service boundaries.
Recovery Challenge
· Consider a system maintained in consistent state, when application fail to perform particular operation, you should recover from it and place the system in the consistent state.
· While doing singe operation, there will be multiple atomic sub operation will happen. These operations might success or fail. We are not considering about sub operation which are failed. We mainly consider about the success operation. Because we have to recover all these state to its previous consistence state.
· Productivity penalty has to be payee for all effort required for handcrafting the recovery logic
· Performance will be decreased because you need to execute huge amount of code.
Solution
Best way to maintain system consistence and handling error-recovery challenge is to use transactions
· Committed transaction: Transaction that execute successfully and transfer the system from consistence state A to B.
· Aborted transaction: Transaction encounters an error and rollback to Consistence State A from intermediate state.
In-doubt transaction: Transactions fail to either in commit or abort.
Transaction Properties
Transaction can be said as pure and successful only if meets four characteristics.
1. Atomic– Either all of the tasks of transactions are performed or none of them are.
2. Consistent - transaction must leave the system in consistent state.
3. Isolated - Resources participating in the transaction should be locked and it should not be access by other third party.
4. Durable - Durable transactions must survive failures.
Transaction PropagationWe can specify whether or not client transaction is propagated to service by changing Binding and operational contract configuration
<bindings>
<netTcpBinding>
<binding transactionFlow="true">
</binding>
</netTcpBinding>
</bindings>
Even after enabling transaction flow does not mean that the service wants to use the client’s transaction in every operation. We need to specify the “TransactionFlowAttribute” in operational contract to enable transaction flow.
[ServiceContract] public interface Iservice
{ [OperationContract]
[TransactionFlow(TransactionFlowOption.Allowed)]
int Add(int a, int b);
[OperationContract]
int Subtract(int a, int b); }
Note: TransactionFlow can be enabled only at the operation level not at the service level
Transaction Attributes in System.ServiceModel
· Namespace (System.Transactions) that makes transaction programming easy and efficient.
ServiceBehavior Attribute
· TransactionAutoCompleteOnSessionClose: Specifies whether pending transactions are completed when the current session closes.
· TransactionIsolationLevel: Determines the isolation level of the transaction.
· TransactionTimeout: Specifies the period in which a transaction has to complete.
OperationBehavior Attribute
· TransactionAutoComplete: Specifies that transactions will be auto-completed if no exceptions occur.
· TransactionScopeRequired: Specifies whether the associate method requires a transaction.
TransactionFlow Attribute
· Allowed: Transaction may be flowed.
· Mandatory: Transaction must be flowed.
· NotAllowed: Transaction cannot be flowed.
Transaction Protocols
WCF itself will take care of what kind of transaction protocols should be used for different situation. Basically there are three different kinds of transaction protocols used by WCF.