I need some help on database and cursor managing. I noticed that, when entering /leaving certain fragments, I get:
W/SQLiteConnectionPool﹕ A SQLiteConnection object for database '+data+data+database' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
That made me go back from scratch and check what I'm doing and when. I have:
- a
DatabaseHelper
class extendingSQLiteOpenHelper
, with just some methods for creating and updating the db; a
DatabaseManager
class, extending nothing. I use this, among other things, to keep a single reference to aDatabaseHelper
object:public class DatabaseManager { private DatabaseHelper h; //class extending SQLiteOpenHelper public DatabaseManager(Context c) { if (h==null) { h = new DatabaseHelper(c.getApplicationContext()); } public Cursor query(...) { SQLiteDatabase db = h.getReadableDatabase(); return db.rawQuery(...) } public void closeConnection() { SQLiteDatabase db = h.getWritableDatabase(); db.close(); h.close(); } }
in this class, some methods querying the database and returning a
Cursor
object;in this class, a
closeConnection()
method, which I'm not really sure of.
I use this class from fragments, calling each time new DatabaseManager(getActivity())
. This should not create a new helper reference. Right now I am:
- calling
Cursor.close()
as soon as I got the information I wanted from the query; - never calling
open()
on my helper neither on my SQLiteDatabase, although I read somewhere that should be done. When exactly? Why it all works even without calling it? - calling
manager.closeConnection()
in theonStop()
method of fragments that make use of my database. As you can see, that calls close onh
(a reference to the helper class) and on a readableSQLiteDatabase
object. However, I'm not really sure about that, because it closes the helper referenceh
without making it null, so maybe there are some problems with future calls tonew DatabaseManager()
? Maybe dealing with database with a singleton pattern does not require you to callh.close()
?
Apart from that, needless to say (that's why I'm asking), when switching through fragments I get the above mentioned warning. What's wrong? What should I do? What does end transactions in progress
mean? Should I modify my closeConnection()
method, call it in different lifecycle times, or don't call it at all?
After embarrassing issue pointed out by @Selvin, I made h
static. Now if I remove any call to closeConnection()
, it all works well and I don't get any warnings. That means I'm never calling neither h.close()
or db.close()
. Is that ok? If not, when should I call it?