[ad_1]
我正在执行使用 Docker 上托管的 SQL 数据库的测试。 我在某些测试中遇到超时问题
var msSqlContainer = new ContainerBuilder() .WithImage("mcr.microsoft.com/mssql/server:2017-latest") .WithName(Guid.NewGuid().ToString()[..5]) .WithPortBinding(1433, true) .WithEnvironment("ACCEPT_EULA", "Y") .WithEnvironment("SQLCMDUSER", "sa") .WithEnvironment("MSSQL_SA_PASSWORD", password) .WithEnvironment("SQLCMDPASSWORD", password) .WithCleanUp(true) .WithNetwork(network) .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(1433)) .Build(); 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.
我尝试过的:
我尝试将“连接超时= 60”添加到连接字符串中
我使用下面的查询检查有多少个连接,我的连接数始终低于 30 个
SELECT a.* FROM (SELECT DB_NAME(dbid) as DBName, COUNT(dbid) as NumberOfConnections, loginame as LoginName FROM sys.sysprocesses WHERE dbid > 0 GROUP BY dbid, loginame) a ORDER BY a.DBName, a.LoginName
解决方案1
是的,我确信可以访问数据库。 在 3000 个测试中,有些测试因 Sql 超时而随机失败。
以下是我用来打开数据库连接的方法
private DbConnection GetDBConnection() { var db = _sqlDBfactory.CreateConnection(); db!.ConnectionString = _databaseConnectionString + "Initial Catalog=clients;"; db.Open(); return db; }
调用示例:
using var db = GetDBConnection(); try { return db.QueryFirst<string>( $"SELECT statment goes here... omitted purposly; } finally { db.Close(); }
[ad_2]
コメント