From 587423afb52f65db28498a9f2378cccbd2e823c8 Mon Sep 17 00:00:00 2001 From: Vasu Nori Date: Mon, 27 Sep 2010 18:18:34 -0700 Subject: fix this: closing database twice fails with IllegalStateException Change-Id: Idda35e49d539845099b377872f9a6f0e0caa4626 --- .../android/database/sqlite/DatabaseConnectionPool.java | 4 ++-- core/java/android/database/sqlite/SQLiteDatabase.java | 11 ++++++++++- core/java/android/database/sqlite/SQLiteProgram.java | 16 +++++++--------- 3 files changed, 19 insertions(+), 12 deletions(-) (limited to 'core/java/android') diff --git a/core/java/android/database/sqlite/DatabaseConnectionPool.java b/core/java/android/database/sqlite/DatabaseConnectionPool.java index 8adc9c578dca..39a9d23c0220 100644 --- a/core/java/android/database/sqlite/DatabaseConnectionPool.java +++ b/core/java/android/database/sqlite/DatabaseConnectionPool.java @@ -93,7 +93,7 @@ import java.util.Random; poolObj = mPool.get(0); } else { for (int i = 0; i < mMaxPoolSize; i++) { - if (mPool.get(i).mDb.isSqlInStatementCache(sql)) { + if (mPool.get(i).mDb.isInStatementCache(sql)) { poolObj = mPool.get(i); break; } @@ -119,7 +119,7 @@ import java.util.Random; // there are free connections available. pick one // preferably a connection caching the pre-compiled statement of the given SQL for (int i = 0; i < poolSize; i++) { - if (mPool.get(i).isFree() && mPool.get(i).mDb.isSqlInStatementCache(sql)) { + if (mPool.get(i).isFree() && mPool.get(i).mDb.isInStatementCache(sql)) { poolObj = mPool.get(i); break; } diff --git a/core/java/android/database/sqlite/SQLiteDatabase.java b/core/java/android/database/sqlite/SQLiteDatabase.java index 6882ea2116cd..7ab5d82936fb 100644 --- a/core/java/android/database/sqlite/SQLiteDatabase.java +++ b/core/java/android/database/sqlite/SQLiteDatabase.java @@ -1048,6 +1048,9 @@ public class SQLiteDatabase extends SQLiteClosable { * Close the database. */ public void close() { + if (!isOpen()) { + return; + } if (Log.isLoggable(TAG, Log.DEBUG)) { Log.i(TAG, "closing db: " + mPath + " (connection # " + mConnectionNum); } @@ -2174,12 +2177,18 @@ public class SQLiteDatabase extends SQLiteClosable { } } - /* package */ boolean isSqlInStatementCache(String sql) { + /* package */ boolean isInStatementCache(String sql) { synchronized (mCompiledQueries) { return mCompiledQueries.containsKey(sql); } } + /* package */ boolean isInStatementCache(SQLiteCompiledSql sqliteCompiledSql) { + synchronized (mCompiledQueries) { + return mCompiledQueries.containsValue(sqliteCompiledSql); + } + } + private synchronized int getCacheHitNum() { return mNumCacheHits; } diff --git a/core/java/android/database/sqlite/SQLiteProgram.java b/core/java/android/database/sqlite/SQLiteProgram.java index 4747a9e9fd1a..e78e35ab4ba2 100644 --- a/core/java/android/database/sqlite/SQLiteProgram.java +++ b/core/java/android/database/sqlite/SQLiteProgram.java @@ -188,15 +188,13 @@ public abstract class SQLiteProgram extends SQLiteClosable { // this SQL statement was never in cache mCompiledSql.releaseSqlStatement(); } else { - synchronized(mDatabase.mCompiledQueries) { - if (!mDatabase.mCompiledQueries.containsValue(mCompiledSql)) { - // it is NOT in compiled-sql cache. i.e., responsibility of - // releasing this statement is on me. - mCompiledSql.releaseSqlStatement(); - } else { - // it is in compiled-sql cache. reset its CompiledSql#mInUse flag - mCompiledSql.release(); - } + if (!mDatabase.isInStatementCache(mCompiledSql)) { + // it is NOT in compiled-sql cache. i.e., responsibility of + // releasing this statement is on me. + mCompiledSql.releaseSqlStatement(); + } else { + // it is in compiled-sql cache. reset its CompiledSql#mInUse flag + mCompiledSql.release(); } } mCompiledSql = null; -- cgit v1.2.3