7

I have a central database in my application that a few different activities need to access.

Should I share this object by making it static? Like for example in the activity that initializes the DB I do this:

protected static appDatabase db;

Then others can access it via FirstActivity.db.

Another option is to create private appDatabase db objects in every activity that needs it, but I suspect opening multiple db objects to access the same stored data may be wasteful.

However I don't know too much about java which is why I'm asking - what's the preferred way to do this, and why?

Thanks

6
  • In my application I doesnt use static on such objects. Maybe its prefered, but I haven't done it. Interesting to see whcih answers you get. Commented Jan 6, 2013 at 19:46
  • You can use a singleton? Commented Jan 6, 2013 at 19:55
  • I usually work with databases behind ContentProvider in my professional environment and have come to find that ensuring only one database connection exists at a time (using a singleton pattern as @fge suggests) helps me deal with some data consistency issues and avoid "sqlite misuse exceptions". I wonder how these argument hold in a non-ContentProvider environment and therefore upvoted your question. Maybe the person that answers your question can also mention how big performance benefits we can expect with multiple vs single connections? :D Commented Jan 6, 2013 at 20:02
  • Singleton sounds like a good way to go. I have this class "appDatabase" which is a custom SQLite wrapper I made specifically for my purposes. Turning that into a singleton seems like the right solution. Commented Jan 6, 2013 at 20:12
  • 1
    My blog post on this topic might help. Commented Jan 6, 2013 at 20:37

2 Answers 2

4

You can use singleton like this;

    private static DataHelper singleton;

    public static DataHelper getDataHelper(Context context) {
            if (singleton == null) {
                    singleton = new DataHelper(context);
                    OpenHelper openHelper = new OpenHelper(singleton.context);
                    singleton.db = openHelper.getWritableDatabase();
            }
            if(!singleton.db.isOpen()){
                    OpenHelper openHelper = new OpenHelper(singleton.context);
                    singleton.db = openHelper.getWritableDatabase();
            }
            singleton.context = context;
            return singleton;
    }

    private DataHelper(Context context) {
        this.context = context;
}

And call your singleton class like this;

public DataHelper dh;
this.dh = DataHelper.getDataHelper(this);
Sign up to request clarification or add additional context in comments.

Comments

1

I handle this situation using the Application class and synchronized database object management. Here is an example. The [synchronized] qualifier is key in a multithreaded app.

By definition the Application object is Singleton in Android.

public class App extends Application
{

private static App _instance;
private AppLocalStorage _localStorage;

@Override
public void onCreate()
{
    super.onCreate();

    _instance = this;
}


public static App getInstance()
{
        //Exposes a mechanism to get an instance of the 
        //custom application object.
        return _instance;
}

public synchronized AppLocalStorage getDatabase()
{
    if (_localStorage == null)
    {
        _localStorage = new AppLocalStorage(this);
    }

    return _localStorage;
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.