Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tests executing that are using an SQL database hosted on Docker. I am facing timeout issues on some tests

var msSqlContainer = new ContainerBuilder()
	.WithPortBinding(1433, true)
	.WithEnvironment("ACCEPT_EULA", "Y")
	.WithEnvironment("SQLCMDUSER", "sa")
	.WithEnvironment("MSSQL_SA_PASSWORD", password)
	.WithEnvironment("SQLCMDPASSWORD", password)
await msSqlContainer.StartAsync();

var msSqlPort = msSqlContainer.GetMappedPublicPort(1433);

_databaseConnectionString = $"Server={msSqlContainer.Hostname},{msSqlPort};User Id=sa;Password={password};TrustServerCertificate=true;";

Timeout after 239.3025928 calling database. Caller:get_cust_messages_by_cust Message:Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.

What I have tried:

I have tried adding 'Connection Timeout=60' to the connection string

I check how many connections I have using the below query, which I am always under 30 connections
DB_NAME(dbid) as DBName,
COUNT(dbid) as NumberOfConnections,
loginame as LoginName
dbid > 0
dbid, loginame) a
ORDER BY a.DBName, a.LoginName
Member 15627495 19-Jan-24 9:05am    
you didn't mention if an easy query return result the good way.

try one basic query...
Are you sure you reach the DB as expected ?

if the connexion fails, you have to review your 'connect DB process'

you are going too fast in your 'debug' ...
before the query, you need a DB connexion.

the 'connexion timeout' will abort 'processing query' and 'living connexion' by '60 s'with this TimeOut = 60 in your connexion string.

1 solution

Yes I am sure database could be reached. Out of 3000 tests some are failing randomly with Sql timeouts.

Below is the method I am using to open the DB connection
private DbConnection GetDBConnection()
    var db = _sqlDBfactory.CreateConnection();
    db!.ConnectionString = _databaseConnectionString + "Initial Catalog=clients;";
    return db;

Calling example:
using var db = GetDBConnection();
	return db.QueryFirst<string>(
		$"SELECT statment goes here... omitted purposly;
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900