这是一篇转载,参考原文在:https://blogs.msdn.microsoft.com/angelsb/2004/08/25/connection-pooling-and-the-timeout-expired-exception-faq/
https://blog.csdn.net/marklr/article/details/4325699

客户在IIS上建立了WebApp,间歇性会发生异常,异常信息为:
Exception System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

从字面上理解就是因为连接超时,可能原因是最大连接数已满。常见的原因主要有三种:

1.You use more than Max Pool Size connections (Max Pool Size default=100)

This is fairly rare in most applications, 100 concurrent connections is a very large number when you are using pooling. In my experience the only time this has been the cause of the exception above is when you open all 100 connections in a single thread as shown below:

  SqlConnection[] connectionArray = new SqlConnection[101];for (int i = 0; i <= 100; i++){connectionArray[i] = new SqlConnection("Server=.\\SQLEXPRESS ;Integrated security=sspi;connection timeout=5");connectionArray[i].Open();}

Solution: Once you have determined that you are using more than 100 concurrent connections (with the same connection string) you can increase Max Pool Size.

2.You are leaking connections

My definition of a leaked connection is a connection that you open but you do not Close OR Dispose explicitly in your code. This covers not only the times when you forget to make the connection.Close() or Dispose() call in your code, but the much harder to catch scenarios where you do call connection.Close but it does not get called! See below:

using System;

using System.Data;

using System.Data.SqlClient;

public class Repro

{

  public static int Main(string[] args){Repro repro = new Repro();for (int i = 0; i <= 5000; i++){try{ Console.Write(i+" "); repro.LeakConnections(); }catch (SqlException){}}return 1;}public void LeakConnections(){          SqlConnection sqlconnection1 = new SqlConnection("Server=.\\SQLEXPRESS ;Integrated security=sspi;connection timeout=5");sqlconnection1.Open();SqlCommand sqlcommand1 = sqlconnection1.CreateCommand();sqlcommand1.CommandText = "raiserror ('This is a fake exception', 17,1)";sqlcommand1.ExecuteNonQuery();  //this throws a SqlException every time it is called.sqlconnection1.Close(); //We are calling connection close, and we are still leaking connections (see above comment for explanation)}

}

Paste this code into visual studio and place a breakpoint in the sqlconnection1.Close(); line, it will never get called since ExecuteNonQurery throws an exception. After a short while you should see the dreaded Timeout exception, in my computer it happens at around 170 connections. This is definitely a contrived example, I am stacking the deck by lowering the connection timeout and throwing an exception every call, but when you consider moderate to heavy load on an ASP.NET application any leak is going to get you in trouble.

3.You are rapidly opening or closing connections with sql debugging enabled in Visual Studio.

There is a known bug with Visual Studio 2003 and Sql Debugging, take a look at http://support.microsoft.com/default.aspx?scid=kb;en-us;830118

System.InvalidOperationException: Timeout expired. Max Size Pool connections.相关推荐

  1. 连接池和 Timeout expired异常

    转自:博客园宁静.致远:http://www.cnblogs.com/zhangzhu/archive/2013/10/10/3361197.html 异常信息: MySql.Data.MySqlCl ...

  2. 连接池和 Timeout expired异常【转】

    异常信息: MySql.Data.MySqlClient.MySqlException (0x80004005): error connecting: Timeout expired. The tim ...

  3. [译]连接池和 Timeout expired异常

    原文标题:Connection Pooling and the "Timeout expired" exception FAQ 来源:http://blogs.msdn.com/a ...

  4. Timeout expired

    Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may ...

  5. C# asp.net 连接 Sql Server数据库 Timeout expired. 错误,怎么办?

    连接数据库时出现 Connection Timeout Expired. The timeout period elapsed while attempting to consume the pre- ...

  6. oracle之 RA-00054: resource busy and acquire with NOWAIT specified or timeout expired

    1. truncate 表报 ORA-00054 ,标明有事务正在操作该表 SQL> truncate table alldm.DM_XQKD_YUJING_D; truncate table ...

  7. System.InvalidOperationException: The stream does not support reading

    We are using EMS(.Net DLL, TIBCO.EMS.dll, version 4.3.0) as the middleware to communicate between .N ...

  8. 【Oracle】ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired

    今天在修改表字段类型时出现错误:ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired,看报错应该是 ...

  9. pyvisa.errors.VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed

    在使用python的pyvisa库函数通过visa指令控制仪器时,出现'pyvisa.errors.VisaIOError: VI_ERROR_TMO (-1073807339): Timeout e ...

最新文章

  1. 安卓实训项目:音乐播放器2.0——实训报告2
  2. java对xml文件的读写_java 自己做的对XML文件的读写操作
  3. WINFORM 多条件动态查询 通用代码的设计与实现
  4. 'staticfiles' is not a registered tag library. Must be one of:
  5. [JSOI2007]字符加密
  6. css中auto啥意思,CSS中各种auto值的作用总结
  7. 21世纪经济网APP
  8. Flash研究(一)——本地通讯
  9. Hive Sql 安装
  10. c++11新特性介绍
  11. WebSphere Application Server V7、V8 和 V8.5 中的高级安全性加强,第 1 部分: 安全性加强的概述和方法...
  12. 关于jmeter运行提示没有权限 报错
  13. 【转】如何成为一名黑客--Eric Steven Raymond
  14. 财经数据----同花顺技术选股,附代码
  15. 基于Django框架实现前后端分离(三)
  16. umijs配置eslint,prettier,stylelint,commitlint
  17. matlab如何添加数据集,如何用matlab把数据集导入数据库
  18. 除氨氮树脂在垃圾渗滤液行业的应用优势
  19. KingBaseES 报错Connection to X.X.X.X:54321 refused.Check that the hostname and port are correct and ..
  20. 在线重装Win11系统详细教程

热门文章

  1. jsliang 陪你瞎叨叨 2
  2. Java 如何判断一个字符串中是否包含某一 子字符串
  3. 聊天机器人之语音识别科大讯飞 Node.js SDK
  4. 2019年工作总结——没有岁月可回首,珍惜当下不负流年
  5. oracle 客户端 sqlplus 命令行 问号 乱码
  6. UR机器人使用教程-总结
  7. Visio如何把形状置于底层
  8. android实现按键找图功能,按键精灵安卓版找图教程 找图功能使用技巧介绍
  9. Linux回炉学习(一)
  10. 树莓派摄像头在桌面不显示 | 树莓派VNC摄像头实时显示