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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:27:52+00:00 2026-06-12T12:27:52+00:00

I am currently implementing a databasehalper class which will be accessed from various threads

  • 0

I am currently implementing a databasehalper class which will be accessed from various threads so it’ll basically be singleton and accessed like this :

               public class HistoryDatabaseHelper extends SQLiteOpenHelper{
                private static HistoryDatabaseHelper mInstance=null;
                public static HistoryDatabaseHelper getInstance(Context context){
            if(mInstance==null)
              mInstance=new HistoryDatabaseHelper(context.getApplicationContext());
            return mInstance;
                }

So there is no public constructor and this is the only way of accessing the helper .

Now my question is that should i open the database everytime i try to perform any operation . For example consider the following menmber function of the helper class :

           public void deleteContact(String staticUrl) {
           SQLiteDatabase db = this.getWritableDatabase();  // does this open a new connection each time?
                                                            //  and invade the whole purpose of 
                                                             //creating singleton helper
           db.delete(TABLE_DOWNLOAD_HISTORY, KEY_STATIC_URL + " = ?",
           new String[] { staticUrl });
           db.close();
                 }

So is this is the proper way or should i open the database once like this :

            public static HistoryDatabaseHelper getInstance(Context context){
          if(mInstance==null){
               mInstance=new HistoryDatabaseHelper(context.getApplicationContext());
               mInstance.open();  // here
                           }
               return mInstance;
                   }

if so then where should we close the database ?

  • 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-12T12:27:53+00:00Added an answer on June 12, 2026 at 12:27 pm

    Stop! Wait! Freeze!

    “I am currently implementing a databasehalper class which will be accessed from various threads so it’ll basically be singleton and accessed like this : “

    You say your HistoryDatabaseHelper will be accessed from various threads and you forget to synchronize the getInstance method? Your threads are going to wear their running shoes before they leave home since they are definitely up for a race condition. You need to synchronize your getInstance methods if you want your threads to behave like grown ups. Alternately, you can instantiate mInstance instance variable during declaration itself and avoid the need to synchronize getInstance or check whether mInstance has already been initialized inside getInstance.

    Next up is the HistoryDatabasrHelper constructor. You need to explicitly declare a private HistoryDatabaseHelper constructor since the compiler will place a public constructor in your class otherwise. Not doing this is like letting your neighbour’s steal from you and not handing them over to the cops.

    Last but not the least is the Context parameter for the getInstance method. Remove this parameter. Let your HistoryDatabasrHelper have a Context instance variable and initialize it within your private constructor assuming that the Context is not going to change from one instance to another.

    So should you have a singleton database connection? I personally think it would be a better idea if you had a singleton database connection pool instead of a singleton database connection. A Google search will give you a lot of good libraries for connection pooling out there.

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

Sidebar

Related Questions

I am currently implementing cache. I have completed basic implementation, like below. What I
I'm currently implementing some code that should, for each method of a class, run
** I am currently implementing fancy URLs to see if these 'solves' this. eg
I'm currently implementing a histogram that will show a very large scale data using
I am currently implementing S3 integration into my Rais application which uses Paperclip. Currently,
I am currently implementing an associacion of strings and enums based on this suggestion
I am currently implementing a Deck class that represents a 52 card playing deck.
I am currently implementing a Japanese dictionary and would like some ideas on how
I am currently implementing a program that requires me to handle threads and process.
I am currently implementing a blog-like site using Ruby on Rails. Each Post has

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.