Sql-server – MS SQL Server 2008R2, “Login failed for user” from time to time, IIS 7 , NET 4.0

sql server

I'm fighting with strange problem that that most probably is connected with Windows / MS SQL Server security/privileges setup. When external app (that runs with administrator privileges) polls the IIS server(it does login to database with already opened connection, checks for data) and when there is data to process(with the same connection when it then tries to get data) fails from time to time with this error:
        Cannot open database "TheDatabase" requested by the login. The login failed. Login failed for user 'DatabaseUser'.
        Even worse, when this happens – every activity that involves database fails with same error message. So for a few minutes the database and thus whole website is down. But I'm not able to recreate this no matter what I do and I'm only able to see this in exception logs. Database user,that public login is mapped to, is 'dbowner' and nothing points to why it happens. This issue seem to appear after update of website code – ie this happened due to some particular changes in either Windows security or SQL Server config or code, but review of changes in sources gives no clues as everything seems 100% unrelated to this. Thanks in advance for helping with this.

Best Answer

Check the Auto Close property on the database you're trying to connect to.

From MSDN:

If True, the database is closed, and its resources are freed when no user connection accesses the database. If False (default), the server maintains the database in an open and ready state regardless of user activity.

Take a look at this article: http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/sql-server-auto-close