Android的SQLite的错误泄露

Android的SQLite的错误泄露

本文介绍了Android的SQLite的错误泄露的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的数据库连接设置在应用程序中,但LogCat中保持的向我讲述了一个SQLite泄漏

  11月4日至25日:22:23.771:W / SQLiteConnectionPool(9484):对数据库中的对象SQLiteConnection'+数据+数据+ com_appstart +数据库+数据库'被泄露!请修正您的应用程序正常结束正在进行的交易,并关闭数据库时不再需要它。

我开始怀疑它是否已经降到了我如何使用数据库适配器。
我附上我的code ...
这是我的code其中一些时候,我发现错误..

  {尝试    DBAdapter DBA =新DBAdapter(本);
    dba.open();    光标C = dba.getModules(Constant.LANGUAGE_ID);    的for(int i = 0; I< c.getCount();我++){
        如果(ⅰ→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();
}赶上(例外五){
    e.printStackTrace();
}

这是包含的open()和close()方法。

我DBAdapter类

 公共DBAdapter的open()抛出的SQLException {
    DB = DBHelper.getWritableDatabase();
    返回此;
}
公共无效的close(){
    DBHelper.close();
}


解决方案

什么是你使用这个DBAdapter类?我不知道它在做什么,所以我不知道这是否是正确的。您应该检查哪里获得SQLiteConnection对象错误消息指,并确保该SQLiteConnection接近()D。

您得到的错误只是偶尔或所有的时间?这可能不是你所观察的主要问题,但你的code也不能接近致电()时,有在收盘前一个例外()。您应该确保close()方法通过用try / finally块守着它就会不顾异常路径名为:

  dba.open();
尝试{
  光标C = ...;
  尝试{
    ...
  } {最后
    c.close();
  }
} {最后
  dba.close();
}

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();
}
解决方案

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

这篇关于Android的SQLite的错误泄露的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 23:52