/** Verify the database exists and is open. */ /* package */ synchronized boolean ensureDatabase() { if (mDb != null && mDb.isOpen()) { return true; } // Sometimes retrieving the database fails. We do 2 retries: first without database deletion // and then with deletion. SQLiteException lastSQLiteException = null; for (int tries = 0; tries < 2; tries++) { try { if (tries > 0) { deleteDatabase(); } mDb = getWritableDatabase(); break; } catch (SQLiteException e) { lastSQLiteException = e; } // Wait before retrying. try { Thread.sleep(SLEEP_TIME_MS); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); } } if (mDb == null) { throw lastSQLiteException; } // This is a sane limit to protect the user from the app storing too much data in the database. // This also protects the database from filling up the disk cache and becoming malformed // (endTransaction() calls will throw an exception, not rollback, and leave the db malformed). mDb.setMaximumSize(DEFAULT_MAX_DB_SIZE); return true; }