Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot Drop Database after Unit Test

ANSWER: I wasn't clearing my ConnectionPools.

The solved scenario is:

SetUp:

  1. Check to see if the 'TEMP_NUnit' database (SQL Server 2005) exists on the local instance
  2. If it exists, drop it.
  3. Create a new blank database named 'TEMP_NUnit'.

Test:

  1. Instantiate the 'Upgrade Database' component.
  2. Check that essential properties of component are defaulting correctly.
  3. Point the component at the blank database and call .Upgrade()
  4. Check that the upgrade actually worked

TearDown:

  1. SqlConnection.ClearAllPools();
  2. Drop the 'TEMP_NUnit' database.

Original question is below the fold.


Hi All

The test scenario is:

SetUp:

  1. Check to see if the 'TEMP_NUnit' database (SQL Server 2005) exists on the local instance
  2. If it exists, drop it.
  3. Create a new blank database named 'TEMP_NUnit'.

Test:

  1. Instantiate the 'Upgrade Database' component.
  2. Check that essential properties of component are defaulting correctly.
  3. Point the component at the blank database and call .Upgrade()
  4. Check that the upgrade actually worked

TearDown:

  1. Drop the 'TEMP_NUnit' database.

Everything's going fine until I get to the TearDown phase. I always get the following error:

Cannot drop database "TEMP_NUnit" because it is currently in use.

This is confusing me, because I'm closing (explicitly calling conn.Close in finally statements) and disposing (via using statement) all my DbConnection objects correctly. There shouldn't be anything running on the "TEMP_NUnit" database by the time the TearDown comes around.

If I close NUnit and then re-open it, step 2 of the SetUp always works first go.

I'm not sure what I'm doing wrong here. Any pointers in the right direction would be appreciated.

like image 334
Daniel Schealler Avatar asked Aug 16 '10 03:08

Daniel Schealler


People also ask

Which database Cannot be dropped?

System databases cannot be dropped.

How do I force drop a database in SQL Server?

Connect to SQL Server and right-click the database you want to remove. Click delete command and the following screen will appear. Click OK to remove the database (in this example, the name is Testdb as shown in the above screen) from MS SQL Server.

How do I drop an entire database?

To delete a database In Object Explorer, connect to an instance of the SQL Server Database Engine, and then expand that instance. Expand Databases, right-click the database to delete, and then click Delete. Confirm the correct database is selected, and then click OK.

Should unit tests hit database?

Unit tests should never connect to a database. By definition, they should test a single unit of code each (a method) in total isolation from the rest of your system. If they don't, then they are not a unit test.


1 Answers

Ah, but you forgot to do this little thing before dropping the database:

SqlConnection.ClearAllPools();

By default the connection pool will maintain a connection to the database, even if you closed it before. Doing SqlConnection.ClearAllPools() will actually force all connections to be closed. You can then connect to master and drop the temp database.

It has caused me quite an amount of grief before :)

like image 60
Igor Zevaka Avatar answered Sep 28 '22 03:09

Igor Zevaka