Database clients: how to wait for database activation after SQL Server restart?

孤街醉人 提交于 2019-12-11 06:47:25

问题


Service dependency is not enough to guarantee that database clients will find their SQL Server up and running after a system reboot. They may be able to create a connection to the master database, but a specific database may still be in the process of opening (in the recovery mode) and connections to it will be refused for some initial period of time.

The exact duration of the delay is highly variable and depends on factors such as number of databases on the system, recovery mode, LDF file size, or traffic prior to the reboot.

The well known approach to dealing with the dependency on the client is sleeping and retrying. However, this approach does not seem exactly clean or reliable, especially when the clients are diverse in terms of technology and ownership and when this race condition appears only very rarely.

Is there any better way to synchronize the clients with the server start-up, ideally through keeping the SQL Server service in the "Starting" state until all databases have been either opened or marked suspect?


回答1:


Create a windows service that tries to access the database which does not report itself as having started unless successful in establishing a reliable connection. Basically that service would return an error and the service manager would try to run it according to the service settings.

And now you can depend on that new service in your other services that would guarantee that the database is responsive.

Related questions

  • In C# what is the best way to determine if a database is up and running?

Update

Also, for a much cleaner way to know if the database is up and running is to check the following value:

SELECT state_desc
FROM sys.databases
WHERE name = 'YourDatabase'

If it says anything but 'ONLINE', then the database is not yet ready. (if it exists at all) http://msdn.microsoft.com/en-us/library/ms190442.aspx



来源:https://stackoverflow.com/questions/9687871/database-clients-how-to-wait-for-database-activation-after-sql-server-restart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!