Usually this error occurs when you have not closed your open connections in the application. When the connections are not closed they are added to a connection pool and once the pool reaches it's maximum above exception would be thrown.
You can increase the pool size from your web config connection string as below but it's not the perfect solution.
Data Source=TestServer;Initial Catalog=Northwind;
User ID=Chester;Password=Tester;Max Pool Size=50;
Min Pool Size=5;Pooling=True;
Most perfect solution would be to close the database connections once your task is finished. You can use some thing like below.
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = connection.CreateCommand();
command.CommandText = "select * from test";
command.CommandType = CommandType.Text;
connection.Open();
object ret = command.ExecuteScalar();
}
{
throw e;
}
finally
{
conn.Close();
conn.Dispose();
}
}
at the end of the "using" block connection will be closed and disposed.
Also can use below code.
try
{
SqlConnection connection = new SqlConnection(connectionString);
catch (Exception e)
SqlCommand command = connection.CreateCommand();
command.CommandText = "select * from test";
command.CommandType = CommandType.Text; connection.Open();
object ret = command.ExecuteScalar();
}
}
{
throw e;
}
finally
{
conn.Close();
conn.Dispose();
}
No comments:
Write comments