简体   繁体   中英

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. Sometimes it takes more then 2 minutes. 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?

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:

System.Data.SqlClient.SqlException: Cannot open database "XXX" requested by the login. The login failed.

This is the SQL Server log:

2014-03-31 08:21:05.65 - SQL Server is now ready for client connections. 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. 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 - Login failed for user '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: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. No user action is required.

2014-03-31 08:21:32.98 - Starting up database 'tempdb'.

2014-03-31 08:21:40.30 - The Service Broker protocol transport is disabled or not configured.

2014-03-31 08:21:40.41 - The Database Mirroring protocol transport is disabled or not configured.

2014-03-31 08:21:41.50 - Recovery is complete. 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:23:41.87 - Starting up database 'XXX'.

Based on this link , the best practise is to query value of Collation property.

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.

So when the Collation property is not null, the database is ready for connections. The query look like this

SELECT DATABASEPROPERTYEX('MyDatabase', 'Collation')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM