Max pool size was reached

Have you ever encountered this error message on your application:

"Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occured because all pooled connections were in use and max pool size was reached."

This problem occurred most probably because of connection leak. Either the connection string do not close properly or consistently.

When you intend to close your database connection, you want to make sure that you are really closing it. The following code looks fine yet causes a connection leak:


     SqlConnection conn = new SqlConnection(myConnectionString);

      conn.Open();

      doSomething();

      conn.Close();     
           

If doSomething() throws an exception - conn will never get explicitly closed. Here is how this can be corrected:


     SqlConnection conn = new SqlConnection(myConnectionString);

      try

      {

            conn.Open();

            doSomething(conn);

      }

      finally

     {

            conn.Close();                

      }

When returning a connection from a class method - make sure you cache it locally and call its Close method. The following code will leak a connection:


     OleDbCommand cmd new OleDbCommand(myUpdateQuery, getConnection());

      intres = cmd.ExecuteNonQuery();

     getConnection().Close(); // The connection returned from the first call to getConnection() is not being closed.

Instead of closing your connection, this line creates a new one and tries to close it.

 

Here are some solutions that you can try to solve the problem:

1) Check your application to make sure all database connections are closed when it is not needed.  ASP.NET is supposed to have garbage collector to reclaim unused resource.  However, on a busy site, it is likely that the connection pool will run out of connections before garbage collection kicks in.

2) You can raise the connection pool size in the connection string.  For example, you can add "Max Pool Size=100" to your connection string to increase the pool size to 100.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值