Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

database - Android SQLite leaked Error

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

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

1 Reply

0 votes
by (71.8m points)

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...