超时异常问题。
背景:Visual Studio 2010
问题描述:我偶尔(大约每天一次,程序每小时运行一次)会遇到一个异常,我不太理解。
异常信息如下:
Exception message : The transaction has aborted.
Exception Source : System.Transactions
Exception Trace : at System.Transactions.TransactionStateAborted.BeginCommit(InternalTransaction tx, Boolean asyncCommit, AsyncCallback asyncCallback, Object asyncState)
at System.Transactions.CommittableTransaction.Commit()
at System.Transactions.TransactionScope.InternalDispose()
at System.Transactions.TransactionScope.Dispose()
at JDTranslation_K4_ReadEmails.Form1.ReadEmailDetails(EmailMessage emMessage, String strUserName, String strAccountName)
Exception Target : Void BeginCommit(System.Transactions.InternalTransaction, Boolean, System.AsyncCallback, System.Object)
异常通常在持续了25-30分钟的事务之后发生。
我已将我的事务设置为持续5小时(见下文),因此我完全不明白为什么它会超时。
我有些担心:我知道一些数据已经提交到数据库中-我能相信它们都被提交了吗? (几乎不可能通过调查数据来验证。)
程序(部分功能)大致如下:
//INSTANTIATE THE TRANSACTION SCOPE
TransactionOptions option = new TransactionOptions();
//SET THE ISOLATION LEVEL AND THE TIMEOUT DURATION
option.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
option.Timeout = new TimeSpan(5, 0, 0);
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, option))
{
try{
// do some select/update/insert/delete stuff on both connections,
// up to perhaps 3600 actions
// some of it happens in static functions declared elsewhere in the same class scope
} // this is the line mentioned in the Exception
}
我很乐意回答一些问题:
异常为什么会发生?
我能否相信所有数据已经提交,还是C#.NET中存在真正的错误导致了部分提交?
我的代码是否正确,或者应该重新组织代码以解决这个问题?
请帮帮我!