Note that there are some explanatory texts on larger screens.

plurals
  1. POConnecting to SQL Server fails in autostart service, works when starting manually. SQL Server not ready, yet service is started
    primarykey
    data
    text
    <h2><strong>TL;DR</strong> - MSSQL Service claims to be ready (status = Started) while it's not really ready (see below strike-outed text)</h2> <p><s>I have the C# written windows service which installs itself as <code>ServiceAccount.LocalSystem</code> and specifies <code>MSSQLSERVER</code> in <code>ServicesDependedOn</code>.</p> <p>Now I've created MS SQL user, granted it all the necessary permissions &amp; roles (including <code>db_owner</code> on the needed database, and Connect / Login privileges). </p> <p>When running the service manually (via <code>net start</code> or <em>services.msc</em> manager) it works corretly, i.e. it connects to the database. When I restart the machine, the service throws this exception:</p> <pre><code>Cannot open database "xxxx" requested by the login. The login failed. Login failed for user 'xxxxx'. </code></pre> <p>What have I configured incorrectly? Again, when I run the service manually it works fine!! I've also tried logging in with this account via MS SQL Management Studio - and this works too.</s></p> <hr> <p><s>as suggested in comments, I've tried waiting before trying to connect - 20sec Sleep doesn't solve the problem. I'm adding the state of MSSQLSERVER service (checked via ServiceController) to the log, and it is "Running". Everything seems to be fine, except that the <strong>Login fails when service is being auto-started</strong></p> <h2></s></h2> <p>Ok, I've been tracking this down for a few hours. Here's what I've found:</p> <p><strong>SQL Server</strong> service (MSSQLSERVER) <strong>claims to be ready</strong> (status = Started) quite quickly (about 2-3 seconds after issuing the "net start" command). Unfortunately warming up (starting up databases, recovery and some other stuff) takes place <strong>later</strong>, and <strong>takes up to 2 minutes</strong> (120 seconds!!). Of course it rejects connections until it's warmed up.</p> <p>I've ended up doing </p> <pre><code>try { connect; } catch { RequestAdditionalTime(); // to avoid Windows Service timeout Sleep(); } </code></pre> <p>in a <code>while</code> loop. </p> <p>I hate this kind of solutions but can't find anything cleaner.</p> <p>If anyone knows how to do it properly, please answer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload