SQL Server Error 1414: Database Closing Fix
In this article
Error 1414 in SQL Server (“The database is being closed before database”) appears when a connection or query tries to use a database that is in the middle of shutting down. The usual cause is a concurrent operation: someone runs ALTER DATABASE ... SET OFFLINE, a restore or detach is in progress, the database is set to auto-close and the last user disconnected, or a failover/availability group move is happening. The fix is to stop the closing operation, bring the database back online, and remove the setting that makes it close unexpectedly. You do not need to restart the whole server in most cases.
What You'll Need
- SQL Server Management Studio (SSMS) or another client that can run T-SQL, connected with an account that has at least
ALTER ANY DATABASEordb_owneron the affected database. - The name of the database that is closing (from the error message or the application log).
- Permission to view SQL Server error logs and, if needed, to change database properties.
- For a restore or detach that is stuck, a maintenance window: some steps may require exclusive access and can interrupt users.
Menu labels in SSMS and the SQL Server error log can differ slightly by version and by whether you use the desktop app or Azure Data Studio; the T-SQL commands below are the stable part.
Step-by-Step
- Confirm the database state. Run this query and look at the
state_desccolumn for the affected database:SELECT name, state_desc FROM sys.databases WHERE name = 'YourDatabase';
If it saysOFFLINE,RESTORING,RECOVERING, orCLOSING(where reported), you know which path to take. If it saysONLINE, the error was probably transient and caused by a connection racing a close; continue to step 2 to stop it recurring. - Stop the operation that is closing it. If a person or job is running
ALTER DATABASE ... SET OFFLINE, a detach, or a restore, let it finish or cancel it. For a restore, cancel the restore job in SSMS Object Explorer (right-click the database and stop the restore) or kill the session running it. Do not kill sessions blindly; checksys.dm_exec_requestsandsys.dm_exec_sessionsfor the database name first. - Bring the database online. If it is offline, run:
ALTER DATABASE YourDatabase SET ONLINE;
If it is stuck in recovery, wait for recovery to complete; forcing it can corrupt data. If it is inRESTORINGbecause a restore was interrupted, either restart the restore from the last full backup or runRESTORE DATABASE YourDatabase WITH RECOVERY;only if you are sure you do not need more log backups. - Check for auto-close. Auto-close shuts a database down when the last user leaves, which is a common trigger for error 1414 in applications that reconnect quickly. Run:
SELECT name, is_auto_close_on FROM sys.databases WHERE name = 'YourDatabase';
If the value is 1, turn it off:ALTER DATABASE YourDatabase SET AUTO_CLOSE OFF;
This is the single most effective long-term fix for recurring 1414 errors on low-traffic databases. - Check for auto-shrink and other background tasks. Auto-shrink can move files and briefly take the database busy. Run
SELECT name, is_auto_shrink_on FROM sys.databases WHERE name = 'YourDatabase';and disable it withALTER DATABASE YourDatabase SET AUTO_SHRINK OFF;if it is on. Also check whether a maintenance plan, index rebuild, or backup job overlaps the time the error appears. - Review the SQL Server error log. In SSMS, open Object Explorer, expand Management, then SQL Server Logs, and open the current log. Look for messages around the time of the error that mention the database name: “Setting database option OFFLINE”, “Database detached”, “Starting restore”, or “Auto-close”. This tells you which operation to prevent next time.
- Check availability groups and mirroring. If the database is in an availability group, a failover or a role change can close it briefly on one replica. Check the AG dashboard in SSMS (Always On High Availability > Availability Groups) and the SQL Server error log for role-change messages. Do not force failover to “fix” 1414; let the role change complete.
- Reconnect and verify. After the database is online and auto-close is off, reconnect from the application and run a simple query such as
SELECT 1;against the database. Then runDBCC CHECKDB (YourDatabase) WITH NO_INFOMSGS;during a quiet period if the database was forcibly closed, to confirm it is healthy.
Common Mistakes
- Restarting SQL Server immediately. A restart can interrupt a restore or recovery and make the problem worse. Check the state first.
- Killing sessions without checking what they are. A session running a restore or a detach is not the same as a normal user query; killing it can leave the database in
RESTORINGorRECOVERY_PENDING. - Ignoring auto-close. Many people fix the immediate error but leave auto-close on, so the error returns the next time the last connection drops.
- Assuming the error is always on the database you are querying. Error 1414 can name a database that a login trigger, a job step, or an application startup routine touches before your query runs.
- Forcing recovery on a database that is still restoring. Only use
WITH RECOVERYwhen you are certain no further log backups are needed.
Tips
- Keep auto-close off on all user databases; it is intended for desktop-style embedded databases, not for application databases.
- Schedule restores, index maintenance, and backups so they do not overlap with application startup or peak connection times.
- Use a dedicated login for application connections and avoid running
ALTER DATABASE ... SET OFFLINEfrom scripts that can run while users are connected. - If the error appears only at application start, look for a startup stored procedure or connection string that opens the database before the instance has finished recovery.
- Keep the SQL Server error log cycle short enough that you can see recent events, and check it after any unexpected 1414.
Troubleshooting
If the database will not come online: check sys.databases again and read the error log for the exact reason (missing file, permission problem, restore incomplete). A database that is RECOVERY_PENDING usually needs its files checked or a restore, not another SET ONLINE.
If 1414 keeps returning: confirm auto-close is off, then look for a job or application that opens and closes connections rapidly. Connection pooling usually prevents this, but a misconfigured pool with a very short lifetime can cause repeated last-connection closes.
If the error names a different database than expected: search the error log for that name and check for logon triggers, startup procedures, or linked-server queries that touch it.
If a restore is stuck: do not detach or drop the database. Cancel the restore cleanly, verify the backup file is accessible, and restart the restore. If the database is in RESTORING and you need it online, run RESTORE DATABASE ... WITH RECOVERY only after confirming you have all required log backups.
If you suspect corruption after a forced close: run DBCC CHECKDB and restore from backup if errors are reported. Do not run repair options without a verified backup.
Frequently Asked Questions
What does SQL Server error 1414 mean exactly?
It means a request tried to use a database while SQL Server was in the process of closing it, for example during an offline, detach, restore, or auto-close operation.
Is error 1414 dangerous?
Usually not. It is a timing error. The risk comes from what caused the close: a forced restore or a killed session can leave the database needing recovery.
Can auto-close cause error 1414?
Yes. With auto-close on, the database closes when the last connection ends, and a new connection arriving at that moment can get 1414. Turning auto-close off prevents it.
Do I need to restart SQL Server to fix it?
No. In most cases you bring the database online with ALTER DATABASE ... SET ONLINE and disable auto-close. Restart only if the instance itself is unresponsive.
Will this affect other databases on the same instance?
No, unless the closing operation was instance-wide, such as a service restart. The steps above target one database at a time.
What if the database is in an availability group?
Let the role change finish and check the AG dashboard. Forcing failover during a role change can cause more errors; if the AG is unhealthy, follow the vendor's AG troubleshooting guidance.