From 8dc3cc2e13b500e368f5ba1aacfaf0eddbce668c Mon Sep 17 00:00:00 2001 From: Jeff Brown Date: Fri, 2 Mar 2012 10:33:52 -0800 Subject: [PATCH] Allow the SQLite sync mode to be set independently for WAL. This change leaves the sync mode at FULL for both WAL and non-WAL but makes it easy to change it for one but not the other. To reduce the number of synchronous writes, it might make sense to change the sync mode for non-WAL to NORMAL instead of FULL which should be just as safe. On the other hand, the sync mode for WAL should probably remain FULL because there may be an impact on transaction durability otherwise. Initial experiments show that there might not be a significant performance benefit to using NORMAL, but we may revisit this later. Change-Id: Ifcd55bedcfefa6600974c2295ca5d4163b408cbf --- .../database/sqlite/SQLiteConnection.java | 25 ++++++++++++------- .../database/sqlite/SQLiteDatabase.java | 2 ++ .../sqlite/SQLiteDatabaseConfiguration.java | 9 +++++++ .../android/database/sqlite/SQLiteGlobal.java | 16 +++++++++--- core/res/res/values/config.xml | 15 +++++++++-- core/res/res/values/public.xml | 3 ++- 6 files changed, 54 insertions(+), 16 deletions(-) diff --git a/core/java/android/database/sqlite/SQLiteConnection.java b/core/java/android/database/sqlite/SQLiteConnection.java index 190030196ff22..d16f29f60eee9 100644 --- a/core/java/android/database/sqlite/SQLiteConnection.java +++ b/core/java/android/database/sqlite/SQLiteConnection.java @@ -208,11 +208,11 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen mConfiguration.label, SQLiteDebug.DEBUG_SQL_STATEMENTS, SQLiteDebug.DEBUG_SQL_TIME); - setSyncMode(); setPageSize(); - setAutoCheckpointInterval(); - setJournalSizeLimit(); + setSyncModeFromConfiguration(); setJournalModeFromConfiguration(); + setJournalSizeLimit(); + setAutoCheckpointInterval(); setLocaleFromConfiguration(); } @@ -236,12 +236,6 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen } } - private void setSyncMode() { - if (!mConfiguration.isInMemoryDb()) { - execute("PRAGMA synchronous=" + SQLiteGlobal.getSyncMode(), null, null); - } - } - private void setPageSize() { if (!mConfiguration.isInMemoryDb()) { execute("PRAGMA page_size=" + SQLiteGlobal.getDefaultPageSize(), null, null); @@ -262,6 +256,12 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen } } + private void setSyncModeFromConfiguration() { + if (!mConfiguration.isInMemoryDb()) { + execute("PRAGMA synchronous=" + mConfiguration.syncMode, null, null); + } + } + private void setJournalModeFromConfiguration() { if (!mConfiguration.isInMemoryDb()) { String result = executeForString("PRAGMA journal_mode=" + mConfiguration.journalMode, @@ -290,6 +290,8 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen } // Remember what changed. + boolean syncModeChanged = !configuration.syncMode.equalsIgnoreCase( + mConfiguration.syncMode); boolean journalModeChanged = !configuration.journalMode.equalsIgnoreCase( mConfiguration.journalMode); boolean localeChanged = !configuration.locale.equals(mConfiguration.locale); @@ -300,6 +302,11 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen // Update prepared statement cache size. mPreparedStatementCache.resize(configuration.maxSqlCacheSize); + // Update sync mode. + if (syncModeChanged) { + setSyncModeFromConfiguration(); + } + // Update journal mode. if (journalModeChanged) { setJournalModeFromConfiguration(); diff --git a/core/java/android/database/sqlite/SQLiteDatabase.java b/core/java/android/database/sqlite/SQLiteDatabase.java index 2f3dc066a1df0..04ee142c09dd9 100644 --- a/core/java/android/database/sqlite/SQLiteDatabase.java +++ b/core/java/android/database/sqlite/SQLiteDatabase.java @@ -1781,6 +1781,7 @@ public class SQLiteDatabase extends SQLiteClosable { mIsWALEnabledLocked = true; mConfigurationLocked.maxConnectionPoolSize = SQLiteGlobal.getWALConnectionPoolSize(); + mConfigurationLocked.syncMode = SQLiteGlobal.getWALSyncMode(); mConfigurationLocked.journalMode = "WAL"; mConnectionPoolLocked.reconfigure(mConfigurationLocked); } @@ -1801,6 +1802,7 @@ public class SQLiteDatabase extends SQLiteClosable { mIsWALEnabledLocked = false; mConfigurationLocked.maxConnectionPoolSize = 1; + mConfigurationLocked.syncMode = SQLiteGlobal.getDefaultSyncMode(); mConfigurationLocked.journalMode = SQLiteGlobal.getDefaultJournalMode(); mConnectionPoolLocked.reconfigure(mConfigurationLocked); } diff --git a/core/java/android/database/sqlite/SQLiteDatabaseConfiguration.java b/core/java/android/database/sqlite/SQLiteDatabaseConfiguration.java index 32a1bcbcd29a1..efbcaca0034f9 100644 --- a/core/java/android/database/sqlite/SQLiteDatabaseConfiguration.java +++ b/core/java/android/database/sqlite/SQLiteDatabaseConfiguration.java @@ -84,6 +84,13 @@ public final class SQLiteDatabaseConfiguration { */ public Locale locale; + /** + * The database synchronization mode. + * + * Default is {@link SQLiteGlobal#getDefaultSyncMode()}. + */ + public String syncMode; + /** * The database journal mode. * @@ -117,6 +124,7 @@ public final class SQLiteDatabaseConfiguration { maxConnectionPoolSize = 1; maxSqlCacheSize = 25; locale = Locale.getDefault(); + syncMode = SQLiteGlobal.getDefaultSyncMode(); journalMode = SQLiteGlobal.getDefaultJournalMode(); } @@ -154,6 +162,7 @@ public final class SQLiteDatabaseConfiguration { maxConnectionPoolSize = other.maxConnectionPoolSize; maxSqlCacheSize = other.maxSqlCacheSize; locale = other.locale; + syncMode = other.syncMode; journalMode = other.journalMode; customFunctions.clear(); customFunctions.addAll(other.customFunctions); diff --git a/core/java/android/database/sqlite/SQLiteGlobal.java b/core/java/android/database/sqlite/SQLiteGlobal.java index af0cf457e0ebf..5d8f80eae167a 100644 --- a/core/java/android/database/sqlite/SQLiteGlobal.java +++ b/core/java/android/database/sqlite/SQLiteGlobal.java @@ -83,11 +83,19 @@ public final class SQLiteGlobal { } /** - * Gets the database synchronization mode. + * Gets the default database synchronization mode when WAL is not in use. */ - public static String getSyncMode() { + public static String getDefaultSyncMode() { return Resources.getSystem().getString( - com.android.internal.R.string.db_sync_mode); + com.android.internal.R.string.db_default_sync_mode); + } + + /** + * Gets the database synchronization mode when in WAL mode. + */ + public static String getWALSyncMode() { + return Resources.getSystem().getString( + com.android.internal.R.string.db_wal_sync_mode); } /** @@ -99,7 +107,7 @@ public final class SQLiteGlobal { } /** - * Gets the default connection pool size when in WAL mode. + * Gets the connection pool size when in WAL mode. */ public static int getWALConnectionPoolSize() { return Math.max(2, Resources.getSystem().getInteger( diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml index be43513c65672..eaf9c8c804762 100755 --- a/core/res/res/values/config.xml +++ b/core/res/res/values/config.xml @@ -661,9 +661,20 @@ truncate it after committing the transaction. --> 524288 - - FULL + FULL + + + FULL