Status Update
Comments
da...@google.com <da...@google.com> #2
Can you provide a bit more details how PRAGMA wal_autocheckpoint
is failing to work in onOpen? When you run the PRAGMA statement again later is the result different than what you expected?
Room does not expose onConfigure but you can pass an open helper factory that creates an open helper that implements onConfigure, see: FrameworkSQLiteOpenHelperFactory
to create the helper to delegate to the other methods you're not interested on. Let me know if this helps.
da...@google.com <da...@google.com> #3
Actually, looking at the framework's binding implementation it seems wal_autocheckpoint
is a per connection configuration, you essentially have to execute that PRAGMA before any query, since neither the framework or Room offers a callback for configuring an individual SQLiteConnection. So I'm not sure you'll be able to successfully and nicely disable auto checkpoint.
I'm curious now as to why do you want to disable auto checkpoints? If it's possible to know that is.
ah...@gmail.com <ah...@gmail.com> #4
The reason is that I have a big transaction that needs to be written (> 20K rows after establishing relations), however after the transaction finishes since the WAL Journal exceeded the autocheckpoint threshold, the SQLite tries to write back the Journal to the DB file, which could take roughly 5 seconds, and during that time the Database is locked from reading and writing, which results in an unpleasant UX as the user waits for 5 seconds until he can be able to see the details of the item.
I tried to commit smaller transactions by chunking the rows into smaller rows, but this made the writing to take more than it should, and also didn't solve the DB read/write block issue part entirely, especially with Android defining the autocheckpoint at 100 pages[1] rather than the default SQLite 1000 pages[2].
So my plan was to disable auto checkpointing entirely, and Schedule a Periodic manual checkpoint that should run as a background task through WorkManager and runs when the app is in background. However as you've mentioned the PRAGMA needs to be executed per connection, and not per open, and that's not supported in Room, or the Framework's SQLite code.
There is a new API introduced since API 30 that exactly solves such a case I'm facing, but not sure if it can be backported. [3]
Right now I've managed to solve my problem by using Requery's version of SQLite since it has better defaults than the Framework's[4], and with that it solved the writing performance that I'm facing, at the expense of the APK size unfortunately.
I think you can close this issue since it won't really solve the use case.
[1]
[2]
[3]
da...@google.com <da...@google.com> #5
I see... Indeed execPerConnectionSQL
would be ideal and was actually introduced for this and similar reasons (to let user apply per connection configurations), but it is tied to the native bindings in the framework. I don't think we can backport it to older APIs, but we can update the SupportSQLiteDatabase
ah...@gmail.com <ah...@gmail.com> #6
Thanks for your reply! I think adding it to the SupportSQLiteDatabase would emphasize its importance so that third party SQLite providers like Requery might take it into account and provide a backport that would work on pre-API 30 levels. I've already opened an issue there asking them to support it, but that was before our conversation.[1] If you don't mind I'll link your comment there. (I'll wait for your OK though before doing that)
ap...@google.com <ap...@google.com> #7
Branch: androidx-master-dev
commit 2e7b221b71de60a3a70589cabfe0816c8a2bba2a
Author: Daniel Santiago Rivera <danysantiago@google.com>
Date: Fri Nov 06 11:35:22 2020
Add default method for execPerConnectionSQL() in SupportSQLiteDatabase
SQLiteDatabase#execPerConnectionSQL() was added in API 30 and cannot be backported but a new default method for it can be declared so those who provide the androidx.sqlite interfaces with unbundled SQLite bindings can implement it. For the default framework implementation of the interface the method will throw an UnsupportedOperationException if the API level is lower than 30. To check if the operation is supported users should call isExecPerConnectionSQLSupported(). This allow us to not add @RequireApi(30) which would prevent unbundled implementations to not offer the functionality to lower API levels.
Bug: 172270145
Test: N/A
Relnote: Add default method for execPerConnectionSQL() in SupportSQLiteDatabase
Change-Id: I86326d81aad82c1efccbb2087c439655c9b4100f
M sqlite/sqlite-framework/src/main/java/androidx/sqlite/db/framework/FrameworkSQLiteDatabase.java
M sqlite/sqlite/api/current.txt
M sqlite/sqlite/api/public_plus_experimental_current.txt
M sqlite/sqlite/api/restricted_current.txt
M sqlite/sqlite/src/main/java/androidx/sqlite/db/SupportSQLiteDatabase.java
Description
Component used: Jetpack Room Version used: 2.2.5 Devices/Android versions reproduced on: Any
I'd like to execute some
PRAGMA
statements[1], before using the Database, I tried to do that ononOpen
callback but it doesn't seem to work. There is anonConfigure
[2] callback from the SQLiteOpenHelper that I can use but it's not exposed through Room. Is it possible to expose it or is there an alternative way to executePRAGMA
statements?I can send a PR if it's okay.
[1] PRAGMA wal_autocheckpoint=-1
[2]https://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper#onConfigure(android.database.sqlite.SQLiteDatabase)