I have an application built in .net 6 that runs in Azure as an App Service. It connects to a SQL Azure database to run queries, update data etc.
Mostly this runs fine, but occasionally we run into a problem where lots of errors are generated in the app due to the connection pool running out of connections. Specifically the error is:
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.
The problem normally happens for a few minutes and then it resolves itself and everything goes back to normal.
So far I've:
- Checked that the code isn't opening more db connections that necessary.
- Checked that all connections are being closed
- Checked performance on the Azure SQL server - it doesn't seem to indicate any spikes in usage at the times this is occurring
- Increased the number of connections in the pool through the connection string. This has now been increased to 250, but the problem still keeps occurring
Troubleshooting is challenging as the problem is intermittent and we only discover it when we check App Insites for the site. The problem is occurring once every day or two - so not frequently, but still a problem.
How can I get more information on what might be causing this and how I can resolve it?
Example code:
protected DbConnection GetOpenConnection()
{
var connection = new SqlConnection(configurationProvider.ConnectionString);
connection.Open();
return connection;
}
public int? GetUncommitedFilesCount(string machineName)
{
using (var connection = GetOpenConnection())
{
var command = connection.CreateCommand();
command.CommandText = @"Select UncommittedFilesCount from Machine Where MachineName = @MachineName";
var parameter = command.CreateParameter();
parameter.ParameterName ="@MachineName";
parameter.Value = machineName;
command.Parameters.Add(parameter);
var result = command.ExecuteScalar();
if (result == null)
{
return null;
}
return Convert.ToInt32(result);
}
}