Android Question SQLite database closed?

FrankBerra

Active Member
Licensed User
Longtime User
Hello forum!

I developed an app that relies on a local sqlite database.
It works almost always but on some random devices i log strange logs/Crashes on Play Developer Console.

The crashes says:
B4X:
java.lang.RuntimeException:
at anywheresoftware.b4a.keywords.Common$13.run (Common.java:1692)
at android.os.Handler.handleCallback (Handler.java:836)
at android.os.Handler.dispatchMessage (Handler.java:103)
at android.os.Looper.loop (Looper.java:203)
at android.app.ActivityThread.main (ActivityThread.java:6251)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1073)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:934)
Caused by: java.lang.IllegalStateException:
at android.database.sqlite.SQLiteClosable.acquireReference (SQLiteClosable.java:55)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory (SQLiteDatabase.java:1344)
at android.database.sqlite.SQLiteDatabase.rawQuery (SQLiteDatabase.java:1287)
at anywheresoftware.b4a.sql.SQL.ExecQuery2 (SQL.java:223)
at myproject.me.funzioni._vvvvvvvvvvvv1 (funzioni.java:2507)
at myproject.me.main._settanotifica_numeroconversazioninonlette (main.java:12816)
at myproject.me.main$ResumableSub_Carica_App_LayoutBase.resume (main.java:3915)
at anywheresoftware.b4a.keywords.Common$13.run (Common.java:1690)

It seems that the database inizialized in starter service has been closed without reason.
Someone on internet says that "you must close your db as soon as you complete your transaction or else your app may throw database object leaked exception if you keep your db open for so long"
(https://stackoverflow.com/questions...lstateexception-attempt-to-re-open-an-already)

Someone else says: "My solution was to catch the exception and re-open the database if needed, and then try the transaction again."
(https://stackoverflow.com/questions...n-an-already-closed-object-what-might-be-clos)

Anyone experienced similar issues and found a solution?
The solution to test if the database is initialized seems a good workaround but i am wondering if there is a more elegant way to keep the database always opened due to the fact it is initialized at startup in starter service.

Thanks in advance for your suggestions
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Use crashlytics. It will provide more information.

It seems that the database inizialized in starter service has been closed without reason.
Is it initialized in Service_Create of the starer service? Are you sure that there aren't any other instances of SQL? Search your code for "as SQL" to check it.
 
Upvote 0

FrankBerra

Active Member
Licensed User
Longtime User
in Service_Create of starter service i call a sub that initializes the Database.

What do you mean with "there aren't any other instances of SQL" ?
Somewhere in the app i initialize the database again if i try to add a column in a table of database:
B4X:
Try
   Starter.Database.ExecQuery("SELECT [" & Column & "] FROM utenti")
Catch '
           Try
               Starter.Database.ExecNonQuery("ALTER TABLE utenti ADD COLUMN [" & Column & "] TEXT")
           Catch

           End Try
           CallSubDelayed2(Starter, "Initialize_Database")
End Try
are you referring to this?

"as SQL" is present only in Starter service and nowere else
 
Upvote 0

FrankBerra

Active Member
Licensed User
Longtime User
Maybe not all cursors are closed...now i double check also with the help of StrictMode Lib.
In the past during some tests i noticed that if i add new columns to a table they were not "visible" until i reinitialize the database, so that's why i initialize it again.
But no, i don't close it before reinitialization. Should i close it before?
 
Upvote 0
Top