Android.Database.Sqlite.SQLiteDatabase.EnableWriteAheadLogging Method
This method enables parallel execution of queries from multiple threads on the same database.

Syntax

[Android.Runtime.Register("enableWriteAheadLogging", "()Z", "GetEnableWriteAheadLoggingHandler")]
public virtual bool EnableWriteAheadLogging ()

See Also

SQLiteDatabase.DisableWriteAheadLogging

Returns

Documentation for this section has not yet been entered.

Exceptions

TypeReason
Java.Lang.IllegalStateExceptionif 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

This method enables parallel execution of queries from multiple threads on the same database. It does this 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.

When write-ahead logging is not enabled (the default), it is not possible for reads and writes to occur on the database at the same time. Before modifying the database, the writer implicitly acquires an exclusive lock on the database which prevents readers from accessing the database until the write is completed.

In contrast, when write-ahead logging is enabled (by calling this method), write operations occur in a separate log file which allows reads to proceed concurrently. While a write is in progress, readers on other threads will perceive the state of the database as it was before the write began. When the write completes, readers on other threads will then perceive the new state of the database.

It is a good idea to enable write-ahead logging whenever a database will be concurrently accessed and modified by multiple threads at the same time. However, write-ahead logging uses significantly more memory than ordinary journaling because there are multiple connections to the same database. So if a database will only be used by a single thread, or if optimizing concurrency is not very important, then write-ahead logging should be disabled.

After calling this method, execution of queries in parallel is enabled as long as the database remains open. To disable execution of queries in parallel, either call SQLiteDatabase.DisableWriteAheadLogging or close the database and reopen it.

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 SQLiteDatabase.BeginTransactionNonExclusive or SQLiteDatabase.BeginTransactionWithListenerNonExclusive(ISQLiteTransactionListener) to start a transaction. Non-exclusive mode allows database file to be in readable by other threads executing queries.

If the database has any attached databases, then execution of queries in parallel is NOT possible. Likewise, write-ahead logging is not supported for read-only databases or memory databases. In such cases, SQLiteDatabase.EnableWriteAheadLogging returns false.

The best way to enable write-ahead logging is to pass the SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING flag to SQLiteDatabase.OpenDatabase(string, .ICursorFactory, .ICursorFactory). This is more efficient than calling SQLiteDatabase.EnableWriteAheadLogging.

java Example

     SQLiteDatabase db = SQLiteDatabase.openDatabase("db_filename", cursorFactory,
             SQLiteDatabase.CREATE_IF_NECESSARY | SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING,
             myDatabaseErrorHandler);
     db.enableWriteAheadLogging();
 

Another way to enable write-ahead logging is to call SQLiteDatabase.EnableWriteAheadLogging after opening the database.

java Example

     SQLiteDatabase db = SQLiteDatabase.openDatabase("db_filename", cursorFactory,
             SQLiteDatabase.CREATE_IF_NECESSARY, myDatabaseErrorHandler);
     db.enableWriteAheadLogging();
 

See also for more details about how write-ahead logging works.

[Android Documentation]

Requirements

Namespace: Android.Database.Sqlite
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 11