[PoC] Direct op-sqlite connections #682
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This exposes an additional
OPSqliteOpenFactory.openDirectConnection()
method, to get direct access to an op-sqlite DB.This is similar to opening another op-sqlite DB directly, but:
OPSqliteOpenFactory
.We do this rather than exposing the existing DB, since that would bypass any existing transaction locking mechanisms, leading to likely race conditions when the DB is used directly.
One specific advantage of this is allowing synchronous queries to be executed, to make it easier to migrate existing applications currently built on synchronous queries. I would not recommend this for other use cases.
Even with this approach, synchronous writes should be wrapped with
PowerSyncDatabase.writeLock()
, otherwise it could fail withSQLITE_BUSY
if another transaction is busy. We cannot really useBUSY_TIMEOUT
for this, since the synchronous query would block the JS thread, preventing the other lock from being released.Synchronous reads do not have the same restriction, and can run concurrently with any other transaction on the PowerSyncDatabase.
Other changes
BEGIN EXCLUSIVE
instead ofBEGIN
: Same as in the web SDK. This reduces the number of lock steps, and avoids triggering SQLITE_BUSY errors later in the transaction.TODOs
OPSQLiteDBAdapter
- this can probably be done better.