SQL Server 数据库连接间歇性中断

SQL Server 数据库连接间歇性中断

偶尔,在我们的某个生产环境中(我们有十几个,没有不同的客户端指向每个环境),我们会在流程的不同点遇到异常。代码没有做任何花哨的事情 - 只是基本的插入和更新。然而,我们在单个连接中跨多个方法执行多个插入和更新。

我们会得到如下异常:

System.InvalidOperationException: ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.
at System.Data.SqlClient.SqlConnection.GetOpenConnection(String method)
at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)....

和这个:

System.Transactions.TransactionManagerCommunicationException: 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. ---> System.Runtime.InteropServices.COMException (0x8004D024): The transaction manager has disabled its support for remote/network transactions. (Exception from HRESULT: 0x8004D024)
at System.Transactions.Oletx.IDtcProxyShimFactory.ReceiveTransaction(UInt32 propgationTokenSize, Byte[] propgationToken, IntPtr managedIdentifier, Guid& transactionIdentifier, OletxTransactionIsolationLevel& isolationLevel, ITransactionShim& transactionShim)
at System.Transactions.TransactionInterop.GetOletxTransactionFromTransmitterPropigationToken(Byte[] propagationToken)
--- End of inner exception stack trace ---
at System.Transactions.Oletx.OletxTransactionManager.ProxyException(COMException comException)

和这个:

System.InvalidOperationException: Invalid attempt to call Read when reader is closed.
at System.Data.SqlClient.SqlDataReader.ReadInternal(Boolean setTimeout)
at System.Data.SqlClient.SqlDataReader.Read()

我认为我们不需要启用 MSDTC(同样,在其他所有环境中都可以正常工作)。关于我们可以从代码或环境方面检查什么,有什么建议吗?

答案1

调用数据库命令的方法是什么样子的?听起来你有一个跨越多个逻辑代码块的开放连接,而且持续了很长时间。

我建议SqlConnection.Open()在需要打开连接之前和SqlConnection.Close()完成连接后立即调用。这一切都应该在一个try/catch块中进行。

如果您正在执行上述方法,则意味着连接没有超时。在这种情况下,您需要查看 SQL Server 错误日志,看看是否有任何需要查看的错误被抛出。

相关内容