blog

Home / DeveloperSection / Blogs / Transaction in WCF

Transaction in WCF

priyanka kushwaha2570 19-Mar-2015

In this post, I’m explaining transaction in WCF.


What is a transaction in WCF?

The process contains multiple organizations and workflows involved. The short running transactions are operations related to business that is completed in few seconds. It contains some external dependencies.  These scenarios are known as a transaction.

The behavior of the transactions is known as ACID transactions. The ACID transactions are as explained below:

1.      Atomic: In atomic transactions, all the updates within the transaction are successful or are rolled back. No partial updates are allowed.

2.      Consistent: After all the operations are completed, the data is validated to the business rules.

3.      Isolated: When the user is executing an operation, no partial results can be viewed outside the transaction.

4.      Durable:  When the transaction is committed, the data is persisted to survive from the failures

 

Transaction type in WCF are:

1.      Light weight

2.      OLE transaction

3.      WS-Atomic transaction,

 

Lightweight protocol: Under this protocol, transaction must be within a single AppDomain. There should not be any across calls for other AppDomains, so logically no client-service calls are allowed.

 

OLE transaction: The OLE transaction protocol is the standard for use with distributed transaction. This protocol works within the window environment through Remote procedure calls. Under this protocol communication through firewalls is now allowed and cross-platform

Communications are also not allowed.

 

WS-Atomic transaction: WS-Atomic protocol is an industry standard that can be used over Http.

OLE is limited to computers running windows behind a firewall.

 

Transaction Attributes in System.ServiceModel

  • ServiceBehavior Attribute
  • OperationBehavior Attribute
  • Transaction Attribute

 

ServiceBehavior Attribute: The [ServiceBehavior] attribute has properties that deal with handling and managing transactions.

  • TransactionAutoCompleted: Specifies whether pending transactions are completed when the current session closed.
  • TransactionIsolationLevel: Determines the isolation level of the transaction.
  • TransactionTimeout: Specifies the period in which a transaction has to complete.
Example:
  [ServiceBehavior(TransactionAutoCompleteOnSessionClose = true, TransactionTimeout = "00:00:30", TransactionIsolationLevel=IsolationLevel.ReadCommitted)]
    publicclassService2 : IService2
    {
      publicint InsertEmployee(Employee emp)
        {
           
        }
        publicint DeleteEmployee(string EmpNo)
        {
        }
        publicEmployee[] GetAllEmployee()
        {
        }
 
        }
        #endregion
    }
}
 

OperationBehavior Attribute:

The [OperationBehavior] also has a coupled of properties related to transactions. The two properties are:


TransactionAutoComplete: Specifies that transactions will be auto-completed if no eTransactionScopeRequired: Specifies whether the associate method requires a transaction.

Example:
  [ServiceBehavior(TransactionAutoCompleteOnSessionClose = true, TransactionTimeout = "00:00:30", TransactionIsolationLevel=IsolationLevel.ReadCommitted)]
    publicclassService2 : IService2
    {
[OperationBehavior(TransactionAutoComplete=true,TransactionScopeRequired=true)]     
[TransactionFlow(TransactionFlowOption.NotAllowed)]
  publicint InsertEmployee(Employee emp)
      {
           
      }
      publicint DeleteEmployee(string EmpNo)
      {
      }
      publicEmployee[] GetAllEmployee()
      {
      } 
      
      #endregion
    }
}

 

TransactionFlowAttribute

The [TransactionFlow attribute] attribute is used to specify the level at which a service operation can accept a transaction header. This attribute has a single property and is the attribute used to annotate a service operation method.

Allowed:Transaction may be flowed.

Mandatory:Transaction must be flowed.

NotAllowed: Transaction cannot be flowed.


 [TransactionFlow(TransactionFlowOption.NotAllowed)]
  publicint InsertEmployee(Employee emp)
  {
           
  }

 


Updated 21-Feb-2018

Leave Comment

Comments

Liked By