none
DbTransaction is not released after Thread.Abort() RRS feed

  • Question

  •  

    Hi All,

    Recently I found a problem with my multi-thread application, I have one thread excecuting the following code block:

    Code Block

    SomeMethod

    {

    DbConnection conn = DBManager.CreateConnection();

    using (DbTransaction transaction = conn.BeginTransaction())

    {

    try

    {

    //Do some job inserting data into DB with the transaction

    transaction.Commit();

    }

    catch (ThreadAbortException)

    {

    transaction.RollBack();

    conn.Close();

    }

    Catch (Exception ex)

    {

    // ...

    }

    }

    }

     

    And sometimes I need to stop the thread by using Thread.Abort() method. What's confusing me is even I put rollback action in the catch block , the transaction is not closed when I check the Activity Monitor of SQL Server. In the monitor it shows that the current transaction is still alive.

    When I put trace before and after the rollback action, it shows the rollback action was excecuted, but in Database it's still in open status.

    As the ThreadAbortException was raised, if at that time the program was inserting a batch of data (before commit), in the catch block the call stack was changed, the excecution context seems to have been changed so any actions like rollback didn't work.

    I did this because for some reason I want to stop the working thread immediately and I want the pending transactions rolled back as well. In my application ( it's a windows service application) , I have one main thread with several working threads, and I want to realize the "Pause" to the service and I'd like to force the working threads to be stopped imediately when I call the "Pause".

    Unfortunately that doesn't work unless I stop the service so the whole context is stopped and then that transaction rolled back. The problem is that the excecution context is changed when it is in the Catch Block, and how can I retrieve the original context.

    Anybody who can help me with this? Thank you.

    Thursday, December 27, 2007 6:56 AM

All replies