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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:23:46+00:00 2026-05-12T18:23:46+00:00

There is this DB-connection-like object that my web application will need. It is pretty

  • 0

There is this DB-connection-like object that my web application will need. It is pretty slow to create and will be used rarely, so I would like to keep only a single instance of it around. If several requests need it at the same time, they will lock() the object and serialize their access.

To make things more fun the object is an IDisposable and can be closed. Naturally, I’ll avoid writing code that closes it, but… you know… better safe than sorry, right?

So, the naïve approach would be to implement it something like this:

private static DBClass _Instance;
private static object _DBLock = new object();

public DBClass GetDB()
{
    if ( _Instance == null )
        lock (_DBLock )
            if ( _Instance == null )
                _Instance = CreateInstance();
    lock (_Instance)
        if ( _Instance.Disposed )
            _Instance = CreateInstance();
    return _Instance;
}

It will then be used like:

lock (var Conn = Global.GetDB() )
{
    // Use Conn here.
}

This will probably work most of the time, but I see an opening that could be exploited – if two threads call this at the same time, they get the same instance at the same time, and then one could still Dispose() it before the other acquires a lock() on it. Thus – later code fails. Checking for Disposed at every place that uses it also seems awkward. In fact, lock()ing it seems awkward too, but the instance isn’t inherently thread-safe so there’s nothing I can do about it.

How would you implement this?

  • 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-12T18:23:47+00:00Added an answer on May 12, 2026 at 6:23 pm

    For a start, I’d suggest avoiding double-checked locking. It’s very easy to get it wrong – as you’ve done in this case, by not making the variable volatile.

    Given that you don’t want to actually dispose the object, is there any reason not to wrap it in something that implements the same API but without exposing disposal? That way you could also encapsulate the locking, potentially, depending on how you actually use the object. Another way of doing this would be to expose an API in terms of Action<DBClass> – you pass in the action you want to take with the object, and the helper takes care of creating the instance and locking appropriately.

    One final point: all of this feels somewhat tricky in terms of testability. I don’t know whether you’re a fan of unit testing etc, but singletons often make life somewhat awkward for testing.

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

Sidebar

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.