Thursday 2 August 2012

WCF : Transactions



What is a transaction in WCF?

  • 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.
What are various types of transaction in WCF?



  • 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
Which protocol is used to handle transactions in WCF?

  • WCF uses WS-Atomic protocol to managed transaction across WCF services. So you can have different WCF services hosted on different computers and they all can run under one transaction unit. 
  • In WS-Atomic protocol one transaction you can have heterogeneous WCF services developed in different platform. In other words you can have JAVA and .NET web services running under one transaction.
What are the two different kinds of phases in WCF transactions?

WCF transactions follow 2 phase commit. So there are 2 phases
  •  prepare phase 
  •  commit phase. 
  • All co-ordination of transactions is done by the transaction manager. 
In prepare phase the transaction manager checks whether all entities are prepared to commit. In commit phase the actual commit starts. 

Where does the transaction manager reside?
Transaction manager resides at the client computer who initiates the transaction.


What the different transaction options ?
In the previous code we have use 'TransactionFlowOption'. We can specify transaction in 3 ways in WCF:- 
  • TransactionFlowOption.NotAllowed
    This is a default option. Using this option no transaction will be propagated across the binding. If any client attempts to call the WCF service in a transaction it will be ignored for this option.  
  • TransactionFlowOption.AllowedThis option specifies that client can call this WCF service in a transaction. It's not compulsory that the service needs to be called in a transaction. You can call without the transaction also. 

  • TransactionFlowOption.MandatoryThis option specifies that client must call the WCF service in a transaction mode. If the WCF service is called without transaction, 'FaultException' will be raised
In WCF, transaction can be propagated across service boundary. This enables service to participate in a client transaction and it includes multiple services in same transaction, Client itself will act as service or client.
We 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 clients  transaction in every operation.
Specify, TranscationFlowAttribute in operational contract to enable transactional flow.

[ServiceContract]
public interface IService
{

    [OperationContract]
    [TransactionFlow(TransactionFlowOption.Allowed)]
    int Add(int a, int b);

    [OperationContract]
    int Subtract(int a, int b);
}


Explain the two phase transaction process.
Let us assume three computers in this example.
  • Computer 1: Client starts transaction
  • Computer 2: Service A is hosted here
  • Computer 3: Service B is hosted here.
  • Computer 1, initiates the transaction; now the first phase ‘prepare’ phase begins.
  • Computer 1, sends messages to Computer2 and Computer3 to check if they are ready.
  • Computer2 and Computer3 respond to the request and indicate they are ready.
  • Computer1 , receives the ready message, phase 1 ends and phase 2 begins.
  • Computer1, issues command to computer 2 and computer 3
  • Computer 2 and computer 3 start execution, once the execution is complete, they commit the transaction and send a message back to Computer 1. This ends the commit phase.
  • In case Computer 2 or Computer 3 are unable to carry out the transaction and the transaction is reverted.

When all the WCF services revert saying they have committed the transaction is marked as successful. 






    No comments:

    Post a Comment