/** * Copies the database file and any supporting journal or WAL files needed to open the DB to the given directory. * This method acquires the exclusive lock on the database before copying, which will prevent any other threads * from reading or writing to the database while the copying is in progress. If this method is called from within * a transaction, an exception will be thrown. This method is intended for debugging purposes only. * * @param toDir the directory to copy the database files to * @return true if copying the database files succeeded, false otherwise */ public boolean copyDatabase(File toDir) { acquireExclusiveLock(); try { return copyDatabaseLocked(toDir); } finally { releaseExclusiveLock(); } }
/** * Close the database if it has been opened previously. This method acquires the exclusive lock before closing the * db -- it will block if other threads are in transactions. This method will throw an exception if called from * within a transaction. * <p> * It is not safe to call this method from within any of the database open or migration hooks (e.g. * {@link #onUpgrade(ISQLiteDatabase, int, int)}, {@link #onOpen(ISQLiteDatabase)}, * {@link #onMigrationFailed(MigrationFailedException)}), etc. * <p> * WARNING: Any open database resources (e.g. cursors) will be invalid after calling this method. Do not call this * method if any open cursors may be in use. */ public final void close() { acquireExclusiveLock(); try { closeLocked(); } finally { releaseExclusiveLock(); } }
/** * Clear all data in the database. This method acquires the exclusive lock before closing the db -- it will block * if other threads are in transactions. This method will throw an exception if called from within a transaction. * <p> * It is not safe to call this method from within any of the database open or migration hooks (e.g. * {@link #onUpgrade(ISQLiteDatabase, int, int)}, {@link #onOpen(ISQLiteDatabase)}, * {@link #onMigrationFailed(MigrationFailedException)}), etc. * <p> * WARNING: Any open database resources (e.g. cursors) will be invalid after calling this method. Do not call this * method if any open cursors may be in use. The existing database file will be deleted and all data will be lost. */ public final void clear() { acquireExclusiveLock(); try { closeAndDeleteLocked(); } finally { releaseExclusiveLock(); } }
acquireExclusiveLock(); try { return other.attachTo(this);
/** * Clears the database and recreates an empty version of it. This method acquires the exclusive lock before closing * the db -- it will block if other threads are in transactions. This method will throw an exception if called from * within a transaction. * <p> * If called from within the {@link #onUpgrade(ISQLiteDatabase, int, int)} or * {@link #onDowngrade(ISQLiteDatabase, int, int)} hooks, this method will abort the remainder of the * migration and simply clear the database. This method is also safe to call from within * {@link #onMigrationFailed(MigrationFailedException)} * <p> * WARNING: Any open database resources (e.g. cursors) will be invalid after calling this method. Do not call this * method if any open cursors may be in use. The existing database file will be deleted and all data will be lost, * with a new empty database taking its place. * * @see #clear() */ public final void recreate() { if (isInMigration) { throw new RecreateDuringMigrationException(); } else if (isInMigrationFailedHook || databaseOpenFailedRetryCount > 0) { recreateLocked(); // Safe to call here, necessary locks are already held in this case } else { acquireExclusiveLock(); try { recreateLocked(); } finally { releaseExclusiveLock(); } } }
private String attachTo(SquidDatabase attachTo) { if (attachedTo != null) { throw new IllegalArgumentException( "Database " + getName() + " is already attached to " + attachedTo.getName()); } if (inTransaction()) { throw new IllegalStateException( "Cannot attach database " + getName() + " to " + attachTo.getName() + " -- " + getName() + " is in a transaction on the calling thread"); } acquireExclusiveLock(); String attachedAs = getAttachedName(); if (!attachTo.tryExecSql("ATTACH '" + getDatabasePath() + "' AS '" + attachedAs + "'")) { releaseExclusiveLock(); // Failed return null; } else { attachedTo = attachTo; return attachedAs; } }
/** * Copies the database file and any supporting journal or WAL files needed to open the DB to the given directory. * This method acquires the exclusive lock on the database before copying, which will prevent any other threads * from reading or writing to the database while the copying is in progress. If this method is called from within * a transaction, an exception will be thrown. This method is intended for debugging purposes only. * * @param toDir the directory to copy the database files to * @return true if copying the database files succeeded, false otherwise */ public boolean copyDatabase(File toDir) { acquireExclusiveLock(); try { return copyDatabaseLocked(toDir); } finally { releaseExclusiveLock(); } }
/** * Clear all data in the database. This method acquires the exclusive lock before closing the db -- it will block * if other threads are in transactions. This method will throw an exception if called from within a transaction. * <p> * It is not safe to call this method from within any of the database open or migration hooks (e.g. * {@link #onUpgrade(ISQLiteDatabase, int, int)}, {@link #onOpen(ISQLiteDatabase)}, * {@link #onMigrationFailed(MigrationFailedException)}), etc. * <p> * WARNING: Any open database resources (e.g. cursors) will be invalid after calling this method. Do not call this * method if any open cursors may be in use. The existing database file will be deleted and all data will be lost. */ public final void clear() { acquireExclusiveLock(); try { closeAndDeleteLocked(); } finally { releaseExclusiveLock(); } }
/** * Close the database if it has been opened previously. This method acquires the exclusive lock before closing the * db -- it will block if other threads are in transactions. This method will throw an exception if called from * within a transaction. * <p> * It is not safe to call this method from within any of the database open or migration hooks (e.g. * {@link #onUpgrade(ISQLiteDatabase, int, int)}, {@link #onOpen(ISQLiteDatabase)}, * {@link #onMigrationFailed(MigrationFailedException)}), etc. * <p> * WARNING: Any open database resources (e.g. cursors) will be invalid after calling this method. Do not call this * method if any open cursors may be in use. */ public final void close() { acquireExclusiveLock(); try { closeLocked(); } finally { releaseExclusiveLock(); } }
acquireExclusiveLock(); try { return other.attachTo(this);
/** * Clears the database and recreates an empty version of it. This method acquires the exclusive lock before closing * the db -- it will block if other threads are in transactions. This method will throw an exception if called from * within a transaction. * <p> * If called from within the {@link #onUpgrade(ISQLiteDatabase, int, int)} or * {@link #onDowngrade(ISQLiteDatabase, int, int)} hooks, this method will abort the remainder of the * migration and simply clear the database. This method is also safe to call from within * {@link #onMigrationFailed(MigrationFailedException)} * <p> * WARNING: Any open database resources (e.g. cursors) will be invalid after calling this method. Do not call this * method if any open cursors may be in use. The existing database file will be deleted and all data will be lost, * with a new empty database taking its place. * * @see #clear() */ public final void recreate() { if (isInMigration) { throw new RecreateDuringMigrationException(); } else if (isInMigrationFailedHook || databaseOpenFailedRetryCount > 0) { recreateLocked(); // Safe to call here, necessary locks are already held in this case } else { acquireExclusiveLock(); try { recreateLocked(); } finally { releaseExclusiveLock(); } } }
private String attachTo(SquidDatabase attachTo) { if (attachedTo != null) { throw new IllegalArgumentException( "Database " + getName() + " is already attached to " + attachedTo.getName()); } if (inTransaction()) { throw new IllegalStateException( "Cannot attach database " + getName() + " to " + attachTo.getName() + " -- " + getName() + " is in a transaction on the calling thread"); } acquireExclusiveLock(); String attachedAs = getAttachedName(); if (!attachTo.tryExecSql("ATTACH '" + getDatabasePath() + "' AS '" + attachedAs + "'")) { releaseExclusiveLock(); // Failed return null; } else { attachedTo = attachTo; return attachedAs; } }