| <html devsite><head> |
| |
| <meta name="book_path" value="/_book.yaml"/> |
| |
| <meta name="project_path" value="/_project.yaml"/> |
| </head> |
| <body> |
| |
| <!-- |
| Copyright 2018 The Android Open Source Project |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| --> |
| |
| <h1 id="compatibility_wal_write_ahead_logging_for_apps" class="page-title">应用的兼容性 WAL(预写日志记录)</h1> |
| |
| <p>Android 9 引入了 <a href="https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html" class="external">SQLiteDatabase</a> 的一种特殊模式,称为“兼容性 WAL(预写日志记录)”,它允许数据库使用 <code>journal_mode=WAL</code>,同时保留每个数据库最多创建一个连接的行为。</p> |
| |
| <p>默认情况下,系统会为应用的数据库启用兼容性 WAL,除非应用已执行以下任一操作:</p> |
| |
| <ol> |
| <li class="external">通过调用 <a href="https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#enableWriteAheadLogging()" class="external"><code>SQLiteDatabase.enableWriteAheadLogging</code></a> 或 <a href="https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#disableWriteAheadLogging()"><code>disableWriteAheadLogging</code></a> 启用或停用了预写日志记录</li> |
| <li>通过调用以下内容明确请求了 journal 模式:<code>SQLiteDatabase.OpenParams.setJournalMode(String mode)</code></li> |
| </ol> |
| |
| <p>启用 WAL journal 模式可显著提升性能并减少写入量。例如,在 ext4 文件系统中,WAL 可将写入速度提高 4 倍。</p> |
| |
| <p>兼容性 WAL 默认处于启用状态,且无需任何额外的实现操作。</p> |
| <aside class="note"><strong>注意</strong>:<span>对于使用 <a href="https://developer.android.com/topic/libraries/architecture/room">Room</a> 的应用,系统会默认启用完全预写日志记录模式(而非兼容性 WAL)。这适用于运行 API 16 或更高版本且未被归为<a href="https://developer.android.com/reference/android/app/ActivityManager.html#isLowRamDevice()">低内存设备</a>的设备。要了解详情,请参阅 <a href="https://developer.android.com/reference/androidx/room/RoomDatabase.JournalMode#AUTOMATIC"><code>RoomDatabase.JournalMode AUTOMATIC</code></a>。</span></aside> |
| <h2 id="disabling_compatibility_wal">停用兼容性 WAL</h2> |
| |
| <p>要停用兼容性 WAL 模式,请覆盖 <a href="https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/config.xml" class="external"><code>db_compatibility_wal_supported</code></a> 配置资源。</p> |
| |
| <p>例如:</p> |
| <pre class="prettyprint"><code><bool name="db_compatibility_wal_supported">false</bool> |
| </code></pre> |
| <p>如果对于某些配置而言,WAL journal 模式的性能与原有回滚 journal 模式相比没有提升,您可能想为这种配置停用兼容性 WAL。例如,在 F2FS 文件系统中,虽然 SQLite 支持原子写入且 DELETE journal 性能与 WAL 类似,但 WAL 可以将写入量提高 10% 到 15%。</p> |
| |
| <h2 id="validation">验证</h2> |
| |
| <p>要验证兼容性 WAL 模式,请从 CtsDatabaseTestCases 模块运行 <a href="https://android.googlesource.com/platform/cts/+/master/tests/tests/database" class="external">CTS 测试</a>。CTS 测试将在启用兼容性 WAL 后验证预期行为。</p> |
| <aside class="note"><strong>注意</strong>:<span>CTS 测试仅在兼容性 WAL 模式停用的情况下才会通过。</span></aside> |
| |
| </body></html> |