A transaction is a group of query’s to be executed as a single unit. when we execute group of querys as a single unit out any problem then only all the modification will be committed to the database. If any single query is not executed due to any problem then all other modifications also will be rollback. The transactions should follow ACID properties or ACID rules ACID rules stands for, A Atomicity C consistency I Isolation D Durability
This word has been derived from greek, “Atom” means individual
The query after execution should return appropriate results and should not return wrong results.
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
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.
In general, when we implement the transactions in WCF services it follows in 2 stages / 2 phases.
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.
Go to sql server database create database with the name sample create 3 tables with the name customer merchant & transactions like, ID customer ID Balance current balance MID Merchant ID PK Primary Key ID Transaction ID To implement the transaction we need to perform 3 modifications with in the WCF service.
[operation behavior (transaction scope required = true)]
<endpoint address = “ ” binding = “WShttp binding” contract = “interface name” binding configuration = “transactional binding”> <binding name = “Transactional Binding” Transaction flow = “true”/> </WS http binding> </binding> Create a new WCF service with the name “WCF customer service” change the interface name to “Icustomer.cs” Change the class file name ot “clscustomer.cs” change the servciefiel name ot “Cls customer.svc”. Write the following code (go to Icustomer.cs file) [service contract] [Transaction flow (Transaction Flowoption. Allowed)] Void update customer (string ID, double amount); } Go 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 Go 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> create a 2nd service with the name WCF merchant service write the following code. [service contract] public interface Imerchant { [operation contract] [Transaction flow(Transaction flowoption.allowed)] void update Merchant (string Id, double amount); } Go to class file clsmerchant.cs 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(); } } Go to .svc file change the service name to cls merchant Go to web. Config file 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) Using 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
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.