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:
- Atomic: Either everything should commit or nothing.
- Consistency: System should remain in consistent.
- Isolation: Resource sharing not allowed. Or it should not be visible to the outside world.
- Durable: State should be maintained regardless of any failure or loss.
How to enable Transaction in WCF:
Step 1:
- [ServiceContract]
- public interface ITransaction
- {
- [OperationContract]
- [TransactionFlow(TransactionFlowOption.Allowed)]
- bool Dosomething();
- }
Step 2: - public class WCFTransaction: ITransaction
- {
- [OperationBehavior(TransactionScopeRequired = true)]
- public bool Dosomething()
- {
- using(SqlTransaction transaction = Program.dbConnection.BeginTransaction())
- {
- Boolean doRollback = false;
- using(SqlCommand cmd = new SqlCommand("Query", objConnection))
- try
- {
- cmd.ExecuteNonQuery();
- }
- catch (SqlException)
- {
- doRollback = true;
- break;
- }
- }
- if (doRollback) transaction.Rollback();
- else transaction.Commit();
- return true;
- }
- return true;
- }
- }
Step 3: - <services>
- <service name="Service">
- <endpoint address="" binding="wsHttpBinding" contract="WCFTransactionAndIsolation.ITransaction" bindingConfiguration="TransactionBinding" /> </service>
- </services>
- <bindings>
- <wsHttpBinding>
- <binding name="TransactionBinding" transactionFlow="true"></binding>
- </wsHttpBinding>
- </bindings>
Now @Client turns: how to call it from the client:
- try
- {
- using(TransactionScope scope = new TransactionScope())
- {
- MyserviceClient client = new MyserviceClient();
- client.upload();
- client.upload();
- scope.Complete();
- MessageBox.Show("Cheers…");
- }
- }
- catch
- {
- MessageBox.Show("My Error………………");
- }
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: - [ServiceBehavior(TransactionIsolationLevel = IsolationLevel.ReadCommitted)]
- public class WCFTransaction: ITransaction
- {
- [OperationBehavior(TransactionScopeRequired = true)]
- public bool Dosomething()
- {
- using(TransactionScope scope = new TransactionScope())
- {}
- return true;
- }
- }
Step 2: At client side
- WcfTransaction.TransactionClient _objTransactionClient = new WcfTransaction.TransactionClient();
- TransactionOptions _tsrnsOption = new TransactionOptions();
- _tsrnsOption.IsolationLevel = IsolationLevel.ReadCommitted;
- using(TransactionScope _objTransactionScope = new TransactionScope())
- {
- try
- {
- _objTransactionClient.Dosomething();
- _objTransactionScope.Complete();
- }
- catch (Exception ex)
- {}
- }
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