Thinking About Journal Modes

If you examine your Room-enabled app’s portion of internal storage in Device File Explorer, in the databases/ subdirectory you will find your actual Room database. Frequently, though, it consists of three files:

This indicates that the database was opened in a particular “journal mode” called write-ahead logging (WAL) and has not been closed. Roughly speaking, in WAL mode, database transactions primarily affect these secondary files and only get merged into the main database at particular “checkpoints”. Such checkpoints occur automatically, and for most circumstances we do not really worry about them. However, when it comes to making a backup or exporting a database, it is best if everything gets merged into the main database file.

Primarily, the way we can do that is to close() our RoomDatabase. Once all connections to our SQLite database are closed, SQLite will checkpoint the database, merging everything into the main database file and removing the -shm and -wal files.

You can disable WAL outright by calling setJournalMode() on your RoomDatabase.Builder and opting into TRUNCATE as a journal mode.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.