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 6644413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:09:52+00:00 2026-05-26T00:09:52+00:00

I’m playing about with some DB connections via WinForms and a WebService and I’ve

  • 0

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).

  • 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-05-26T00:09:52+00:00Added an answer on May 26, 2026 at 12:09 am

    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.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.