Kể từ ngày 27 tháng 3 năm 2025, bạn nên sử dụng android-latest-release thay vì aosp-main để xây dựng và đóng góp cho AOSP. Để biết thêm thông tin, hãy xem phần Thay đổi đối với AOSP.
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Android 9 giới thiệu một chế độ đặc biệt của SQLiteDatabase có tên là Ghi trước khả năng tương thích (WAL). Chế độ này cho phép cơ sở dữ liệu sử dụng journal_mode=WAL trong khi vẫn duy trì hành vi giữ tối đa một kết nối cho mỗi cơ sở dữ liệu.
Theo mặc định, WAL tương thích được bật cho cơ sở dữ liệu của ứng dụng, trừ phi ứng dụng có:
Yêu cầu rõ ràng chế độ nhật ký bằng cách gọi SQLiteDatabase.OpenParams.setJournalMode(String mode)
Việc bật chế độ nhật ký WAL có thể giúp cải thiện đáng kể hiệu suất và giảm lượng dữ liệu ghi. Ví dụ: trên hệ thống tệp ext4, WAL có thể giúp cải thiện tốc độ ghi gấp 4 lần.
WAL tương thích được bật theo mặc định và không yêu cầu triển khai thêm.
Bạn nên tắt WAL tương thích cho các cấu hình mà chế độ nhật ký WAL không mang lại lợi thế về hiệu suất so với các chế độ nhật ký rollback truyền thống. Ví dụ: trên hệ thống tệp F2FS, mặc dù SQLite hỗ trợ ghi nguyên tử và hiệu suất nhật ký DELETE tương tự như WAL, nhưng WAL có thể tăng số lượng ghi từ 10% đến 15%.
Xác nhận kết quả
Để xác thực chế độ WAL tương thích, hãy chạy các kiểm thử CTS từ mô-đun CtsDatabaseTestCases. Các bài kiểm thử CTS sẽ xác minh hành vi dự kiến khi bật Compatibility WAL.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-27 UTC."],[],[],null,["# Compatibility write-ahead logging for apps\n\nAndroid 9 introduces a special mode of\n[SQLiteDatabase](https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html)\ncalled Compatibility write-ahead logging (WAL) that allows a database to use\n`journal_mode=WAL` while preserving the behavior of keeping a maximum of one\nconnection per database.\n\nCompatibility WAL is enabled for an app's database by default unless the\napp has either:\n\n1. Opted-in or out of write-ahead logging by calling [`SQLiteDatabase.enableWriteAheadLogging`](https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#enableWriteAheadLogging()) or [`disableWriteAheadLogging`](https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#disableWriteAheadLogging())\n2. Explicitly requested journal mode by calling `SQLiteDatabase.OpenParams.setJournalMode(String mode)`\n\nEnabling the WAL journal mode can lead to a significant improvement in\nperformance and reduction in the amount of writes. For example, on an ext4\nfile system, WAL can lead to a 4x improvement in write speed.\n\nCompatibility WAL is enabled by default and doesn't require any additional\nimplementation.\n| **Note:** For apps using [Room](https://developer.android.com/topic/libraries/architecture/room), full write-ahead logging mode (not Compatibility WAL) is enabled by default. This applies to devices running API 16 and higher and aren't categorized as a [low memory device](https://developer.android.com/reference/android/app/ActivityManager.html#isLowRamDevice()). For more information, see [`RoomDatabase.JournalMode AUTOMATIC`](https://developer.android.com/reference/androidx/room/RoomDatabase.JournalMode#AUTOMATIC).\n\nDisable Compatibility WAL\n-------------------------\n\nTo disable the Compatibility WAL mode, overlay the\n[`db_compatibility_wal_supported`](https://android.googlesource.com/platform/frameworks/base/+/5bd43ad2e7e4e1ee2c31d920ba4b148bbdf74d11/core/res/res/values/config.xml#1692)\nconfig resource.\n\nFor example: \n\n \u003cbool name=\"db_compatibility_wal_supported\"\u003efalse\u003c/bool\u003e\n\nYou may want to disable Compatibility WAL for configurations where the WAL\njournal mode doesn't provide a performance advantage over traditional rollback\njournal modes. For example, on a F2FS file system, although SQLite supports\natomic writes and the DELETE journal performance is similar to WAL, WAL can\nincrease the amount of writes by 10% to 15%.\n\nValidation\n----------\n\nTo validate the Compatibility WAL mode, run\n[CTS tests](https://android.googlesource.com/platform/cts/+/android16-release/tests/tests/database)\nfrom the CtsDatabaseTestCases module. CTS tests will verify the expected\nbehavior when Compatibility WAL is enabled.\n| **Note:** CTS tests pass when the Compatibility WAL mode is disabled."]]