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

  • Home
  • SEARCH
  • 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 93263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:19:01+00:00 2026-05-10T23:19:01+00:00

As part of our unit tests, we restore a blank database when the tests

  • 0

As part of our unit tests, we restore a blank database when the tests start . The unit tests then perform their tests by calling web services (hosted in the Visual Studio ASP.NET host).

This works fine for us the first time the unit tests are run, however if they are re-run without restarting the web services, an exception is raised as all the connections have been reset as part of the restore.

The code below simulates what occurs:

static void Main(string[] args) {     DoDBStuff();     new Server('localhost').KillAllProcesses('Test');     DoDBStuff(); }  private static void DoDBStuff() {     string constr = 'Data Source=localhost;Initial Catalog=Test;Trusted_Connection=Yes';     using (SqlConnection con = new SqlConnection(constr))     {         con.Open();         using (SqlCommand cmd = new SqlCommand('SELECT TOP 1 id FROM sysobjects', con))         {             int? i = cmd.ExecuteScalar() as int?;         }     } } 

All the code, except for the KillAllProcesses runs in the web service’s process, while the KillAllProcess runs in the Unit Test process. The same could be achieved by restarting the SQL server.

The problem we face is the webservices doesn’t know when the connection is killed, and just picks a ‘bad’ connection from the connection pool. Further, the creation of a connection and the execution of the command are several layers apart within the app.

How can we detect that a connection is ‘bad’ before executing a command, without drastically affecting the performance of the application?

  • 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. 2026-05-10T23:19:01+00:00Added an answer on May 10, 2026 at 11:19 pm

    After killing SQL Server, your connection pool will contain stale connections to the old instance of SQL Server.

    You can call SqlConnection.ClearAllPools() to clear the stale connections from the pool after restarting SQL Server, e.g.:

    static void Main(string[] args) {         DoDBStuff();         new Server('localhost').KillAllProcesses('Test');     SqlConnection.ClearAllPools();     DoDBStuff(); } 

    NB The ClearAllPools method was introduced in .NET 2.0

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

Sidebar

Ask A Question

Stats

  • Questions 68k
  • Answers 68k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer In terms of your procedure you seem safe as the… May 11, 2026 at 12:15 pm
  • added an answer A real-world example would be a FIFO queue. An simple… May 11, 2026 at 12:15 pm
  • added an answer According to the spec you cannot have an interface that… May 11, 2026 at 12:15 pm

Related Questions

As part of our unit tests, we restore a blank database when the tests
As part of our current database work, we are looking at a dealing with
As part of our build process we run a database update script as we
We have a site that needs to (as part of our process) generate a
We've used the no-longer-supported RichTextBox control as part of our (ASP.NET-based) CMS for a
As part of some error handling in our product, we'd like to dump some
We are running part of our app as a windows service and it needs
Our installation program is written using InstallShield2009, and as part of certification requirements we
Our projects are typically stored as single projects, and are not part of a
As part of a larger web-app (using CakePHP), I'm putting together a simple blog

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.