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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:37:43+00:00 2026-06-09T11:37:43+00:00

Since I found no question covering this topic, I thought I’d share my solution

  • 0

Since I found no question covering this topic, I thought I’d share my solution to the following scenario. The answer may be obvious, but I took the long route to find out. 🙂 I’d appreciate feedback to both the question and answer as well as other solutions.

Scenario:

Suppose you have a multi-threaded program and want a database connection (or some other shared object) for some functionality in your program while other parts of your program don’t need it at all. There should only ever be one connection to the db though.

At the same time, you want to detect db connection loss and try to reconnect on the fly.

To cover that, you implement a lazy load pattern “getter” that also checks connection validity before returning the connection object.

Your code may look like this:

public class Main {
  private DB _db;

  public static void main(String[] args) {
    new Main().start();
  }

  private void start() {
    // Program code goes here
    // You create several threads, some of which may call getDB() whenever they need DB access
  }

  public DB getDB() {
    if (_db == null) {
      _db = getDBConnection();
    } else if (!_db.isConnectionValid()) {
      /*
       * DB connection is not valid anymore. Let's close it and
       * try to get a new connection.
       */
      _db.close();
      _db = getDBConnection();
    }

    return _db;
  }

  private DB getDBConnection() {
    DB db;

    // Obtain a new connection...
    ...

    return db;
  }
}

The problem:

Several threads may try to obtain a db connection at nearly the same time. It is even possible that several connects co-exist when some classes keep references to them.

  • 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-06-09T11:37:44+00:00Added an answer on June 9, 2026 at 11:37 am

    Several threads may try to obtain a db connection at nearly the same time. It is even possible that several connects co-exist when some classes keep references to them.

    In that case you need a pool as you can obtain multiple different instances. There are many DatabaseConnection pools available and some JDBC drivers have their own. I suggest you use the one which comes with the JDBC driver or use C3P0 or the like to act as Database connection pool.

    More specifically you need to take a connection (not just get it) in a way that another thread cannot get the same connection. A trivial example is to use a Queue.

    private final Queue<DB> freeDBs = new ConcurrentLinkedQueue<>();
    
    public DB acquireDB() {
        DB db = freeDBs.poll();
        if (db != null && db.isConnectionValid()) 
            return db;
        if (db != null)
            db.close();
        return getDBConnection();
    }
    
    public void release(DB db) {
        if (freeDBs.size() >= MAX_FREE_SIZE)
            db.close();
        else
            freeDBs.add(db);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, since the last question, I found that my solution is to duplicate the
I have found this question but it was written in 2009, presumably since then
I found this question , which has an answer, but facebook changed the token
I found this question here on SO and the solution is pretty simple: jsfiddle:
In the article Choosing Index Keys at SQLServerpedia I found the following lines: Since
I am trying to solve a little problem since yesterday, but no solution found
There's probably a Google search that'll answer this question but for the life of
Following this question I decided to use ffmpeg to crop MP3s. On another question
Sorry for the probably stupid question. Since I found nothing about it on the
I found this question .jar to .cs class/file transform technique or utility? which also

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.