objectbox / objectbox-dart

Flutter database for super-fast Dart object persistence
https://docs.objectbox.io/getting-started
Apache License 2.0
1.04k stars 120 forks source link

Could not begin read transaction (another read transaction is still active on this thread) #368

Closed rizkyagungramadhan closed 2 years ago

rizkyagungramadhan commented 2 years ago

image

Hello thanks a lot for making my life easier with ObjectBox. I faced a problem when I implemented ObjectBox using WorkManager in Flutter.

In the process there are transactions in the form of reads and writes to the database, when the read -> download from Api -> insert process has been completed I get an error "Could not begin read transaction (another read transaction is still active on this thread)" when I retry to read database in the application (the process of inserting into the database all successful).

But when the process in Work Manager is still running, everything works normally for me when I want to access the database directly in the application.

The error only appears when all work manager process has been completed.

This is my stacktrace :

E/Box     ( 9986): Storage error (code -30783)
[GETX] Info: AttendanceController Instance of 'AttendanceController' #0      ObjectBoxNativeError.throwMapped (package:objectbox/src/native/bindings/helpers.dart:78:9)
objectbox/objectbox-java#1      throwLatestNativeError (package:objectbox/src/native/bindings/helpers.dart:50:48)
objectbox/objectbox-java#2      checkObxPtr (package:objectbox/src/native/bindings/helpers.dart:32:5)
objectbox/objectbox-java#3      new Transaction (package:objectbox/src/native/transaction.dart:33:5)
objectbox/objectbox-java#4      Store._runInTransaction (package:objectbox/src/native/store.dart:299:32)
objectbox/objectbox-java#5      Store.runInTransaction (package:objectbox/src/native/store.dart:292:12)
objectbox/objectbox-java#6      BaseDao._executeQuery (package:app/entity/base_dao.dart:11:86)
objectbox/objectbox-java#7      BaseDao.findAllWhere (package:app/entity/base_dao.dart:39:74)

I am using objectbox: ^1.3.0 Thank you, very happy if anyone can help me with this problem

greenrobot-team commented 2 years ago

Moved to objectbox-dart repo as this is using Flutter.

The underlying issue is that the transaction was not completed because it failed with a crash: "E/Box ( 9986): Storage error (code -30783)". This error is described as MDB_BAD_RSLOT "Invalid reuse of reader locktable slot". @greenrobot Do you have an idea?

How does your WorkManager code look like? Does it maybe close the database when it finished all work?

greenrobot commented 2 years ago

@rizkyagungramadhan Are you using multiple isolates?

rizkyagungramadhan commented 2 years ago

Moved to objectbox-dart repo as this is using Flutter.

The underlying issue is that the transaction was not completed because it failed with a crash: "E/Box ( 9986): Storage error (code -30783)". This error is described as MDB_BAD_RSLOT "Invalid reuse of reader locktable slot". @greenrobot Do you have an idea?

How does your WorkManager code look like? Does it maybe close the database when it finished all work?

No, Im not closing the database either the WorkManager result error or success. Currently I'm handling with openStore() right after WorkManager process is done. At least it is working now. But is it should be fine to accessing database from multiple isolates since Im using same reference to the Store object in all isolates?

gcostaapps commented 2 years ago

I am having the same error when trying to open my flutter project with another flutter engine. The reads of object box (on this other flutter engine) throws this error.

greenrobot-team commented 2 years ago

@gcostaapps Thanks, but what do you mean by "open my flutter project with another flutter engine"? Can you share more details about the project?

greenrobot-team commented 2 years ago

But is it should be fine to accessing database from multiple isolates since Im using same reference to the Store object in all isolates?

New releases have added a Store.attach API for this purpose. There are now also Store.runInTransactionAsync and Store.runAsync for running database operations using simple callback style.

Closing this then. Feel free to comment or create a new issue if this is not resolved.