[英]C#, SQL SP inside transactionScope ends transaction by timing out
With the code: 使用代码:
using (TransactionScope scope = new TransactionScope( TransactionScopeOption.RequiresNew, new System.TimeSpan( 0, 15, 0 ) ))
{
try
{
for (int i=0; i<10000; i++)
{
dataContext.CallSP( i );
}
}
catch (Exception e)
{
log( e );
}
finally
{
scope.Complete();
}
}
If we call CallSP, it may time out. 如果我们致电CallSP,它可能会超时。 If it times out, we get the error when trying to Complete() the transaction.
如果超时,则在尝试完成交易时会收到错误消息。
The transaction has aborted. 交易已中止。 |
| System.Data.SqlClient.SqlException (0x80131904): The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION.
System.Data.SqlClient.SqlException(0x80131904):COMMIT TRANSACTION请求没有相应的BEGIN TRANSACTION。
What is a way to structure this, so that we keep the speedup benefit of running the CallSPs in a transaction, and we don't try to complete it, if the CallSP failed in such a way that the transaction is no longer open for Completing? 有什么方法可以构造这种结构,以便使我们保留在事务中运行CallSP的加速优势,并且如果CallSP失败,导致事务不再开放以供完成,我们就不会尝试完成它?
Rather than calling scope.Complete()
in the finally{}
clause, call it in the try{}
clause. 而不是在
finally{}
子句中调用scope.Complete()
,而在try{}
子句中调用它。
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew, new System.TimeSpan( 0, 15, 0 ) ))
{
try
{
for (int i=0; i<10000; i++)
{
dataContext.CallSP( i );
}
scope.Complete();
}
catch (Exception e)
{
log( e );
}
}
The TransactionScope
will automatically abort if you enter the catch{}
clause and therefore do not call scope.Complete()
before exiting the using{}
clause.. 如果您输入
catch{}
子句,那么TransactionScope
将自动中止,因此在退出using{}
子句之前不要调用scope.Complete()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.