diff --git a/core/java/android/database/sqlite/SQLiteConnection.java b/core/java/android/database/sqlite/SQLiteConnection.java index 7717b8d361236..2a791ec9a42ee 100644 --- a/core/java/android/database/sqlite/SQLiteConnection.java +++ b/core/java/android/database/sqlite/SQLiteConnection.java @@ -292,8 +292,10 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen final boolean walEnabled = (mConfiguration.openFlags & SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING) != 0; // Use compatibility WAL unless an app explicitly set journal/synchronous mode + // or DISABLE_COMPATIBILITY_WAL flag is set final boolean useCompatibilityWal = mConfiguration.journalMode == null - && mConfiguration.syncMode == null && mConfiguration.useCompatibilityWal; + && mConfiguration.syncMode == null + && (mConfiguration.openFlags & SQLiteDatabase.DISABLE_COMPATIBILITY_WAL) == 0; if (walEnabled || useCompatibilityWal) { setJournalMode("WAL"); if (useCompatibilityWal && SQLiteCompatibilityWalFlags.areFlagsSet()) { @@ -423,8 +425,8 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen boolean foreignKeyModeChanged = configuration.foreignKeyConstraintsEnabled != mConfiguration.foreignKeyConstraintsEnabled; boolean walModeChanged = ((configuration.openFlags ^ mConfiguration.openFlags) - & SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING) != 0 - || configuration.useCompatibilityWal != mConfiguration.useCompatibilityWal; + & (SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING + | SQLiteDatabase.DISABLE_COMPATIBILITY_WAL)) != 0; boolean localeChanged = !configuration.locale.equals(mConfiguration.locale); // Update configuration parameters. diff --git a/core/java/android/database/sqlite/SQLiteConnectionPool.java b/core/java/android/database/sqlite/SQLiteConnectionPool.java index dc60612451d47..dadb95b7a7415 100644 --- a/core/java/android/database/sqlite/SQLiteConnectionPool.java +++ b/core/java/android/database/sqlite/SQLiteConnectionPool.java @@ -315,7 +315,12 @@ public final class SQLiteConnectionPool implements Closeable { } } - if (mConfiguration.openFlags != configuration.openFlags) { + // We should do in-place switching when transitioning from compatibility WAL + // to rollback journal. Otherwise transient connection state will be lost + boolean onlyCompatWalChanged = (mConfiguration.openFlags ^ configuration.openFlags) + == SQLiteDatabase.DISABLE_COMPATIBILITY_WAL; + + if (!onlyCompatWalChanged && mConfiguration.openFlags != configuration.openFlags) { // If we are changing open flags and WAL mode at the same time, then // we have no choice but to close the primary connection beforehand // because there can only be one connection open when we change WAL mode. diff --git a/core/java/android/database/sqlite/SQLiteDatabase.java b/core/java/android/database/sqlite/SQLiteDatabase.java index ae1f57d622283..b463d8d0c01fd 100644 --- a/core/java/android/database/sqlite/SQLiteDatabase.java +++ b/core/java/android/database/sqlite/SQLiteDatabase.java @@ -252,6 +252,13 @@ public final class SQLiteDatabase extends SQLiteClosable { */ public static final int ENABLE_WRITE_AHEAD_LOGGING = 0x20000000; + /** + * Open flag: Flag for {@link #openDatabase} to disable Compatibility WAL when opening database. + * + * @hide + */ + public static final int DISABLE_COMPATIBILITY_WAL = 0x40000000; + /** * Absolute max value that can be set by {@link #setMaxSqlCacheSize(int)}. * @@ -288,10 +295,10 @@ public final class SQLiteDatabase extends SQLiteClosable { mConfigurationLocked.idleConnectionTimeoutMs = effectiveTimeoutMs; mConfigurationLocked.journalMode = journalMode; mConfigurationLocked.syncMode = syncMode; - mConfigurationLocked.useCompatibilityWal = SQLiteGlobal.isCompatibilityWalSupported(); - if (!mConfigurationLocked.isInMemoryDb() && SQLiteCompatibilityWalFlags.areFlagsSet()) { - mConfigurationLocked.useCompatibilityWal = SQLiteCompatibilityWalFlags - .isCompatibilityWalSupported(); + if (!SQLiteGlobal.isCompatibilityWalSupported() || ( + SQLiteCompatibilityWalFlags.areFlagsSet() && !SQLiteCompatibilityWalFlags + .isCompatibilityWalSupported())) { + mConfigurationLocked.openFlags |= DISABLE_COMPATIBILITY_WAL; } } @@ -2082,21 +2089,21 @@ public final class SQLiteDatabase extends SQLiteClosable { synchronized (mLock) { throwIfNotOpenLocked(); - final boolean oldUseCompatibilityWal = mConfigurationLocked.useCompatibilityWal; final int oldFlags = mConfigurationLocked.openFlags; - if (!oldUseCompatibilityWal && (oldFlags & ENABLE_WRITE_AHEAD_LOGGING) == 0) { + final boolean walDisabled = (oldFlags & ENABLE_WRITE_AHEAD_LOGGING) == 0; + final boolean compatibilityWalDisabled = (oldFlags & DISABLE_COMPATIBILITY_WAL) != 0; + if (walDisabled && compatibilityWalDisabled) { return; } mConfigurationLocked.openFlags &= ~ENABLE_WRITE_AHEAD_LOGGING; - // If an app explicitly disables WAL, do not even use compatibility mode - mConfigurationLocked.useCompatibilityWal = false; + // If an app explicitly disables WAL, compatibility mode should be disabled too + mConfigurationLocked.openFlags |= DISABLE_COMPATIBILITY_WAL; try { mConnectionPoolLocked.reconfigure(mConfigurationLocked); } catch (RuntimeException ex) { mConfigurationLocked.openFlags = oldFlags; - mConfigurationLocked.useCompatibilityWal = oldUseCompatibilityWal; throw ex; } } diff --git a/core/java/android/database/sqlite/SQLiteDatabaseConfiguration.java b/core/java/android/database/sqlite/SQLiteDatabaseConfiguration.java index a14df1ebcbc62..275043f83743c 100644 --- a/core/java/android/database/sqlite/SQLiteDatabaseConfiguration.java +++ b/core/java/android/database/sqlite/SQLiteDatabaseConfiguration.java @@ -110,15 +110,6 @@ public final class SQLiteDatabaseConfiguration { */ public long idleConnectionTimeoutMs = Long.MAX_VALUE; - /** - * Enables compatibility WAL mode. Applications cannot explicitly choose compatibility WAL mode, - * therefore it is not exposed as a flag. - * - *

In this mode, only database journal mode will be changed, connection pool - * size will still be limited to a single connection. - */ - public boolean useCompatibilityWal; - /** * Journal mode to use when {@link SQLiteDatabase#ENABLE_WRITE_AHEAD_LOGGING} is not set. *

Default is returned by {@link SQLiteGlobal#getDefaultJournalMode()} @@ -191,7 +182,6 @@ public final class SQLiteDatabaseConfiguration { lookasideSlotSize = other.lookasideSlotSize; lookasideSlotCount = other.lookasideSlotCount; idleConnectionTimeoutMs = other.idleConnectionTimeoutMs; - useCompatibilityWal = other.useCompatibilityWal; journalMode = other.journalMode; syncMode = other.syncMode; } diff --git a/core/java/android/database/sqlite/SQLiteOpenHelper.java b/core/java/android/database/sqlite/SQLiteOpenHelper.java index 64e9e5db7c4b4..7ff66358de5f0 100644 --- a/core/java/android/database/sqlite/SQLiteOpenHelper.java +++ b/core/java/android/database/sqlite/SQLiteOpenHelper.java @@ -197,6 +197,8 @@ public abstract class SQLiteOpenHelper { } mOpenParamsBuilder.setWriteAheadLoggingEnabled(enabled); } + // Compatibility WAL is disabled if an app disables or enables WAL + mOpenParamsBuilder.addOpenFlags(SQLiteDatabase.DISABLE_COMPATIBILITY_WAL); } }