Community wiki answer originally left as an edit to the question by its author:
The problem was that the database property AutoClose
was set on.
The solution was to set AutoClose
to false.
There are couple of things to note here. The database will only be accessible by query seamlessly and would allow connection if all three phases of recovery has completed. The three phases are analysis, redo and Undo(sometimes if DB is in consistent state after redo an UNDO phase would not be required). In enterprise edition the database would come online after second phase (this is called Fast recovery which was introduced from SS 2005 onwards) and you would see database as online but still internally third phase, the UNDO, phase is going on and would be accessible completely only after third phase completes. What I mean by completely is For fast recovery, a query may bump into one of the locks being held to allow fast recovery – in which case it will have to wait for that lock to be dropped as UNDO progresses.Please read more about Fast recovery
In standard edition database would only come online after all three phases have completed.
You can read more about three phases of recovery
You can check status of database using sys.databases view
select state from sys.databases
In errorlog if it is written for particular database that Recovery is complete then you can be assured that database is online and can be accessed
EDIT
EDIT: How do i tell when a database is ONLINE but still in the UNDO recovery stage?
Well easiest method is reading errorlog it would have information like
Recovery of database 'TestDB' (5) is 3% complete (approximately 1294 seconds remain). Phase 2 of 3. This is an informational message only. No user action is required.
2015-03-05 10:07:47.76 spid18s Recovery of database 'TestDB' (5) is 3% complete (approximately 1281 seconds remain). Phase 2 of 3. This is an informational message only. No user action is required.
2015-03-05 10:07:47.78 spid18s Recovery of database 'TestDB' (5) is 3% complete (approximately 1281 seconds remain). Phase 3 of 3. This is an informational message only. No user action is required.
2015-03-05 10:07:47.99 spid18s CHECKDB for database 'TestDB' finished without errors on 2013-08-13 21:40:44.543 (local time). This is an informational message only; no user action is required.
~DATABASE ONLINE
If you look at logs you could see information related to what phases completed
I would say you should not worry about when database would come online because in any case it will always follow three phases of recovery (when undergoing crash recovery) you should focus on why Unexpectedly SQL server restarted. This should be avoided at all cost. Make sure SQL Server is patched to latest service pack
Best Answer
As confirmed by you the SQL server is of enterprise version and database was not starting after restore so the recovery process can utilize fast recovery. In enterprise edition the database would come online after second phase (this is called Fast recovery which was introduced from SS 2005 onwards) and you would see database as online but still internally third phase, the UNDO, phase is going on and would be accessible completely only after third phase completes. What I mean by completely is For fast recovery, a query may bump into one of the locks being held to allow fast recovery – in which case it will have to wait for that lock to be dropped as UNDO progresses
I suggest you also read