Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've got my database connection setup in an Application but LogCat keep's telling me about a SQLite leak

04-25 11:22:23.771: W/SQLiteConnectionPool(9484): A SQLiteConnection object for database '+data+data+com_appstart+databases+database' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.

I'm starting to wonder if it is down to how I'm using the database adapter. I attach my code... This is my code where some times i found and error..

try{

    DBAdapter dba = new DBAdapter(this);
    dba.open();

    Cursor c = dba.getModules(Constant.LANGUAGE_ID);

    for (int i = 0; i < c.getCount(); i++) {
        if (i > 2) {

            a.add(c.getString(2));
            moduleid.add(c.getString(0));
            icon_name.add(c.getString(1));

            System.out.println(c.getString(2) + "------" + c.getString(0));

        }
        c.moveToNext();
    }

    c.close();
    dba.close();
}catch(Exception e){
    e.printStackTrace();
}

this is my DBAdapter class that contains Open() and Close() methods.

public DBAdapter open() throws SQLException {
    db = DBHelper.getWritableDatabase();
    return this;
}
public void close() {
    DBHelper.close();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
263 views
Welcome To Ask or Share your Answers For Others

1 Answer

What is this DBAdapter class you're using? I don't know what it's doing so I don't know if it's correct. You should check where the SQLiteConnection object is obtained that the error message refers to, and ensure that that SQLiteConnection is close()d.

Are you getting the error only occasionally or all the time? It's probably not the main problem you are observing, but your code also fails to call close() when there is an exception before the close(). You should ensure close() gets called regardless of exception path by guarding it with a try/finally block:

dba.open();
try {
  Cursor c = ...;
  try {
    ...
  } finally {
    c.close();
  }
} finally {
  dba.close();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...