C# 事务处理处理嵌套的sql事务
我继承了一段代码,该代码正在执行5个不同的存储过程,这些存储过程在后端进行更新,每个存储过程都有自己的事务对象。 我将所有5个sql事务包装在transaction scope对象中,因为它们都需要同时提交或回滚,现在抛出以下错误:C# 事务处理处理嵌套的sql事务,c#,.net,transactions,transactionscope,sqltransaction,C#,.net,Transactions,Transactionscope,Sqltransaction,我继承了一段代码,该代码正在执行5个不同的存储过程,这些存储过程在后端进行更新,每个存储过程都有自己的事务对象。 我将所有5个sql事务包装在transaction scope对象中,因为它们都需要同时提交或回滚,现在抛出以下错误: An exception of type 'System.Transactions.TransactionManagerCommunicationException' occurred in applicationname.dll but was not hand
An exception of type 'System.Transactions.TransactionManagerCommunicationException'
occurred in applicationname.dll but was not handled in user code
Additional information: Network access for Distributed Transaction Manager (MSDTC) has been disabled.
Please enable DTC for network access in the security configuration for
MSDTC using the Component Services Administrative tool.
这是一个函数,在该函数中,一旦代码命中,就会抛出异常:
Protected Overridable Function GetParameters(ByRef inConnection As SqlClient.SqlConnection _
, ByVal inStoredProcedureName As String _
, ByVal inIncludeReturnValue As Boolean) As SqlClient.SqlParameter()
Return SqlHelperParameterCache.GetSpParameterSet(inConnection, inStoredProcedureName, inIncludeReturnValue)
End Function
本文将解释如何解决此问题:
但我不想在不了解问题的情况下启用这些选项。我不能将sqltransactions包装在transactionscope中吗?或者发生此问题是因为它们正在使用连接
--更新1
我们的DBA现在已经启用了MSDTC,它可以在除我调用WCF Web服务之外的所有调用上工作。现在它只会抛出一个超时异常:
The operation has timed out
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Net.WebException: The operation has timed out
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
堆栈跟踪:
[WebException: The operation has timed out]
System.Net.HttpWebRequest.GetResponse() +8420880
System.ServiceModel.Channels.HttpChannelRequest.WaitForReply(TimeSpan timeout) +234
[TimeoutException: The HTTP request to 'http://test/testValidationService/test.svc' has exceeded the allotted timeout of 00:01:00. The time allotted to this operation may have been a portion of a longer timeout.]
System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason) +7074108
System.ServiceModel.Channels.HttpChannelRequest.WaitForReply(TimeSpan timeout) +16650152
System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout) +388
[TimeoutException: The request channel timed out while waiting for a reply after 00:00:59.9843749. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout.]
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +14579646
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +622
更新2
超时似乎是由锁引起的。因此,我在transactionscope中有一个事务,我认为该事务现在将被忽略,因为transactionscope是包装器,但显然我仍然必须提交或回滚单个事务,这似乎已经解决了问题。从我读到的内容来看,单个提交/回滚将被忽略,就像transactionscope最后说的那样?是这样吗?有人能解释一下吗?您使用的是哪个SQL Server版本 在SQL server 2005及更早版本中:
我正在使用SQL server 2008 R2。我们的dba拒绝启用指定的选项。是否存在任何安全风险?这就是确保一次只打开一个Sql连接的要点,这样您就不需要启用MSTDC。您的dba是对的,因为这不是分布式事务的情况,您应该努力防止DTC升级。我只打开了一个连接。有些事情很奇怪。超时可能完全有效。也许你在服务中所做的工作需要1分钟以上,因为在查看错误时,它似乎已经配置好了?@Simon:我刚刚发现这是由于死锁造成的。从之前阅读的文章来看,如果我将transactionscope作为包装所有调用的父方法,并且任何使用它自己的单个事务的子方法都将被忽略,那么情况似乎并非如此?很难帮助诊断带DTC的死锁,因为它在很大程度上取决于您的上下文、配置、安全性,等等。尝试在DTC死锁等情况下进行google搜索。同时确保您正在使用IsolationLevel.ReadCommitted选项(以匹配SQL server的默认值:)创建TransactionScope实例。谢谢你提供了所有有用的信息。@simon:我提供了更多关于如何解决锁的信息,但没有得到,因为我认为在transactionscope中忽略了单个事务?