我必須更新一些DataTable,我將它作爲XML發送數據。如果出現問題,我的存儲過程有事務中止操作。但是我必須更新的記錄數量非常大,XML達到35 MB +。它只是在開發中,而且實時數據會更大。任務,TransactionScope和SQL Server存儲過程
爲了處理這個問題,我想通過塊發送數據進行更新,也就是說,我一次會發送數百條記錄的XML。我試圖使用Task
庫來像這樣並行更新數據庫。
var ret=0;
using(var ts = new TransactionScope(TransactionScopeOption.Required,
new TimeSpan(2,0,0))
{
try
{
for(some condition)
{
get chunk of records
generate xml
create new task and call routing to push data to db
var t = create new task and call routing to push data to db
tasks.Add(t);
}
Task.WaitAll(tasks.ToArray());
ts.Complete();
foreach(var t in tasks)
ret += t.Result;
}
catch(Exception ex)
{
//log exception and show user message
}
}
return ret;
但我得到異常事務已被中止。
我需要做什麼來完成單個事務中的更新,因爲如果任何塊無法正確更新,我必須回滾任何更改。
編輯: - 我使用new TransactionScope(TransactionScopeOption.Required,new TimeSpan(2,0,0))
勵科普塞的建議,但即使一個調用數據庫,在2-3秒完成後,仍然收到錯誤System.Transactions.TransactionAbortedException: The transaction has aborted. ---> System.TimeoutException: Transaction Timeout
。
只有一個呼叫
'WaitForAll'不上'Task'的方法 - 你可以把你的真實代碼? –
@Reed Copsey真正的代碼在辦公室:D該方法是'Task.WaitAll'。 – TheVillageIdiot