服务器中的连接超时

时间:2015-03-24 08:31:40

标签: c# connection-pooling connection-timeout

我想询问连接超时,因为我遇到了问题。 错误是

  

"超时已过期。从池中获取连接之前经过的超时时间。这可能已经发生,因为所有池化连接都在使用中并且达到了最大池大小。"

这样的代码会导致连接超时吗? 调用连接的最佳做法是什么?

using (var cnn = OpenExternalContractor()) //call connection string
{
    var query = @"insert into interfaceRequest  (requestNo,requestAssemblyName,requestApplicationName,requestFunctionName,
            requestParameters,requestUserName,requestUserMail,requestDate)
            values (@requestNo,@requestAssemblyName,@requestApplicationName,@requestFunctionName,
            @requestParameters,@requestUserName,@requestUserMail,getDate())";
                cnn.Execute(query, new
                {
                    requestNo = requestNo,
                    requestAssemblyName = requestAssemblyName,
                    requestApplicationName = jobName.Split('-')[0],
                    requestFunctionName = jobName.Split('-')[1],
                    requestParameters = requestParameters,
                    requestUserName = requestUserName.Replace("\"", "\\"),
                    requestUserMail = userMail
                });
                //Call another function here that call connection string
                PersonnelRepository.UpdateLastNumber(requestNamePrefix,   Convert.ToInt32(lastNumber));
}

public static void UpdateLastNumber(string type, int value)
{
    using (var cnn = OpenExternalContractor()) //Call connection string
    {
        var query = "update AutoNumber set LastNumber = " + value + " where Type = '" + type + "'";
        cnn.Query<int>(query);
    }
}

以下是连接字符串的代码:

protected static IDbConnection OpenExternalContractor() 
{
    IDbConnection dbConnection = new SqlConnection(WebConfigurationManager.ConnectionStrings["ExternalContractor"].ConnectionString);
    dbConnection.Open();
    return dbConnection;
}

谢谢,

1 个答案:

答案 0 :(得分:0)

使用WebConfigurationManager.ConnectionStrings["ExternalContractor"].ConnectionString获取连接字符串时,可以通过更改此连接字符串来更改WebConfig文件中的连接超时,例如:

Data Source=(local);
Initial Catalog=AdventureWorks;
Integrated Security=SSPI;
Connection Timeout=30

来自social.msdn.microsoft.com