views:

281

answers:

1

With some slower machines that our application runs on (which is in the windows startup folder), when the machine is Rebooted and autologged on, the SQL Server service is still in a starting up state when my application is launched. (SqlServer 2005)

What I've done to counter this is to Check the Service state (MSSQL$idealsql) and wait until the service is Running.

Public Function isServiceRunning() As Boolean
    Dim theServices() As System.ServiceProcess.ServiceController
    Dim theservice As System.ServiceProcess.ServiceController
    theServices = System.ServiceProcess.ServiceController.GetServices
    Dim running As Boolean

    For Each theservice In theServices
        If String.Equals(theservice.ServiceName, mServiceName, StringComparison.OrdinalIgnoreCase) Then
            running = (theservice.Status = ServiceProcess.ServiceControllerStatus.Running)
            Exit For
        End If
    Next
    Return running
End Function

This is returning the correct result once the service comes online, but SQLServer takes a little while before it is fully operational after the service has been started.

Update
This process is only used on application startup.
PseudoCode.
Check Service running (this process)
Check Database exists
false -> create database
true -> perform any updates required to structures etc
Continue into main program execution
End Update

I then need to check for the presence of our Database (that gets dynamically created if it's not present), which I do via looking as sysdatabases, and finding if our database is there, but once again, it takes a little bit before all the databases are actually brought online.

I also found some additional code to check for the creation of the TempDB and get the date that it was created to determine when the service came online.

SELECT crdate AS StartTime FROM sys.sysdatabases WHERE name = 'tempdb'

According to the EventVwr, TempDB is the First Database brought online.

Is there a proper way to determine that SQLServer is fully online, and all databases have been started up?

Whilst the code shown is VB.Net i'm not too concerned with the language, moreso just the proper way to determine if the Database Server is fully online.

A: 

On a separate thread in your application, poll a table in your database (every 10 seconds say) with a low I/O query such as

SELECT 1 FROM Some Table

wrapped in 'appropriate' exception handling.

Based on the result of that select (1 or NULL), raise your custom .NET events DBOnline and DBOffline respectively.

Another approach using DMV's (for SQL Server 2005 onwards) is discussed in this article: How to tell when the SQL Server service was started

USE master
GO 

SELECT TOP 1        
    sample_ms AS Millisecond_Since_Start
FROM        
    sys.dm_io_virtual_file_stats(DB_ID(), NULL)
Mitch Wheat