0

I have a problem which make me turn around for 3 days. I'm using transaction scope in this transactioscope I insert say 5 values in 5 tables. The insertion goes correctly for the first 3 tables and totally ignore the 4th insertion and insert the 5th value correctly without any problem. No exception appears and no rollback occurs.

EDIT: This problem only occurs on the production server and it's not regularly occur. In few times it occur and the most times it works correctly without any problems.

Note: this problem started to be appeared after i host another application on the same server.

public void InsertStuff()
{
    try
    {
        using(TransactionScope ts = new TransactionScope())
        {
            //perform insert 1
            Tablel1.Insert();

            //perform insert 2
            Tablel2.Insert();

            //perform insert 3 - 
            Tablel3.Insert();

            //perform insert 4 - No insertion occur !!!!!
            Tablel4.Insert();

            //perform insert 5 - insertion works fine!!!!!
            Tablel5.Insert();


            ts.Complete();            
        }
    }
    catch(Exception ex)
    {
        throw ex;
    }
}
Blachshma
  • 17,097
  • 4
  • 58
  • 72
user1194842
  • 71
  • 3
  • 14
  • 1
    unlikely this can be answered as it stands. – Mitch Wheat Dec 17 '12 at 07:05
  • [Have you tried](http://mattgemmell.com/2008/12/08/what-have-you-tried/) to swap last two table inserts? What's the result? Do you have any foreign keys in that tables? – AgentFire Dec 17 '12 at 07:09
  • I couldn't swap two tables because each one is related with the previous one .. i mean after insertion I get the record ID to be foreign key in the other table – user1194842 Dec 17 '12 at 07:16
  • 2
    Please use `throw;` to rethrow or better, do not add a `catch` if you are not doing anything. This way you are losing/hiding the stacktrace. – Emond Dec 17 '12 at 07:41
  • post the code in Tablel4.Insert(); , the problem is most likely in your insert code. – Peter Dec 17 '12 at 09:11

2 Answers2

0

If an exception does occur then the transaction scope should rollback.

Why don't you test your theory by throwing an exception yourself, or by explicitly calling a ts.Rollback() in the catch.

dutzu
  • 3,883
  • 13
  • 19
  • it doesn't go threw the catch because it does not feel that there was an error and the insertion does not occur.. – user1194842 Dec 17 '12 at 07:26
  • that can't be right. So the insertion doesn't throw an exception, then it isn't an exception. Then what you can do, is to manually check that the inserts were succesful and if any of them failed call ts.Rollback() manually. – dutzu Dec 17 '12 at 07:27
  • EDIT: this problem only occur on the production server and it's not regularly occur.. in few times it occur and the most times it works correctly without any problems .. Note: this problem started to be appeared after i host another application on the same server. – user1194842 Dec 17 '12 at 07:36
  • you need to dig into the issue more. To see why the insert doesn't happen. If there was a problem inserting the data, then an exception would be thrown. Otherwise something else is wrong and you need to figure it out and complete your question with the missing details. – dutzu Dec 17 '12 at 07:48
0

It does not looks anything wrong with the transaction or program flow. Check whatever is programmed for

Tablel4.Insert();

There is something magical that program/computer will do by self. we often make mistakes but do not notice. We can suggest the possibilities but its you who can double check your logic or post that code segment if want other to take a look.

Exception does not occur because the there could be a logical mistake instead some compilation or syntax

Munawar
  • 2,588
  • 2
  • 26
  • 29