9

I'm trying to delete something from a database then insert a new value. I don't know much about databases, so would appreciate advice on whats going wrong here.

I keep getting the following error:

Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase:

Its called from an async task, works fine the first time, but breaks the second time that the task is triggered.

Here are the important snippets:

 public void removeAndInsert(String configRaw) {
    SQLiteDatabase sqlite = null;
    try {
        sqlite = dbHelper.getWritableDatabase();
        removeAndInsert(configRaw, sqlite);
    .....
    finally {
        if (sqlite != null) {
            sqlite.close();
        }
    }



void removeAndInsert(String configRaw, SQLiteDatabase sqlite) throws SQLException {
    ContentValues initialValues = new ContentValues();
    initialValues.put(DBOpenHelper.CONFIG_CACHE_RAW_DATA, configRaw);
    sqlite.delete(DBOpenHelper.CONFIG_CACHE_TABLE_NAME, null, null);
    sqlite.insert(DBOpenHelper.CONFIG_CACHE_TABLE_NAME, null, initialValues);
}
4
  • 1
    well this part of code looks right, are you closing somewhere else in the code ? your dbHelper class for instance?
    – Guru
    Sep 26, 2013 at 17:17
  • 2
    Okay, I found the issue, its seems that something somewhere else in the app was opening and closing the database on another thread!
    – serenskye
    Sep 27, 2013 at 9:47
  • @serenskye Please edit your question (or post an answer) to include the details of your solution.
    – acj
    Sep 27, 2013 at 12:43
  • possible duplicate of attempt to re-open an already-closed object: SQLiteDatabase
    – rds
    May 16, 2014 at 22:06

3 Answers 3

15

Try this.

public void removeAndInsert(String configRaw) {
  SQLiteDatabase sqlite = null;
  try {
    sqlite = dbHelper.getWritableDatabase();
    synchronized(sqlite) {
      removeAndInsert(configRaw, sqlite);
    }
  .....
    finally {
    if (sqlite != null && sqlite.isOpen()) {
        sqlite.close();
    }
}

void removeAndInsert(String configRaw, SQLiteDatabase sqlite) throws SQLException {
  ContentValues initialValues = new ContentValues();
  initialValues.put(DBOpenHelper.CONFIG_CACHE_RAW_DATA, configRaw);
  sqlite.delete(DBOpenHelper.CONFIG_CACHE_TABLE_NAME, null, null);
  sqlite.insert(DBOpenHelper.CONFIG_CACHE_TABLE_NAME, null, initialValues);
}
6
  • The error doesn't occur on the closing line, the problem occurs when removeAndInsert is used for the second time
    – serenskye
    Sep 27, 2013 at 9:22
  • @serenskye I changed the example above to include synchronizing the call on the Database object.
    – DroidT
    Sep 27, 2013 at 13:33
  • synchronized(value) ? what is it ?, asynctask ?
    – user3402040
    Jul 29, 2015 at 15:18
  • I used method public synchronized void insert() //form correct... and not work this solution, I never used synchronized(sqlite) // never
    – user3402040
    Jul 29, 2015 at 15:35
  • isOpen(): The method isOpen() is undefined for the type SQLLiteAdapter. I'm not able to find that method in the list. Sep 23, 2015 at 6:38
8

It looks like you're creating an SQLiteOpenHelper instance (dbHelper) somewhere and getting a writable database from it (sqlite), but then you close the database. If this is the last (or only) database reference that is open, then the entire database will be closed when you call sqlite.close().

You can safely remove the call to sqlite.close() in removeAndInsert(). When your AsyncTask is finished with its database work, call dbHelper.close() to clean up any dangling database references.

2
  • moving where close is called, does not change the result
    – serenskye
    Sep 27, 2013 at 9:20
  • 1
    Your app must be closing the database elsewhere, then, as @Guru noted. Without seeing more code, we can only speculate about the source of the problem.
    – acj
    Sep 27, 2013 at 12:42
4

try to replace sqlite.close(); with dbHelper.close();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.