Transactions in WCF

Ratings:
(4)
Views:709
Banner-Img
  • Share this blog:

WCF Transactions Overview

A transaction is a group of query’s to be executed as a single unit. 11when we execute group of querys as a single unit out any problem then only all the modification will be committed to the database. 11If any single query is not executed due to any problem then all other modifications also will be rollback. 11The transactions should follow ACID properties or ACID rules ACID rules stands for, A     11        Atomicity C     11        consistency I      11         Isolation D     11       Durability  

Atomicity

This word has been derived from greek, “Atom” means individual

  • The querys with in the transaction should into be executed separately rather, should be executable as a group only
  • We can’t divide the queries and we can’t execute separately.
  • If we execute the queries, execute as group only all the queries otherwise don’t execute any query

 

Consistency

The query after execution should return appropriate results and should not return wrong results.  

Isolation

Isolation means if multiple transactions are doing executed, each transaction is independent of  there transactions until one transaction is committed, to be data should not be reflected to other transactions  

Durability

The transactions should be able to services after failures i.e. in the middle of the transaction if execution is stopped due to power failures or due to breakdowns after restoring the failure the transaction is able to complete its execution.  

Implementing transactions using WCF services

In general, when we implement the transactions in WCF services it follows in 2 stages / 2 phases.

  1. Ready phase / preparation phase
  2. Execution and commit phase

Step1:- Client sends request to first WCF service whether it is ready to implement transaction cannot. Step2:- WCF service will acknowledge that it is to implement the service. Step3:- it is ready to implement transactions (or) not. Step4:- WCF service 2 will acknowledge implementing the transaction then execution and commit phase will start. Step5:- Client sends requests to execute the query to WCF servcvie1. Step6:- WCF service 1 will execute the query with DB Step7:- WCF service 1 will acknowledge the client Step 8 :- Client will send request to execute the query to WCF service 2. Step9:- WCF service 2 will execute the query to DB. Step 10:- WCF service 2 will acknowledge the client Step 11:-    The transactions will be committed to DB.  

Example to implement transactions using WCF services

Go to sql server database 11create database with the name sample 11create 3 tables with the name customer merchant & transactions like,   Screenshot_76 ID            11customer ID Balance 11 current balance MID        11 Merchant ID PK           11 Primary Key ID           11 Transaction ID   To implement the transaction we need to perform 3 modifications with in the WCF service.

  1. Write the following attribute with in the functions of interface in which we cant to implement transactions. [transaction flow (transaction flow option. Allowed) ]
  2. Write the following attribute in preceding to the functions of the class in which we implement the interface functions.

[operation behavior (transaction scope required = true)]

  1. In Web config file in end point add a new attribute binding configuration like

<endpoint address = “ ” binding =  “WShttp binding” contract = “interface name” binding configuration = “transactional binding”> Screenshot_77 <binding name = “Transactional Binding” Transaction flow = “true”/> </WS http binding> </binding> 11Create a new WCF service with the name “WCF customer service” 11 change the interface name to “Icustomer.cs” 11Change the class file name ot “clscustomer.cs” 11change the servciefiel name ot “Cls customer.svc”. 11Write the following code (go to Icustomer.cs file) [service contract] [Transaction flow (Transaction Flowoption. Allowed)] Void update customer (string ID, double amount); } 11Go to the classfile clscustomer.cs à write the following code. using system.data; using system.data.sqlclient; public class cls customer: I customer [operation behavior (transaction scope required = true)] Public void update customer (string Id, double amount) { sqlconnection con = New sqlconnection (“---“); String s = “update customer set (balance “=+Amount + “where CID = ‘”+ ID + “ ‘ “; Sql command cmd = new sql command (s, con); Con.cpen(); cmd. execute nonquery(); con.close(); Go to .svc file change the service name to cls customer 11Go to web. Config file à write the following code in endpoint. Endpoint address = “ “ binding  = “WShttp binding” contract =”Icustomer” binding configuration = “Transactional binding “ / > After completion of service tag add a new tag with the name bindings à write the following code. <System. Servicemodel> <Services> ….. </Services> <bindings> <WShttp binding> <Binding name = “Transactional binding “ transaction flow = “true”/> </WS http binding> 11create a 2nd service with the name WCF merchant service 11write the following code. [service contract] public interface Imerchant { [operation contract] [Transaction flow(Transaction flowoption.allowed)] void update Merchant (string Id, double amount); } 11Go to  class file clsmerchant.cs 11 write the following code using system data; using system.dat.sqlclient; public class cls merchant: Imerchant { [operation behavior (transaction scoperequired = true)] public void update merchant (string Id, double Amoutn) { sql connection con=new sqlconnection (“----“); string s = “update Merchant set c balance = “ + amont p “where MID = ‘ “ + ID + “ ‘ “ ; sqlcommand cmd = new sqlcommand (s, con); (on. Open (); cmd. Execute non Query (); con.close(); } } 11Go to .svc file change the service name to cls merchant 11Go to web. Config file 11 write the following code in endpoint. <endpoint address = “ “ binding = “ WS httpbinding” contract = “Imerchant” binding configuration = “Transactionalbinding”/> <bindings> <WS http Bninding> <binding name = “Transactional binding” transaction flow = “True”/> </WS Http binding> </Bindings> create a new WCF service with the name WCF Transaction service à write the following code. [service contract] public interface ITransaction { [operation contract] [Transaction Flow (Transaction Flow option Allowed)] void Insert Transaction (String ID1, string Id2, string Id3, date Time D1, double amount1, double amount2, double amount3) 11Using system. Data: using system. Data.sqlclient; public clas cls transaction: I Transaction { [operation behavior (transaction scope required = true) ] Public void insert transaction (string Id1, string id2 string Id3, date time d1, double amount 1, double amount 2, double amount 3) { sql connection con = new sqlconnection (“----“); String  S = “ Insert into transactions values (‘ “ + Id 1 + “ ‘,’ “ + Id2 + “ ‘,’ “ Id 3 + “ ‘ , ‘ “ + D1 ‘’, amount 1 + “ ‘ “ + amount 2 + “ , “ + amount 3 + “) “ ; Sql command cmd = new sqlcommand (s, con); Con.open(); cmd. Execute Nonquery(); con.close(); } }

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox