simolus3 / drift

Drift is an easy to use, reactive, typesafe persistence library for Dart & Flutter.
https://drift.simonbinder.eu/
MIT License
2.43k stars 351 forks source link

LazyDatabase is imposible to execute queries when reopen #3003

Closed dko-orion closed 1 month ago

dko-orion commented 1 month ago

I have in my part of my application I use LazyDatabase open connection like it shared in example. After my part (module) of application is finished I close database to release not needed resources for now and prevent memory leaks etc.

Then I can return to this part of application again and call open connection again with LazyDatabase and continue working with database. After all this done I'm getting error:

IStateError (Bad state: Tried to send Request (id = 6): StatementMethod.select: SELECT * FROM "s_table"; with [] over isolate channel, but the connection was closed!)

drift version 2.1.8 Dependency injection: Modular

simolus3 commented 1 month ago

Closing drift databases is a final operation, they can't be re-opened afterwards. This is by design - in some cases (like when talking to a database over the network, we'd have no good time point to actually close underlying resources if it's expected that users will just re-open the database after closing it).

The solution is to either not close the database (since you're still referencing it to reuse it afterwards, you're not really saving any memory). Or, if you still want to close it, you'll have to re-create a new instance of your database class - that one can then be used again of course.