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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:15:07+00:00 2026-05-12T10:15:07+00:00

i Dispose an SqlConnection object, but of of course it isn’t really closed .

  • 0

i Dispose an SqlConnection object, but of of course it isn’t really closed. i need closed connection to not hold locks on database objects. How can i prevent closed connections from holding locks?


Explanation of the above sentance for those who don’t know:

  • When you close an ADO or ADO.NET connection, you aren’t actually severing the connection to SQL Server. The ADO/ADO.NET infrastructure keeps the connection around in case you want to use it again. The connections are kept lingering around in what’s called “The Connection Pool”.

  • After a few minutes of not being used, the connection will be actually closed. Although, not really. TCP/IP has it’s own method for keeping TCP connections open for a few more minutes (in the “CLOSE_WAIT” state). This is done in case you ask to open a TCP connection to the same IP:Port again. If so, it can use that already open TCP connection.

  • With connection pooling and SQL Server, the connection is still established to SQL Server. Each connection has a database context that it is sitting in. As long as a connection is sitting in that database: it holds a shared database (S-DB) lock on that database.

  • A Shared-Database lock simply means, “Don’t delete this database while i’m in it please.”

How can i prevent it from holding a shared lock on my database, while keeping the benefits of connection pooling?


My ad-hoc solution right now is every time a developer called Dispose:

connection.Dispose()

change it into a call to a global helper function:

Database.DisposeConnection(connection);

which changes the database context to master:

public static void DisposeConnection(SqlConnection connection)
{
    //Stop holding a database lock - in my database at least
    ADOHelper.ExecuteNonQuery(connection, "USE master");

    connection.Dispose();
}

It solves my immediate problem; closed connections aren’t holding a lock on my database.

But now i’m worried that connection pooling will have its brain scrambled – because i switched database contexts behind its back.


In case anyone didn’t know, or thought otherwise:

From the SDK:

Close and Dispose are functionally
equivalent.

  • 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-12T10:15:08+00:00Added an answer on May 12, 2026 at 10:15 am

    You can place the database in single user mode in order to restore it. IIRC, something like this…

    ALTER DATABASE TheDatabaseToRestore SET SINGLE_USER WITH  ROLLBACK IMMEDIATE;
    RESTORE DATABASE TheDatabaseToRestore 
    FROM DISK =N'Z:\Path\To\Backup\BackupFile';
    ALTER DATABASE TheDatabaseToRestore SET MULTI_USER;
    

    see: Obtain Exclusive Access to Restore SQL Server and/or Restore Database Backup using SQL for some more background.

    Edit: Single user mode is intended for backups and restores (which, on the non-express edition, is what I use it for regularly). Using it kicks out all other connections, and new connections cannot be made. I haven’t played with the various “WITH” options such as with “ROLLBACK IMMEDIATE”, but their usage seems straightforward.

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

Sidebar

Related Questions

I used to dispose elements of type : SqlConnection SqlDataAdapter DataSet etc. But i
I understand that if I instantiate a SqlConnection object, I am really grabbing a
I've inherited an application that uses a global database sqlconnection object in order to
According to this , Dispose() on a SqlConnection calls Close() , so you don't
Should I dispose GDI+ object before its creation? Is recommended to always Dispose GDI+
When i call object.Dispose() ; Will CLR immediately destroy the object from memory or
Why isn't HashAlgorithm.Dispose public? void IDisposable.Dispose() { this.Dispose(true); GC.SuppressFinalize(this); } I understand that it
Which pattern is better for SqlConnection object? Which is better in performance? Do you
If I do a conn.Dispose(); (where conn is an instance of a SqlConnection Class),
I need to get values based on previous dropdownlist value. But I am confused

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.