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 .
Asked
Active
Viewed 148 times
0
-
You don't need to close the DB. – Ashok Kumar Sep 10 '19 at 12:39
-
check this https://stackoverflow.com/questions/8888530/is-it-ok-to-have-one-instance-of-sqliteopenhelper-shared-by-all-activities-in-an – Ankit Sep 10 '19 at 12:44
1 Answers
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