I’m playing about with some DB connections via WinForms and a WebService and I’ve found that when using the web service the connections are not closed. This is causing a glitch in my automated test because I destroy and recreate the database bewteen tests so I always have the same starting state for each test.
The actual data access code for the call is the same in both cases, for example:
using (DataHandler handler = new DataHandler(Config.ConnectionString()))
{
return handler.GetDbVersion();
}
The DataHandler uses a data context (created using SqlMetal) to load an integer from a DB table and return it.
The Dispose method of the DataHandler closes up the connection and data context:
public void Dispose()
{
_data.Connection.Close();
_data.Connection.Dispose();
_data.Dispose();
}
When running directly from the unit test – this runs through and afterwards there are no connections left on the DB.
select spid, status, loginame, hostname, blocked, db_name(dbid), cmd from master..sysprocesses where db_name(dbid) = 'TEST_DATABASE'
Then I wrap this in a [WebMethod] and put it on an IIS7 instance:
[WebMethod]
public int GetDatabaseVersionNumber()
{
using (DataHandler handler = new DataHandler(Config.ConnectionString()))
{
return handler.GetDbVersion();
}
}
And the test calls the web serivce:
int dbVersion = _webService.GetDatabaseVersionNumber();
The web service creates the data context – gets the number – calls Dispose and then returns the number. However the connection is still ‘Sleeping’ in the DB. This means that when I try to destroy and recreate the database for the next test, the drop database fails (Active connection).
select spid, status, loginame, hostname, blocked, db_name(dbid), cmd from master..sysprocesses where db_name(dbid) = 'TEST_DATABASE'
54 sleeping IIS APPPOOL\ASP.NET v4.0 PETE 0 TEST_DATABASE AWAITING COMMAND
I’m assuming this is some kind of connection pooling that IIS and SQL Server handle to reduce the overhead of all the opening and closing of connections from web requests (I guess I wouldn’t want to mess with this in the context of deploying a webservice)
However, for the tests, I would like to kill off this connection so I can rebuild my database.
What is the best way to handle this? (Is this something I need to set in IIS, or in the test db? Can I define it in the connection string so it happens on test only?)
For info
Solved by adding the following code to the test teardown method:
if (data.DatabaseExists())
{
data.ExecuteCommand("ALTER DATABASE TEST_DATABASE SET SINGLE_USER WITH ROLLBACK IMMEDIATE;");
data.ExecuteCommand("ALTER DATABASE TEST_DATABASE SET MULTI_USER;");
data.DeleteDatabase();
}
Update
I found that I can bypass this issue by adding “Pooling=false” to the connection string in the Unit Test App.Config. This seems to work much better than force closing the connections because when you force close them sometimes the db isn’t avaliable for the next test int he suite (you get a “no serice on the other end of the pipe” type error because SQL has gone away but .net thinks it’s still there).
You could always run an alter database command and set it to single user with rollback immediate. That should kill all coonections for you to delete the database, or whichever task you want.