Transaction And Isolation Levels In WCF

My last article discusssed about WCF security. Now let us talk about transactions, isolation levels and there implementation.

Transactions

It is a logical unit and having set of operations that need either all success or all fail. There should be nothing in between state for a Transaction.

Let us say salary credited to your account. This transaction is having two activity i.e. your account will get ++ (with your salary amount) and your company account will get -- (with your salary amount).

Now both activities should get performed all together otherwise everything will be in trouble.

Again let us say companies account got credited with your salary account but forget to deposit your salary in your account.

Gone case how we will pay rent, credit card bills, etc.

Transaction Properties

The four properties followed by a WCF transaction are the following:

  1. Atomic: Either everything should commit or nothing.
  2. Consistency: System should remain in consistent.
  3. Isolation: Resource sharing not allowed. Or it should not be visible to the outside world.
  4. Durable: State should be maintained regardless of any failure or loss.

How to enable Transaction in WCF:

Step 1:

  1. [ServiceContract]  
  2. public interface ITransaction  
  3. {  
  4.     [OperationContract]  
  5.     [TransactionFlow(TransactionFlowOption.Allowed)] //Step1  
  6.     bool Dosomething();  
  7. }  
Step 2:
  1. public class WCFTransaction: ITransaction  
  2. {  
  3.     [OperationBehavior(TransactionScopeRequired = true)] //Step 2  
  4.     public bool Dosomething()  
  5.     {  
  6.         using(SqlTransaction transaction = Program.dbConnection.BeginTransaction())  
  7.         {  
  8.             Boolean doRollback = false;  
  9.             using(SqlCommand cmd = new SqlCommand("Query", objConnection))  
  10.             try  
  11.             {  
  12.                 cmd.ExecuteNonQuery();  
  13.             }  
  14.             catch (SqlException)  
  15.             {  
  16.                 doRollback = true;  
  17.                 break;  
  18.             }  
  19.         }  
  20.         if (doRollback) transaction.Rollback();  
  21.         else transaction.Commit();  
  22.         return true;  
  23.     }  
  24.     return true;  
  25. }  
  26. }  
Step 3:
  1. <services>  
  2.     <service name="Service">  
  3.         <endpoint address="" binding="wsHttpBinding" contract="WCFTransactionAndIsolation.ITransaction" bindingConfiguration="TransactionBinding" /> </service>  
  4. </services>  
  5. <bindings>  
  6.     <wsHttpBinding>  
  7.         <binding name="TransactionBinding" transactionFlow="true"></binding>  
  8.     </wsHttpBinding>  
  9. </bindings>  
Now @Client turns: how to call it from the client:
  1. try  
  2. {  
  3.     using(TransactionScope scope = new TransactionScope())  
  4.     {  
  5.         MyserviceClient client = new MyserviceClient();  
  6.         client.upload();  
  7.         client.upload();  
  8.         scope.Complete();  
  9.         MessageBox.Show("Cheers…");  
  10.     }  
  11. }  
  12. catch  
  13. {  
  14.     MessageBox.Show("My Error………………");  
  15. }  
Isolation Level

Question

What is the need of Isolation Level: Indeed it is necessary, let say we have two transaction: In one you are updating the records in table X and at the very same time you want to select or do some other operation on the same records in the table X .

Step 1:
  1. [ServiceBehavior(TransactionIsolationLevel = IsolationLevel.ReadCommitted)]  
  2. public class WCFTransaction: ITransaction  
  3. {  
  4.     [OperationBehavior(TransactionScopeRequired = true)] //Step 2  
  5.     public bool Dosomething()  
  6.     {  
  7.         using(TransactionScope scope = new TransactionScope())  
  8.         {}  
  9.         return true;  
  10.     }  
  11. }  
Step 2: At client side
  1. WcfTransaction.TransactionClient _objTransactionClient = new WcfTransaction.TransactionClient();  
  2. TransactionOptions _tsrnsOption = new TransactionOptions();  
  3. _tsrnsOption.IsolationLevel = IsolationLevel.ReadCommitted;  
  4. using(TransactionScope _objTransactionScope = new TransactionScope())  
  5. {  
  6.     try  
  7.     {  
  8.         _objTransactionClient.Dosomething();  
  9.         _objTransactionScope.Complete();  
  10.     }  
  11.     catch (Exception ex)  
  12.     {}  
  13. }  
We have the following isolation levels:
  • Read Uncommitted: Also known as Dirty isolation level. It makes sure that corrupt data cannot be read. This is the lowest isolation level.

  • Read Committed: It ensures not to read the data that has been changed by any other application and is not yet committed. It is the default level.

  • Repeatable Read: It stops the usage of dirt read and non-repeatable read. It states the data fetched through a query will be locked and will not be updated by any other transaction.

  • Serializable: It does not allow any modification and addition of new data till the transaction is completed. This is considered to be a restrictive level.

  • Snapshot: It raises error on modifying a data that has already been changed by any transaction.

Reference


Similar Articles