简体   繁体   中英

SQL timeout on last connection

I have a weird issue. I have a C# application that makes multiple connections to SQL (7 in total). Everything had been working fine for a while and then all of the sudden, SQL times out on the last connection. 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.

If it is timing out, there are 3 likely scenarios:

  • sp_proc is simply taking too long to run; 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)
  • 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.

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.

If this is timing out, I would think that there is a process that is being suspended or a lock of some kind somewhere

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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