-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Database locked with databaseFactoryFfi after hot restart #5
Comments
Thanks for the report. On iOS and Android I recommend using sqflite instead of sqflite_ffi. This is indeed an issue that could happen during hot reload/restart if you were in a transaction. sqflite should handle this properly (or at least I'll be glad to know). import 'dart:io';
import 'package:sqflite/sqflite.dart' as sqflite;
import 'package:sqflite_common_ffi/sqflite_ffi.dart' as sqflite_ffi;
import 'package:sembast_sqflite/sembast_sqflite.dart' as sembast;
/// Get sqflite ffi on Windows and Linux, sqflite otherwise
sqflite.DatabaseFactory getSqfliteDatabaseFactory() {
if (Platform.isLinux || Platform.isWindows) {
return sqflite_ffi.databaseFactoryFfi;
} else {
return sqflite.databaseFactory;
}
}
void main() {
// io sembast factory for flutter using sqflite ffi on windows and linux, sqflite otherwise
var factory =
sembast.getDatabaseFactorySqflite(getSqfliteDatabaseFactory());
// ...
} |
Thanks, i'll try it with the sqflite |
@alextekartik If sqflite has special handling for this, can you point me towards it? Is this related to the |
@simolus3 The issue happens if you restart while a transaction is in progress. Unfortunately you loose the native database handler. It is unfortunately only available in memory. sqflite stores a map on the native side and return the handler when the user open the same database on hot-restart/reload. The only solution I could think off is to have a native component that allow storing/restoring global variable. After opening a database you could store you handler there (if that is possible) and restore it on reload (the dart side is killed, loosing the handler but not the native side since the shared lib is still loaded). Sorry I'm not clear as I write this in a rush. Will spend more time replying later... |
@simolus3 Here is the native trick On the dart side, I call 'ROLLBACK' in this case to revert any pending transaction |
After the change hot-restart now consistently fails on the first hot-restart but works on the second:
|
Ok... the last problem was my own stupidity. |
@kuhnroyal Thanks (I could not reproduce) so you confirm switching to sqflite fixes your original issue? |
Yes it does for hot-restarts. I suspect it having to do with the activity being killed when the app goes into background, maybe somehow the FlutterEngine stays around, not completely sure on this. @simolus3 had some ideas on how to clean up transactions in the future in the sqlite3 library. See simolus3/drift#835 |
…ackground of information here simolus3/drift#835 The solution from Simon Binder is simple and brilliant
Excellent, I was able to adapt @simolus3 very smart solution in |
…ackground of information here simolus3/drift#835 The solution from Simon Binder is simple and brilliant
I am using
databaseFactoryFfi
to create the store.When the app is processing data and using the Sembast store and I perform a hot reload it happens quite often, that afterwards the database is locked and the app needs to be killed off in order to work again. I have also gotten this error twice in the production app which I don't understand at all.
All the work and Sembast access is done in the main isolate. This happens on iOS and Android. Afterwards I get endless exceptions from everywhere in the app where I access Sembast.
From
JdbDatabaseSqflite
:Do you have any idea on how to get to the bottom of this?
Maybe the
SqfliteIsolate
still exists or something?The text was updated successfully, but these errors were encountered: