Git Product home page Git Product logo

Comments (5)

simolus3 avatar simolus3 commented on September 22, 2024

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.

diederikMrAwesome avatar diederikMrAwesome commented on September 22, 2024

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.

simolus3 avatar simolus3 commented on September 22, 2024

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.

diederikMrAwesome avatar diederikMrAwesome commented on September 22, 2024

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.

simolus3 avatar simolus3 commented on September 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.