-1

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();
}
laalto
  • 150,114
  • 66
  • 286
  • 303
M D
  • 47,665
  • 9
  • 93
  • 114

4 Answers4

1

Your problem is that you have to "Close" the database once you finished your database operation. So close your DB wherever you opened your DB.

Subburaj
  • 5,114
  • 10
  • 44
  • 87
  • i used DBAdapter class for Database connectivity and this class contain Close and Open method. so, when i need any Database table i open this adpater and after my used i close this adapter.but some times i got this error and No data would found. – M D Apr 25 '13 at 06:05
  • Make sure that you closed DB for all open?? otherwise You post your code i can check it and if possible i will help you.. – Subburaj Apr 25 '13 at 06:07
  • i used first time my Adapter in this way.i first open database through DBAdapter.Open() and after i got data i close database in this way DBAdapter.Close(). – M D Apr 25 '13 at 06:12
  • K..The above code only u have in your adapter class..Or u have some other code?? – Subburaj Apr 25 '13 at 06:16
1

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();
}
Tobias
  • 1,107
  • 7
  • 8
1

in DBAdapter class, in close function replace DBHelper.close() from db.close();

public void close() {
    DBHelper.close();
}

replace with the below;

public void close() {
    db.close();
}
KumailR
  • 505
  • 1
  • 4
  • 20
0
public DBAdapter open() throws SQLException {
    db = DBHelper.getWritableDatabase();
//You should close the opened database
DBhelper.close
    return this;

the same also goes for cursor once you opened it it needs to be closed.