Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9117971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:01:33+00:00 2026-06-17T05:01:33+00:00

I am currently building unit tests that tests if some classes are editing a

  • 0

I am currently building unit tests that tests if some classes are editing a sql server 2005 database correctly. To do this I have created a small subset of data from our production and are storing it as a backup file. Whenever a unit test needs to be sure it has a clean database state it calls a restore routine that basically call the following sql:

RESTORE DATABASE database FROM DISK = 'c:\test\backup.bak' WITH REPLACE, NORECOVERY

This normally works, and has decent speed. When I say normally it’s because sometimes the database gets stuck in ‘restore’ mode result in error messages that looks like this (assuming it’s a Alter command):

ALTER DATABASE is not permitted while a database is in the Restoring state.

This means that if the first fail, every test fails. I can get the database out of it stuck state, but it’s rather irritating to do it every single time, it’s very time consuming and the whole point of unit testing was so I didn’t need anything when I activate the test.

I tried to see if I could avoid it by the RESTORE syntax, but can’t kind any options or flags that sound like it could resolve the issue.

Is there any other method I could use that is a lot safer that the RESTORE command?

Update:
I discovered that the restore fail happens mostly when a connection was still active. I usually handled that issue with the following structure:

ALTER DATABASE MyDatabase SET Single_User WITH Rollback Immediate

-- Restore logic here

ALTER DATABASE MyDatabase SET Multi_User

The problem with this is that it only limits all the connections to one. Sometimes one of the connections that is forced to close jumps over to the only one free, disrupting the restore and making it fail (or that is what I can gather from the documents and pages I have been reading).

I am instead trying to do the following SQL command before I start on my restore logic:

Declare @spid int
Select @spid = min(spid) from master.dbo.sysprocesses
where dbid = db_id('Mydatabase')
While @spid Is Not Null
Begin
        Execute ('Kill ' + @spid)
        Select @spid = min(spid) from master.dbo.sysprocesses
        where dbid = db_id('Mydatabase') and spid > @spid
End

-- Do Restore Logic

http://geekswithblogs.net/AngelEyes/archive/2010/02/24/kill-connections-to-resote-db—sql-server.aspx

This section of code goes in and actively closes all connections, which gives the restore logic room to execute. I am, however, not entirely sure that this is the best way to go, simply because it does not prevent connections to reestablish, potentially disrupting the restore process again. But so far I haven’t seen evidence that this has happened yet.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:01:35+00:00Added an answer on June 17, 2026 at 5:01 am

    By using usr’s suggestion and scripting my way through the restore process, I got it working. This answer is to close the question so visitor know I found the solution. The solution is described in the original question as an update.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Python project building with Hudson . Most unit tests work correctly,
We are building a new application in .net 3.5 with SQL server database. The
I'm currently building a unit test suite for my application, using QTestLib . It's
I am currently building a measurement unit convertion app for Windows Phone. After setting
Currently building an app that runs on mobile phones not related to the issue
I am currently building my project locally using MVC. The user enters some search
Some background I'm currently building a predominantly classic web app (rather than a single
I am currently building some application for iOS. It is actually my second application.
Im currently building a php framework... again. I have a class called config. its
I am currently building a C++ application that communicate via socket to a C#

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.