Friday, July 20, 2012

Transaction got aborted ,WHY?

Transaction Exception: The operation is not valid for the state of the transaction.


There may be different reasons for transaction exception in distributed transaction when we are using TransactionScope in our code :-

1. Transaction may get timed out and can cause this type of exception.So, when facing this type of exception in our code we should first check whether the transaction can be completed in specified time or not. If not then we should increase the transaction time .
Options to increase the transaction time out can be easily find out by going to msdn sites.

2. Majority of time we got this type of exception when we are using TransactionScope and inside that TransactionScope ,we are opening some sql connection and executing some sql query.
 using (TransactionScope Scope = new TransactionScope(TransactionScopeOption.RequiresNew))
    {
        using(SQLServer Sql = new SQLServer(this.m_connstring))
        {
            //code for sql query .
        }
    }

There may be couple of reason to get this type of error in such situations:-
  • Whenever we are calling some sql procedure and in sql procedure we might be using transaction due to which when any exception occurs in procedure then transaction gets rolled back and when we try to do other stuff inside the transaction Scope then we will get this error as transaction is already rolled back and we are trying to do operation using that invalid transaction.
We might not be able to find out the cause of this error on code side when exception occurs in database side whenever we use sqlreader in our code in try ,catch block (and in catch block we are eating exception).
So, beware and try to lookout for SQL reader in code when you face any such exception and then check stored procedure whether its throwing any exception or not.

You might also look for transaction log,which system  generates for any distributed transaction.Since its not in scope of this post ,I am not talking about that in this post.

I hope this post can be helpful to you guys when you face this type of transaction exception.


Sunday, March 11, 2012

Sockets overview

Sockets:Use it or Not 
Many of us have listened about socket  in our programming language but we may not have given enough attention to it.
Some basic questions comes in mind when we listen about socket :-
What is socket? Why should I use socket ? What are  the benefits of using socket ? Should I use socket or not in my coding ? Is there other alternatives to socket programming ? What are the other ways ,using which I can do coding  without need of using sockets?  And One important question, I haven't used sockets till yet in my programming career,so why should i bother of using it or pay attention to understand the process of socket communication?


These are some of questions which came to my mind also, when i first started doing some programming using sockets.So,I started doing research about this topic and found it very interesting . It is basically the base of  network communication in our programming world.

The socket concept is not specific to any programming language, it is basically same concept in c#,java,c++ and other known programming languages for network communication.

Here in this post ,I will talk about sockets in c#.
So ,
Q -What is socket?
Ans-Sockets is a method for communication between a client program and a server program in a network. A socket is defined as "the endpoint in a connection." Sockets are created and used with a set of programming requests or "function calls" sometimes called the sockets application programming interface (API). The most common sockets API is the Berkeley  interface for sockets. Sockets can also be used for communication between processes within the same computer.


That's the answer I found from a site which I think will give you a good definition to understand sockets and its use. Also ,refer to link for sockets in .net for further understanding of socket APIs in .net.

For other above questions answers, I would say we have WCF  in .net for network communication.Internally WCF uses sockets for network communication. So, we can say WCF is just a wrapper on socket programming  which gives us many more  facilities than network communication e.g security , reliability and other options when doing communication between services and clients.

Sending  and Receiving data on network (Communication)
Before sending data using sockets on network we need to convert data into streams.Here comes the other concepts serialization and deserialization in our programming language to convert data into stream and converted stream back into data objects. FYI, In WCF we have DataContractSerialization and in web services we use normally XmlSerialization. I will not go further into serialization and deserialization. You can easily found topics on serialization and deserialization by searching on Google but I am sharing msdn link on serialization and deserialization in .net.

So,our purpose to know what is socket and why should we use it.. is almost complete.
For more information on sockets, how they communicate and what type of data can be passed from one socket to another ,please refer the link.

Next.. in this socket series , I will post  a simple example of web server using socket... (And also a simple comparison  of our web server code with IIS.. web server.)


Please share your views on this topic and let me know .. what more you think .. i should add in this socket series.