diff --git a/api/current.xml b/api/current.xml index 6fa6916cd7f4a..06eeba7603a6e 100644 --- a/api/current.xml +++ b/api/current.xml @@ -66018,7 +66018,7 @@ return="boolean" abstract="false" native="false" - synchronized="true" + synchronized="false" static="false" final="false" deprecated="not deprecated" @@ -66600,19 +66600,6 @@ - - - - - + @@ -248010,7 +247997,7 @@ deprecated="not deprecated" visibility="public" > - + @@ -248022,7 +248009,7 @@ deprecated="not deprecated" visibility="public" > - + @@ -248693,7 +248680,7 @@ deprecated="not deprecated" visibility="public" > - + @@ -283770,9 +283757,9 @@ > - + - + - + - + - + @@ -284488,9 +284475,9 @@ > - + - + - + - + - + - + - + - + - + - + @@ -285171,9 +285158,9 @@ > - + - + - + - + - + @@ -285462,9 +285449,9 @@ > - + - + - + - + - + @@ -285770,9 +285757,9 @@ > - + - + - + - + - + @@ -286120,9 +286107,9 @@ > - + - + - + - + - + diff --git a/core/java/android/database/sqlite/DatabaseConnectionPool.java b/core/java/android/database/sqlite/DatabaseConnectionPool.java index 4f5c4e63770b6..249001f2c8494 100644 --- a/core/java/android/database/sqlite/DatabaseConnectionPool.java +++ b/core/java/android/database/sqlite/DatabaseConnectionPool.java @@ -246,16 +246,14 @@ import java.util.Random; } } - /* package */ void setMaxPoolSize(int size) { - synchronized(mParentDbObj) { - mMaxPoolSize = size; - } + /** only used for testing purposes. */ + /* package */ synchronized void setMaxPoolSize(int size) { + mMaxPoolSize = size; } - /* package */ int getMaxPoolSize() { - synchronized(mParentDbObj) { - return mMaxPoolSize; - } + /** only used for testing purposes. */ + /* package */ synchronized int getMaxPoolSize() { + return mMaxPoolSize; } /** only used for testing purposes. */ diff --git a/core/java/android/database/sqlite/SQLiteDatabase.java b/core/java/android/database/sqlite/SQLiteDatabase.java index 4cb70262f7c96..6882ea2116cda 100644 --- a/core/java/android/database/sqlite/SQLiteDatabase.java +++ b/core/java/android/database/sqlite/SQLiteDatabase.java @@ -1048,14 +1048,16 @@ public class SQLiteDatabase extends SQLiteClosable { * Close the database. */ public void close() { - if (!isOpen()) { - return; // already closed - } if (Log.isLoggable(TAG, Log.DEBUG)) { Log.i(TAG, "closing db: " + mPath + " (connection # " + mConnectionNum); } lock(); try { + // some other thread could have closed this database while I was waiting for lock. + // check the database state + if (!isOpen()) { + return; + } closeClosable(); // finalize ALL statements queued up so far closePendingStatements(); @@ -2102,7 +2104,7 @@ public class SQLiteDatabase extends SQLiteClosable { return; } - if (!isCacheFullWarningLogged() && mCompiledQueries.size() == mMaxSqlCacheSize) { + if (!mCacheFullWarning && mCompiledQueries.size() == mMaxSqlCacheSize) { /* * cache size of {@link #mMaxSqlCacheSize} is not enough for this app. * log a warning. @@ -2110,7 +2112,7 @@ public class SQLiteDatabase extends SQLiteClosable { */ Log.w(TAG, "Reached MAX size for compiled-sql statement cache for database " + getPath() + ". Use setMaxSqlCacheSize() to increase cachesize. "); - setCacheFullWarningLogged(); + mCacheFullWarning = true; } /* add the given SQLiteCompiledSql compiledStatement to cache. * no need to worry about the cache size - because {@link #mCompiledQueries} @@ -2134,14 +2136,16 @@ public class SQLiteDatabase extends SQLiteClosable { * From the compiledQueries cache, returns the compiled-statement-id for the given SQL. * Returns null, if not found in the cache. */ - /* package */ synchronized SQLiteCompiledSql getCompiledStatementForSql(String sql) { - SQLiteCompiledSql compiledStatement = mCompiledQueries.get(sql); - if (compiledStatement == null) { - mNumCacheMisses++; - return null; + /* package */ SQLiteCompiledSql getCompiledStatementForSql(String sql) { + synchronized (mCompiledQueries) { + SQLiteCompiledSql compiledStatement = mCompiledQueries.get(sql); + if (compiledStatement == null) { + mNumCacheMisses++; + return null; + } + mNumCacheHits++; + return compiledStatement; } - mNumCacheHits++; - return compiledStatement; } /** @@ -2159,7 +2163,7 @@ public class SQLiteDatabase extends SQLiteClosable { * the value set with previous setMaxSqlCacheSize() call. */ public void setMaxSqlCacheSize(int cacheSize) { - synchronized(this) { + synchronized(mCompiledQueries) { if (cacheSize > MAX_SQL_CACHE_SIZE || cacheSize < 0) { throw new IllegalStateException("expected value between 0 and " + MAX_SQL_CACHE_SIZE); } else if (cacheSize < mMaxSqlCacheSize) { @@ -2176,14 +2180,6 @@ public class SQLiteDatabase extends SQLiteClosable { } } - private synchronized boolean isCacheFullWarningLogged() { - return mCacheFullWarning; - } - - private synchronized void setCacheFullWarningLogged() { - mCacheFullWarning = true; - } - private synchronized int getCacheHitNum() { return mNumCacheHits; } @@ -2279,26 +2275,28 @@ public class SQLiteDatabase extends SQLiteClosable { * * @return true if write-ahead-logging is set. false otherwise */ - public synchronized boolean enableWriteAheadLogging() { - if (mPath.equalsIgnoreCase(MEMORY_DB_PATH)) { - Log.i(TAG, "can't enable WAL for memory databases."); - return false; - } - - // make sure this database has NO attached databases because sqlite's write-ahead-logging - // doesn't work for databases with attached databases - if (getAttachedDbs().size() > 1) { - if (Log.isLoggable(TAG, Log.DEBUG)) { - Log.d(TAG, - "this database: " + mPath + " has attached databases. can't enable WAL."); + public boolean enableWriteAheadLogging() { + synchronized(this) { + if (mPath.equalsIgnoreCase(MEMORY_DB_PATH)) { + Log.i(TAG, "can't enable WAL for memory databases."); + return false; } - return false; + + // make sure this database has NO attached databases because sqlite's write-ahead-logging + // doesn't work for databases with attached databases + if (getAttachedDbs().size() > 1) { + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, + "this database: " + mPath + " has attached databases. can't enable WAL."); + } + return false; + } + if (mConnectionPool == null) { + mConnectionPool = new DatabaseConnectionPool(this); + setJournalMode(mPath, "WAL"); + } + return true; } - if (mConnectionPool == null) { - mConnectionPool = new DatabaseConnectionPool(this); - setJournalMode(mPath, "WAL"); - } - return true; } /** @@ -2338,32 +2336,6 @@ public class SQLiteDatabase extends SQLiteClosable { } } - /** - * Sets the database connection handle pool size to the given value. - * Database connection handle pool is enabled when the app calls - * {@link #enableWriteAheadLogging()}. - *

- * The default connection handle pool is set by the system by taking into account various - * aspects of the device, such as memory, number of cores etc. It is recommended that - * applications use the default pool size set by the system. - * - * @param size the value the connection handle pool size should be set to. - */ - public void setConnectionPoolSize(int size) { - synchronized(this) { - if (mConnectionPool == null) { - throw new IllegalStateException("connection pool not enabled"); - } - int i = mConnectionPool.getMaxPoolSize(); - if (size < i) { - throw new IllegalArgumentException( - "cannot set max pool size to a value less than the current max value(=" + - i + ")"); - } - mConnectionPool.setMaxPoolSize(size); - } - } - /* package */ SQLiteDatabase createPoolConnection(short connectionNum) { SQLiteDatabase db = openDatabase(mPath, mFactory, mFlags, mErrorHandler, connectionNum); db.mParentConnObj = this; diff --git a/core/tests/coretests/src/android/database/sqlite/SQLiteDatabaseTest.java b/core/tests/coretests/src/android/database/sqlite/SQLiteDatabaseTest.java index 86eda710266f6..39258ae473eec 100644 --- a/core/tests/coretests/src/android/database/sqlite/SQLiteDatabaseTest.java +++ b/core/tests/coretests/src/android/database/sqlite/SQLiteDatabaseTest.java @@ -135,28 +135,6 @@ public class SQLiteDatabaseTest extends AndroidTestCase { c.close(); } - @SmallTest - public void testSetConnectionPoolSize() { - mDatabase.enableWriteAheadLogging(); - // can't set pool size to zero - try { - mDatabase.setConnectionPoolSize(0); - fail("IllegalStateException expected"); - } catch (IllegalArgumentException e) { - assertTrue(e.getMessage().contains("less than the current max value")); - } - // set pool size to a valid value - mDatabase.setConnectionPoolSize(10); - assertEquals(10, mDatabase.mConnectionPool.getMaxPoolSize()); - // can't set pool size to < the value above - try { - mDatabase.setConnectionPoolSize(1); - fail("IllegalStateException expected"); - } catch (IllegalArgumentException e) { - assertTrue(e.getMessage().contains("less than the current max value")); - } - } - /** * a transaction should be started before a standalone-update/insert/delete statement */ @@ -299,7 +277,7 @@ public class SQLiteDatabaseTest extends AndroidTestCase { if (useWal) { // set up connection pool mDatabase.enableWriteAheadLogging(); - mDatabase.setConnectionPoolSize(i + 1); + mDatabase.mConnectionPool.setMaxPoolSize(i + 1); } else { mDatabase.disableWriteAheadLogging(); }