如果 machine.config 中没有 "system.transactions" 元素,那么 maxTimeout 的默认值是多少(见示例)?
<system.transactions>
<machineSettings maxTimeout="??:??:??" />
</system.transactions>
我之所以提出这个问题是因为代码崩溃了,原因是发生了以下异常,并且似乎与事务超时有关,在SaveChanges方法执行期间程序崩溃了,我收到的异常信息如下:
The transaction associated with the current connection has completed
but has not been disposed. The transaction must be disposed before
the connection can be used to execute SQL statements.
这是导致程序崩溃的代码片段:
using (TransactionScope transaction = TransactionHelper.CreateTransactionScope())
{
using (EFContext efDBContext = new EFContext())
{
try
{
efDBContext.Connection.Open();
foreach (MyEntity currentEntity in myEntities)
{
//Insertion
}
transaction.Complete();
}
catch (Exception ex)
{
//Inspect current entity
//Get Total Time of the run
//Number of entities processed
}
finally
{
if (esfDBContext.Connection.State == ConnectionState.Open)
esfDBContext.Connection.Close();
}
}
}
这是我创建TransactionScope的方法:
public static TransactionScope CreateTransactionScope(TransactionScopeOption option = TransactionScopeOption.Required, IsolationLevel isolationLevel = IsolationLevel.ReadCommitted)
{
var transactionOptions = new TransactionOptions()
{
Timeout = TimeSpan.MaxValue,
IsolationLevel = isolationLevel
};
return new TransactionScope(option, transactionOptions);
}
TransactionScope时,请考虑使用TransactionManager.MaximumTimeout而不是TimeSpan.MaxValue(请参见https://stackoverflow.com/a/21935333/7665486)。 - Oliver Schimmer