I use a TransactionScope in a thread to dump data in my SQL Server database.
using (TransactionScope scope = new TransactionScope())
{
// Dump data in database
scope.Complete();
}
The transaction is a long transaction (~40 seconds) because the large amount of data. This is normal.
When I do an Abort() to stop the thread during this transaction, SQL Server seems to be locked for a few minutes.
What is happening and how can I avoid this?
Simply said: you can not.
No. SQL Server is busy undoing your transaction. It is optimized fully to COMMIT transactions, not to undo them. Undoing LARGE updates is a SLOW operation. Depending on your server layout and the amount of data it can take some minutes or even hours.
All you can do is… not rollback / abort. Or wait.