0

I had an application that was functioning properly on an IIS server. However, after hosting it on an ECS fargate Linux container and conducting a load test, the ECS task crashes due to the following error:

Microsoft.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired. The timeout period elapsed before the operation could be completed, or the server is not responding.

Also, I occasionally encounter the following error message:

System.InvalidOperationException: Timeout expired. The timeout period elapsed before obtaining a connection from the pool. This could be due to all pooled connections being in use and the maximum pool size being reached.

Even though my query has a proper index and a timeout of 5 seconds, it executes within only 20ms. Therefore, the query itself is not the cause of the issue.

Here is my connection string:

Data Source=x.x.x.x;Initial Catalog=DbName;User ID=Username;Password=mypassword;TrustServerCertificate=True

Previously, I had "MultipleActiveResultSets=True" in my connection string, but even after removing it, I still encounter the same error.

Here is my dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
# copy csproj and restore as distinct layers
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY *.sln .
COPY ["PdfApp/PdfApp.csproj", "PdfApp/"]
RUN dotnet restore "PdfApp/PdfApp.csproj"
# copy and publish app and libraries
COPY . .
WORKDIR "/src/PdfApp"
RUN dotnet build "PdfApp.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "PdfApp.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "PdfApp.dll"]

I am utilizing .NET Core 7.0, with the package version of Microsoft.Data.SqlClient is set to 5.1.1, while the SQL server is running on Microsoft SQL Server 2019.

Please find below the connection configuration.

public interface IDbCon : IDisposable
{
    IDbConnection GetConnection { get; }
}

public class DbCon : IDbCon
{
    private readonly IConfiguration _config;
    private IDbConnection _connection;

    public DbCon(IConfiguration configuration)
    {
        _config = configuration;
        _connection = new SqlConnection(_config.GetConnectionString("ConStr"));
    }

    public IDbConnection GetConnection
    {
        get
        {
            if (_connection.State == ConnectionState.Closed)
            {
                _connection.Open();
            }
            return _connection;
        }
    }

    public void Dispose()
    {
        if (_connection.State == ConnectionState.Open)
        {
            _connection.Close();
        }
        _connection.Dispose();
    }
}

I'm utilizing Dapper to perform the data queries from the database.

public class Repository : IRepository
{
    private readonly IDbCon _conn;

    public Repository(IDbCon conn)
    {
        _conn = conn;
    }

    public async Task<IEnumerable<CustomList>> MyFunctionName()
    {
        IEnumerable<CustomList> list = new List<CustomList>();
        var con = _conn.GetConnection;
        try
        {
            list = await con.QueryAsync<CustomList>("spname", commandType: CommandType.StoredProcedure, commandTimeout: 30);
        }
        finally
        {
            con.Close();
        }
        return list;
    }
}
Roshan
  • 167
  • 1
  • 16
  • Few questions, where your database is hosted? Do you get timeout error always or only a few times randomly. – Ankush Jain Jul 05 '23 at 07:07
  • Also, can you share little bit of code, how are you opening and closing the database connection? Might be, you are not disposing the open connections properly. – Ankush Jain Jul 05 '23 at 07:08
  • You need to look at your database server's active connection count, and maximum connection setting, to see if you are just trying to open more connections than the database server allows. You should also verify that the database server isn't becoming unresponsive because of CPU load. – Mark B Jul 05 '23 at 11:45
  • @AnkushJain As I mentioned earlier, I only experience the issue when performing load tests with heavy requests, yet the CPU and memory usage remains within normal limits and does not exceed 40%. The application is functioning properly on IIS, as it is hosted on an EC2 Windows machine equipped with 16 CPU cores and 128GB of RAM. – Roshan Jul 05 '23 at 12:33
  • @MarkB The database is functioning smoothly with IIS, but there seems to be an issue with Docker on Linux. It appears that the problem may be related to SqlClient. Could you please review the following issue on GitHub for more details: Issue: https://github.com/dotnet/SqlClient/issues/1530 – Roshan Jul 05 '23 at 12:38
  • @AnkushJain Question updated with open and closing connection – Roshan Jul 05 '23 at 12:49

0 Answers0