Table adapter update in transaction scope keeps timing out
So I have a typed dataset that I have created records for from another database (Over 500,000 records!). I need to import all of these records into the other database and have the following code (minus initialize, adding rows etc):
try
{
Console.WriteLine("Time to process of adding to table: Start at " + startDate.ToString() + " | End at " + DateTime.Now.ToString());
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew, new TimeSpan(1, 0, 0)))
{
laborTicketTableAdapter.Update(laborTicket);
ts.Complete();
}
Console.WriteLine("Time to process transaction: Start at " + startDate.ToString() + " | End at " + DateTime.Now.ToString());
}
catch (SqlException ex)
{
MessageB开发者_如何学Pythonox.Show("Something bad happened" + Environment.NewLine + ex.StackTrace);
result = false;
}
catch (Exception ex)
{
MessageBox.Show("Something bad happened" + Environment.NewLine + ex.StackTrace);
result = false;
}
The console outputs the following:
Time to process of adding to table: Start at 1/1/2009 2:05:59 PM | End at 5/18/2011 2:06:30 PM
The thread '<No Name>' (0xa5c) has exited with code 0 (0x0).
The thread '<No Name>' (0x2e4) has exited with code 0 (0x0).
The thread '<No Name>' (0xae0) has exited with code 0 (0x0).
A first chance exception of type 'System.Transactions.TransactionAbortedException' occurred in System.Transactions.dll
It throws an exception, with the inner exception of "Transaction timeout". The transaction has expired so I set the timeout to one hour and it still times out after under 15 minutes. What's going on?
Final time of processing was 2:19:40PM, it took around 15 minutes to run. How come I am getting this exception? And I see three threads on the console but this is a blank slate in an otherwise empty project. All other connections are closed to the external data sources and all that is left when I do the update is that table adapter and table object.
-- EDIT -- Machine.config does not have any settings for the maxTimeout attribute
There can be all kinds of reasons for the exception. Maybe your actual SQL connection had a timeout? this might happen when your table needs to reorganize (just speculating here)
As a sidenode, have you taken a look at SqlBulkCopy? ( http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx ) ->> Lets you efficiently bulk load a SQL Server table with data from another source.
In my experience this is blazingly fast.
精彩评论