繁体   English   中英

从池中获取连接之前经过了超时时间,所有池中的连接都在使用中,并且已达到最大池大小

[英]The timeout period elapsed prior to obtaining a connection from the pool, All pooled connections were in use and max pool size was reached

我有一个exe文件,显示以下错误。

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

使用过该语句来创建连接对象,并在finally块中关闭连接。

在exe连接字符串的web.config中,最大app_pool大小值为200。

我还使用sp_who2命令检查了连接,该命令显示25个连接。

private Dictionary<string, SomeObject> SomeMethod(int orderedXmlId, string clientLogFilePath, ref bool errorGettingRMData)
        {
            Dictionary<string, SomeObject> someDictionary = new Dictionary<string, SomeObject>();
            //Create connection object
            using (SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["MasterDB"]))
            {
                SqlCommand command = null;
                try
                {
                    //open the connection
                    connection.Open();
                    command = new SqlCommand("someSP", connection);
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@someParameter", someParameter);
                    command.CommandTimeout = _commandTimeOut;

                    using (SqlDataReader someVariable = command.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        if (someVariable != null && someVariable.HasRows)
                        {
                            int intId = someVariable.GetOrdinal("intId");
                            int imageUrl = someVariable.GetOrdinal("imageUrl");
                            int contentLength = someVariable.GetOrdinal("contentLength");
                            int path = someVariable.GetOrdinal("path");
                            int parentId = someVariable.GetOrdinal("parentId");
                            int propertyId = someVariable.GetOrdinal("propertyId");

                            while (someVariable.Read())
                            {
                                using (SomeObject someUploadObject = new SomeObject())
                                {
                                    // Avoid this Exception: "An item with the same key has already been added".
                                    if (!someDictionary.ContainsKey(someVariable.GetString(intImageUrlOrdinal)))
                                    {
                                        someUploadObject.OrderedXmlId =
                                            someVariable.GetInt32(intOrderedXMLIdOrdinal);
                                        someUploadObject.ImageUrl = someVariable.GetString(intImageUrlOrdinal);
                                        someUploadObject.ContentLength =
                                            someVariable.GetInt64(intContentLengthOrdinal);
                                        someUploadObject.SaveAsPath = someVariable.GetString(intSaveAsPathOrdinal);
                                        someUploadObject.ParentOrderedXmlId =
                                            someVariable.GetInt32(intParentOrderedXmlIdOrdinal);
                                        someUploadObject.PropertyId = someVariable.GetString(intPropertyIdOrdinal);

                                        //Add to Dictionary
                                        someDictionary.Add(someUploadObject.ImageUrl, someUploadObject);
                                    }
                                    else
                                    {
                                        if (detailedLog)
                                        {
                                            AddToLog("SomeMethod: Image {0}, for id:{1} is already present in someDictionary",
                                                                     new object[] { someVariable.GetString(imageUrlOrdinal), lId }, 80,
                                                                     false, true, clientLogFilePath);
                                        }
                                    }
                                }
                            }
                            someVariable.Close();
                        }
                        //No records are found i.e. Datareader is empty
                        else
                        {
                            //Do nothing
                        }
                    }
                }
                catch (Exception ex)
                {
                    errorGettingData = true;
                    Logger.Write2Log("SomeMethod: Error in retrieving uploaded images for id:" + Id + "-" + ex.Message, true, 40, true, logFilePath);
                    SendMail(MailOption.exception, ex, logFilePath);
                }
                finally
                {
                    command = null;
                    if (connection != null)
                    {
                        connection.Close();
                    }
                }
                //Return list of Dictionary
                return someDictionary;
            }
        } //SomeMethod

知道这里可能出了什么问题吗?

好吧,错误是自我描述的。 从通常的嫌疑人开始:

  1. 检查您的服务器是否确实在使用您想要的配置(而不是限制为25个连接的配置)。 看看问题是否真的从这个数字开始。
  2. 如果可能的话,直接检查池-如果是建立的连接正在被阻塞,则说明您没有正确丢弃它们-通常存在一种处理此问题的模式。 使用它,不要重新发明轮子。

暂无
暂无

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

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