0

I am creating a Windows Service that basically runs every 24 hours gets data from files on the computer and puts the data into a database.

There are 27 files, 5 of which run one after another and then other 22 get spawned into new threads and ran at the same time.

The issue I am currently facing is that at the end of my using statement for the TransactionScope i call Complete() which run through fine, but as soon as the end of the using statement is hit (The final bracket) a TransactionAbortedException gets thrown.

This is how my code is setup:

using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew, TimeSpan.FromHours(1)))
{
    using (RgsDb2.DbConnection pDbConn = mComms.DbConnection(mConnectionString))
    {
        if (FirstFile(mCurrentImportDate, pDbConn))
        {          
            if (SecondFile(mCurrentImportDate, pDbConn))
            {            
                if (ThirdFile(mCurrentImportDate, pDbConn))
                {            
                    if (FourthFile(mCurrentImportDate, pDbConn))
                    {            
                        if (FifthFile(mCurrentImportDate, pDbConn))
                        {
                            //LOOPS THROUGH THE LIST AND CREATES A NEW THREAD FOR THE FILE AND ADDS THAT TO A LIST.
                            foreach (string pFileThread in pThreadArray)
                            {
                                ProcessFilesThread pFileImportThread = new ProcessFilesThread(mConfiguration, pFileThread, mCurrentImportDate, ref pThreadList, ref mImportResults, mConnectionString, mFiles);
                                mActiveThreads.Add(pFileImportThread);
                             }

                             //STARTS EACH THREAD IN THE LIST.
                             mActiveThreads.ForEach(a => a.Start());
                        }
                    }
                }
            }
        }
    }
    scope.Complete();
}

For the first 5 files they use pDbConn which is in the using statement, but because the 22 files will be running at the same time each of the 22 other files have there own connection.

Can anyone see a reason why a TransactionAbortedException is getting thrown?

Ben Clarke
  • 1,051
  • 4
  • 21
  • 47

0 Answers0