summaryrefslogtreecommitdiff
path: root/core/java/android
diff options
context:
space:
mode:
Diffstat (limited to 'core/java/android')
-rw-r--r--core/java/android/database/sqlite/DatabaseConnectionPool.java14
-rw-r--r--core/java/android/database/sqlite/SQLiteDatabase.java100
2 files changed, 42 insertions, 72 deletions
diff --git a/core/java/android/database/sqlite/DatabaseConnectionPool.java b/core/java/android/database/sqlite/DatabaseConnectionPool.java
index 4f5c4e63770b..249001f2c849 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 4cb70262f7c9..6882ea2116cd 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;
- }
+ public boolean enableWriteAheadLogging() {
+ synchronized(this) {
+ 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.");
+ // 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;
}
- return false;
- }
- if (mConnectionPool == null) {
- mConnectionPool = new DatabaseConnectionPool(this);
- setJournalMode(mPath, "WAL");
+ if (mConnectionPool == null) {
+ mConnectionPool = new DatabaseConnectionPool(this);
+ setJournalMode(mPath, "WAL");
+ }
+ return true;
}
- 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()}.
- * <p>
- * 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;