Språk

SQLiteDatabase.EnableWriteAheadLogging Method

Definition

Write-ahead logging enables parallel execution of queries from multiple threads on the same database, and reduces the likelihood of stalling on filesystem syncs.

[Android.Runtime.Register("enableWriteAheadLogging", "()Z", "GetEnableWriteAheadLoggingHandler")]
public virtual bool EnableWriteAheadLogging();
[<Android.Runtime.Register("enableWriteAheadLogging", "()Z", "GetEnableWriteAheadLoggingHandler")>]
abstract member EnableWriteAheadLogging : unit -> bool
override this.EnableWriteAheadLogging : unit -> bool

Returns

True if write-ahead logging is enabled.

Attributes

Exceptions

if there are transactions in progress at the time this method is called. WAL mode can only be changed when there are no transactions in progress.

Remarks

Write-ahead logging enables parallel execution of queries from multiple threads on the same database, and reduces the likelihood of stalling on filesystem syncs. Write-ahead logging is significantly faster in most scenarios.

<h3>Benefits and Best Practices</h3>

The default journal mode can vary by device. It is generally recommended for apps to enable write-ahead logging, unless the app uses SQLite features that are not compatible with write-ahead logging.

When write-ahead logging is enabled, write operations occur in a separate log file, which allows reads in other threads to proceed concurrently. A read transaction continues to see its earlier snapshot after another transaction commits. A new read transaction can see the committed changes.

Another benefit of write-ahead logging is that it reduces the number of fsync calls to the underlying file system. Calling fsync may block on system locks and has poor tail latency on devices with slow filesystems.

The most efficient way to enable write-ahead logging is to pass EnableWriteAheadLogging to OpenDatabase(String, SQLiteDatabase+ICursorFactory, DatabaseOpenFlags, IDatabaseErrorHandler). This is more efficient than calling EnableWriteAheadLogging(). The following example uses Java: <pre> SQLiteDatabase db = SQLiteDatabase.openDatabase("db_filename", cursorFactory, SQLiteDatabase.CREATE_IF_NECESSARY | SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING, myDatabaseErrorHandler); </pre><h3>Disabling Write-ahead Logging</h3>

If write-ahead logging is not already enabled, this method returns false for a read-only database, an in-memory database, or a database with attached databases.

Without write-ahead logging, a writer must obtain exclusive access before it writes changes to the database file. Active readers can delay that step.

Write-ahead logging slightly increases the memory usage by SQLite. You can measure the impact on memory using tools like meminfo.

<h3>Usage</h3>

When this method enables write-ahead logging, queries can run in parallel. DisableWriteAheadLogging() clears the flags that enable write-ahead logging. On Android 13 and later, an explicitly configured WAL journal mode can keep write-ahead logging enabled after that call.

Parallel execution of queries is performed by opening multiple connections to the database and using a different database connection for each query. The database journal mode is also changed to enable writes to proceed concurrently with reads. The maximum number of connections used to execute queries in parallel is dependent upon the device memory and possibly other properties.

If a query is part of a transaction, then it is executed on the same database handle the transaction was begun.

Writers should use BeginTransactionNonExclusive() or BeginTransactionWithListenerNonExclusive(ISQLiteTransactionListener) to start a transaction. Non-exclusive mode allows database file to be in readable by other threads executing queries.

See also SQLite Write-Ahead Logging for more details about how write-ahead logging works.

Java documentation for android.database.sqlite.SQLiteDatabase.enableWriteAheadLogging().

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

See also