0

It is thread safe to use single instance of database by all activities of my Applications. And if i do so, when should i close the database connection. Please recommend the right method to use SqliteOpenHelper .

P L DHIMAN
  • 59
  • 4

1 Answers1

0

I've been using an approach I found in an online course and haven't had a slightest issue with it. Just put a synchronized keyword for your database instance so that multiple threads could manage it at the same time. This is what I am using:

synchronized static AppDatabase getInstance(Context context) {
    if (instance == null) {
        instance = new AppDatabase(context.getApplicationContext());
    }
    return (instance);
}
Oleh Romanenko
  • 289
  • 1
  • 11