1

I have a class that does all the database access. It holds a reference to an SQLiteDatabase. The 30 or so functions look like this:

public boolean updateSomething(long a, int b, long c) {
    ContentValues values = new ContentValues();
    values.put(COL_A, a);
    ...
    return database.update(TABLE, values, COL_ID + "=?", new String[]{id}) > 0;
}

Many StackOverflows suggest to do it like this:

public boolean updateSomething(long a, int b, long c) {
    SQLiteDatabase database = dbHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(COL_A, a);
    ...
    boolean success = database.update(TABLE, values, COL_ID + "=?", new String[]{id}) > 0;
    database.close();
    return success;
}

but I also have functions that return cursors precisely because I need a cursor over the 10k rows of my database and can't just copy stuff to an array and close the database.

Others say to open the database in the onResume and close it onPause but that means to move the database code to the Activities or to have the database as a parameter in all these functions. Then again what do I do about AsyncTasks and other classes that run in parallel to my Activities.

My approach was to open the database in my main Activity and close it on destroy, then I realized it might be created multiple times so I counted the instances. Then I realized it might get destroyed while another Activity relies on having a database and that's where I'm stuck now. Application has no onStart/Stop callbacks that I could use to connect and close the database in.

In Android: How to close a cursor that returns from Class to Activity it is even suggested to not bother closing the database. Is that a wise move? I mean that's how I started and the LogCat got me to worry and do what is described above. What is the down side of never closing the database other than some log output?

Community
  • 1
  • 1
Giszmo
  • 1,944
  • 2
  • 20
  • 47
  • use content provider, cursor loaders, do not store data in array! use Plain cursor (especially when you working with listviews) ... with content provider you can only send uri between activites ... it will up to next activity to ask DB (through CP) see my and zapl comments in one of the answer here: http://stackoverflow.com/questions/13586262/sqliteopenhelper-vs-contentprovider – Selvin Feb 06 '13 at 14:03
  • My app is API level 4 and these are supposed to be the last changes (bugfixes) before I go to API level 11 due to some libraries needed for other features. I'm not happy about dropping support for 37% of my existing and 25% of my new users and certainly will not do that to fix some bug that is reported twice per week. (Damn I had some less dramatic numbers in mind. Maybe the lib was not for 11 but 8 or so.) – Giszmo Feb 06 '13 at 17:17
  • cursor loaders, fragmets and other useful stuff from API 11 are aviable through support v4 library(which need only API 4) – Selvin Feb 06 '13 at 18:04
  • Thank you Selvin. Kind of embarrassing I'm not using it yet. :/ I will look into it. Much to catch up. – Giszmo Feb 06 '13 at 19:24

1 Answers1

0

How about a static singleton db access class that handles all your database jobs. Then you can call a static method on destroy (or when ever appropriate) to close the connection.?

nine stones
  • 3,264
  • 1
  • 24
  • 36
  • I do have a singleton db access class. When would it be appropriate to close the db? Currently all my efforts lead to running into a closed database when I did not expect it. Main.java has a static counter an closes the db onDestroy if the counter reached 0 but that seams to happen and is a problem when some Child.java gets opened directly without going through Main.java. I get 2 crash reports per week with 10k active installs but it bugs me to not get this right. – Giszmo Feb 06 '13 at 14:01
  • The static db access class then should handle all requests, so it can account for the ones coming from Child.j, too. – nine stones Feb 06 '13 at 14:05
  • Yes, sure but how does that help me to decide when to close the (only) database connection? – Giszmo Feb 06 '13 at 14:12
  • What I need is some Application.onDestroy that gets triggered with the last Activity/Service/... being destroyed. – Giszmo Feb 06 '13 at 14:14