[英]SqlException when the database is not started but SQL Server is ready for connections
I am dealing with quite long delay between "ready for connections" state of SQL Server and the time when my database is started.我正在处理 SQL Server 的“准备连接”状态和我的数据库启动时间之间的相当长的延迟。 Sometimes it takes more then 2 minutes.有时需要超过 2 分钟。 That's reason why I sometimes try to connect to database before it's online.这就是为什么我有时会在数据库在线之前尝试连接到数据库的原因。 Do you know, how to reduce time between the "ready for connections" and started database?您知道如何缩短“准备连接”和启动数据库之间的时间吗? Or how can I determine that the db is online to connect to it from my C# application?或者我如何确定数据库在线以从我的 C# 应用程序连接到它?
Basically I have some "cache" of connections by connection string.基本上我有一些连接字符串的“缓存”连接。 This happens for the first connection of course, so my code for connections quite simple:这当然发生在第一次连接上,所以我的连接代码非常简单:
var result = new SqlConnection();
result.ConnectionString = "some connection string here";
try
{
result.Open()
}
catch (Exception ex)
{
//some logging stuff here
throw;
}
This is the C# exception:这是 C# 异常:
System.Data.SqlClient.SqlException: Cannot open database "XXX" requested by the login. System.Data.SqlClient.SqlException:无法打开登录请求的数据库“XXX”。 The login failed.登录失败。
This is the SQL Server log:这是 SQL Server 日志:
2014-03-31 08:21:05.65 - SQL Server is now ready for client connections. 2014-03-31 08:21:05.65 - SQL Server 现在已准备好进行客户端连接。 This is an informational message;这是一条情报信息; no user action is required.无需用户操作。
2014-03-31 08:21:09.21 - Recovery completed for database model (database ID 3) in 1 second(s) (analysis 234 ms, redo 0 ms, undo 514 ms.) This is an informational message only. 2014-03-31 08:21:09.21 - 数据库模型(数据库 ID 3)在 1 秒内恢复完成(分析 234 毫秒,重做 0 毫秒,撤消 514 毫秒。)这只是一条信息性消息。 No user action is required.无需用户操作。
2014-03-31 08:21:11.52 - Error: 18456, Severity: 14, State: 38. 2014-03-31 08:21:11.52 - 错误:18456,严重性:14,状态:38。
2014-03-31 08:21:11.52 - Login failed for user 'YYY'. 2014-03-31 08:21:11.52 - 用户“YYY”登录失败。 Reason: Failed to open the explicitly specified database.原因:无法打开明确指定的数据库。 [CLIENT: ] [客户: ]
2014-03-31 08:21:13.88 - Clearing tempdb database. 2014-03-31 08:21:13.88 - 清除 tempdb 数据库。
2014-03-31 08:21:21.38 - Recovery completed for database msdb (database ID 4) in 2 second(s) (analysis 327 ms, redo 0 ms, undo 468 ms.) This is an informational message only. 2014-03-31 08:21:21.38 - 数据库 msdb(数据库 ID 4)的恢复在 2 秒内完成(分析 327 毫秒,重做 0 毫秒,撤消 468 毫秒。)这只是一条信息性消息。 No user action is required.无需用户操作。
2014-03-31 08:21:32.98 - Starting up database 'tempdb'. 2014-03-31 08:21:32.98 - 启动数据库“tempdb”。
2014-03-31 08:21:40.30 - The Service Broker protocol transport is disabled or not configured. 2014-03-31 08:21:40.30 - Service Broker 协议传输被禁用或未配置。
2014-03-31 08:21:40.41 - The Database Mirroring protocol transport is disabled or not configured. 2014-03-31 08:21:40.41 - 数据库镜像协议传输被禁用或未配置。
2014-03-31 08:21:41.50 - Recovery is complete. 2014-03-31 08:21:41.50 - 恢复完成。 This is an informational message only.这只是一条信息性消息。 No user action is required.无需用户操作。
2014-03-31 08:21:41.52 - Service Broker manager has started. 2014-03-31 08:21:41.52 - Service Broker 管理器已启动。
2014-03-31 08:23:41.87 - Starting up database 'XXX'. 2014-03-31 08:23:41.87 - 启动数据库“XXX”。
Based on this link , the best practise is to query value of Collation property.基于此链接,最佳实践是查询 Collation 属性的值。
A database that has just come online is not necessarily ready to accept connections.刚刚上线的数据库不一定准备好接受连接。 To identify when a database can accept connections, query the collation_name column of sys.databases or the Collation property of DATABASEPROPERTYEX.要确定数据库何时可以接受连接,请查询 sys.databases 的 collation_name 列或 DATABASEPROPERTYEX 的 Collation 属性。
So when the Collation property is not null, the database is ready for connections.因此,当 Collation 属性不为 null 时,数据库已准备好进行连接。 The query look like this查询看起来像这样
SELECT DATABASEPROPERTYEX('MyDatabase', 'Collation')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.