patternsqlMinor
Why is my database stuck in the Restoring state?
Viewed 0 times
whythestuckrestoringdatabasestate
Problem
I have some unit tests that manipulates data in a database. In order to guarantee that the database state is always the same throughout all the tests, we're trying restore a
The restoration code looks like this:
After this, the tests connect to the database and do whatever they need to do.
The problem is that, during the tests debug, if eventually I need to hit the
Do you have any suggestions of what might be causing this?
EDIT
Complementing the @usr's response, to recover the database from the inconsistent state at the beginning of the tests, it's necessary to add the
It will work if it's like this:
database snapshot at the beginning of these tests. The restoration code looks like this:
USE Master
ALTER DATABASE {0} SET SINGLE_USER WITH ROLLBACK IMMEDIATE
RESTORE DATABASE {0} FROM DATABASE_SNAPSHOT = '{1}' WITH RECOVERY
ALTER DATABASE {0} SET MULTI_USERAfter this, the tests connect to the database and do whatever they need to do.
The problem is that, during the tests debug, if eventually I need to hit the
stop button to cancel the tests, the database is being left in the Restoring state forever. It's strange because it only happens when I stop the debug session. If I have 20 tests and all of them restores the snapshot prior to the test, I'll get no error during these test executions.Do you have any suggestions of what might be causing this?
EDIT
Complementing the @usr's response, to recover the database from the inconsistent state at the beginning of the tests, it's necessary to add the
REPLACE option to the restore statement.It will work if it's like this:
USE Master
ALTER DATABASE {0} SET SINGLE_USER WITH ROLLBACK IMMEDIATE
RESTORE DATABASE {0} FROM DATABASE_SNAPSHOT = '{1}' WITH RECOVERY, REPLACE
ALTER DATABASE {0} SET MULTI_USERSolution
If you abort a
Stopping the debugger kills the client process causing SQL Server to kill the connection and all associated sessions and requests.
To get it working, restart the last restore step that was interrupted. In your case, restore from snapshot again.
RESTORE mid-way the database is in an unusable state. This makes sense: Some pages are old, some are new.Stopping the debugger kills the client process causing SQL Server to kill the connection and all associated sessions and requests.
To get it working, restart the last restore step that was interrupted. In your case, restore from snapshot again.
Context
StackExchange Database Administrators Q#30313, answer score: 6
Revisions (0)
No revisions yet.