简体   繁体   English

上次连接的SQL超时

[英]SQL timeout on last connection

I have a weird issue. 我有一个奇怪的问题。 I have a C# application that makes multiple connections to SQL (7 in total). 我有一个C#应用程序,可与SQL建立多个连接(共7个)。 Everything had been working fine for a while and then all of the sudden, SQL times out on the last connection. 一切运行了一段时间,然后突然之间,SQL在最后一个连接上超时。 That connection is pretty simple 该连接非常简单

public static void APP()
{
    using (SqlConnection conn7 = new SqlConnection(ConfigurationManager.ConnectionStrings["Connect"].ConnectionString))
    {
        conn7.Open();
        SqlCommand cmd7 = new SqlCommand("sp_proc", conn7);
        cmd7.CommandType = System.Data.CommandType.StoredProcedure;
        cmd7.ExecuteNonQuery();

        conn7.Close();
    }
}

My connection string looks like this. 我的连接字符串如下所示。

add name="Connect" connectionString="Data Source=Server; Initial Catalog=DB; User ID=User; Password=password" providerName="System.Data.SqlClient"

I am doing a using on each one and I am closing each connection at the end of each class. 我正在每个人上使用,并在每个课程结束时关闭每个连接。 Has anyone seen anything like this happen? 有没有人看到这样的事情发生? Do I have too many connections? 我的联系太多了吗?

I run each class in order from Main. 我从Main开始按顺序运行每个课程。

If it is timing out, there are 3 likely scenarios: 如果超时,则可能有3种情况:

  • sp_proc is simply taking too long to run; sp_proc花费的时间太长了; you'll need to address the code 您需要解决代码
  • there is some kind of locking that is making it impossible to complete (perhaps an open transaction on a competing SPID that has touched the same data and taken conflicting locks) 某种锁使无法完成(也许在竞争的SPID上的未完成交易已经接触了相同的数据并采取了冲突的锁)
  • there is some unrelated server load happening at the same time that is making it run too slow (this is unlikely to be the issue if it happens reliably) 同时发生一些无关的服务器负载,这使其运行速度太慢(如果可靠地发生,这不太可能成为问题)

I would recommend adding 我建议添加

cmd7.CommandTimeout = 6000

the time out is measured in seconds so put a time out that is acceptable for the users of the application, 超时以秒为单位,因此请为应用程序用户接受可以接受的超时,

I'd recommend this for all your SQL connections too just as a standard this way you should always have sufficient time to get the data. 我也建议将它作为所有SQL连接的标准,这样一来,您应该始终有足够的时间来获取数据。

one thing you might want to do is run a trace \\ sql profile on the database that youre running against and also check for locking of some kind. 您可能想做的一件事是在要运行的数据库上运行trace \\ sql配置文件,并检查某种形式的锁定。

If this is timing out, I would think that there is a process that is being suspended or a lock of some kind somewhere 如果这超时了,我会认为某个进程正在被暂停或某种形式的锁定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM