0

I am facing weird behaviour on IIS 8.5. I am getting an error after I consume my endpoint few times (5-6 times): 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.

My code is written in net core 3.1, and connects to sql server and tries to execute a stored procedure. it works locally with any number of calls. Verified locally that no exception is being thrown.

 public int GetCount()
 {
        int jobCount = 0;
        using (SqlCommand com = new SqlCommand("dbo.GETCount", new SqlConnection(this.ConnectionString)) { CommandType = CommandType.StoredProcedure })
        {
            com.Parameters.Add(new SqlParameter("@jobCount", SqlDbType.Int) { Direction = ParameterDirection.Output });
            try
            {
                com.Connection.Open();
                com.ExecuteNonQuery();
                jobCount = (int)com.Parameters[0].Value;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                com.Connection.Close();
            }

        }

        return jobCount;
    }

Any suggestions?

1 Answer 1

2

Here are my suggestions.

  1. Always close your connection in the finally block

  2. Increase pool size like in your connection string

    string connectionString = "Data Source=localhost; Initial Catalog=Northwind;" +

    "Integrated Security=SSPI; Min Pool Size=10; Max Pool Size=100";

  3. Don't use pooling at all

    string connectionString = "Data Source=localhost; Initial Catalog=Northwind;" +

    "Integrated Security=SSPI; Pooling=false;";

Sign up to request clarification or add additional context in comments.

2 Comments

Do I need the finally block if the "using" block being used? thought.net core framework handles that by itself?also I thought pooling enhances performance
If the object in using block implements the IDispose interface, there is no need to use finally block. The performance of pool not only depends on core framework, but also developer's code and configuration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.