Comments (5)
Thanks for the report! I can reproduce the issue, but I'm afraid I don't really understand how this is happening either. Interestingly it works for me after re-opening the app and navigating to the Flutter fragment - so it's not an actual database corruption but rather appears to be corrupting an in-memory cache.
One workaround may be to use drift_sqflite instead of package:drift/native.dart
and sqlite3_flutter_libs
- you can replace the NativeDatabase
with an SqfliteQueryExecutor
. sqflite uses platform channels to run sqlite3 queries, so that avoids potential conflicts there.
from drift.
I made a new branch on that repo that seems to confirm your suspicions. I can't now reproduce this problem on the example app. Thanks!
(I do notice that it is much slower, but I guess that is the cost of the workaround.)
Do you think it is worth posting the original problem on the SQLite forums? I am not expecting the Android process (That accesses the SQLite db file via the system SQLite service (and uses an older version of SQLite) to conflict with the one read/written by normal drift in this way.
from drift.
Do you think it is worth posting the original problem on the SQLite forums?
I think the chance of this being an Android-specific issue are far higher. I know that Android is doing its own locking around sqlite3 databases when using the Java API, but I don't know if that means they're setting a flag to disable sqlite3 locks altogether. I wanted to check, but pragma compile_options;
doesn't return any rows for me when running it on Android.
There has to be a proper solution here, but I could not find it. You can't be the first one sharing a sqlite3 database between Java and what's essentially an NDK thread accessing the same database via a direct C library for sqlite3.
from drift.
I've seen an interesting thing: if you enable write-ahead logging on Android side, the problem goes away.
It is my understanding that the filesystem locking should allow for smooth access in this case between Android and drift. Could one of the two parties here be ignoring the locks? Or not setting them up correctly?
Patch snippet for above idea:
Index: app/src/main/java/com/skynamo/sqlitedbfun/ui/home/HomeFragment.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/app/src/main/java/com/skynamo/sqlitedbfun/ui/home/HomeFragment.kt b/app/src/main/java/com/skynamo/sqlitedbfun/ui/home/HomeFragment.kt
--- a/app/src/main/java/com/skynamo/sqlitedbfun/ui/home/HomeFragment.kt (revision fdd421dfd08dda9613476f708192df3b57eb4a9d)
+++ b/app/src/main/java/com/skynamo/sqlitedbfun/ui/home/HomeFragment.kt (date 1715266975382)
@@ -64,6 +64,8 @@
private fun insertRandomUserData() {
if (_dao == null) {
_dao = DataItemDao(this.requireContext())
+ _dao?.databaseHelper?.setWriteAheadLoggingEnabled(true)
}
// Launch a coroutine in the background
from drift.
Could one of the two parties here be ignoring the locks? Or not setting them up correctly?
Yeah that's what I'm thinking as well. It's possible that WAL mode just hides the symptoms (WAL mode still uses locks on the WAL file to avoid two concurrent writers - it's possible that this still has potential to break, but if one process is read-only it should be safe).
Still, drift/package:sqlite3
isn't disabling thread safety and as far as I know, Android isn't doing that either. So I still don't understand what's causing this.
from drift.
Related Issues (20)
- Drift generator doesn't strip custom row class from SQL HOT 1
- Primary Key not required in companion insert HOT 2
- Issue with generated code: database.g.dart file HOT 29
- Select int64 return type HOT 4
- Start a select with the Dart API without specifiying a table HOT 4
- Custom query generated type missing nullables HOT 4
- [WEB] DriftRemoteException: RangeError (start): Invalid value: Not in inclusive range 0..2322399232: -1972640192 HOT 1
- createdAt & updatedAt columns HOT 4
- SqliteException(5): while selecting from statement, database is locked, database is locked (code 5) HOT 2
- drift files: file formatting impossible
- How to initialize the database without the constructor syntax HOT 2
- Add toJson() for result class HOT 1
- Query stream cancellations can affect migrations
- No modification allowed Error HOT 4
- Schema migration helper fails when there are no migrations yet HOT 1
- Views code generation HOT 9
- Cannot test migrations using modular approach with store_date_time_values_as_text: true HOT 4
- Unable to override `runs_before` in `build.yaml` due to `Unrecognized keys` exception HOT 2
- Support remote SQLite databases HOT 8
- drift_flutter package doesn't exist HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from drift.