Android Question IllegalStateException: attempt to re-open an already-closed object

Creideiki

Active Member
Licensed User
Longtime User
Hi,

I get sporadically this error.

Erel wrote here that one should initialize the SQL object in the Starter and never re-initialize it.
I do initialize it the first time in Service_Create of the Starter Service.
But I backup the database file now and then. So I have to
B4X:
Sub DatenSicherung()
    ' ...
    Main.SQL1.close
    File.Copy(Main.CSVdir.internPath, Main.DatabaseName, zielDir, "DatenSicherung.db")
    Main.SQL1.Initialize(Main.CSVdir.internPath, Main.DatabaseName, True)
End Sub
The Sub resides in a code class and is called by several Activities.

I don't think it's a good idea to copy the file without closing it.

But what's the right way to do that without closing and re-initializing SQL?
 

Creideiki

Active Member
Licensed User
Longtime User
Make sure that the journal mode is not set to wal as the database will be made of three files.
Oh... there is a journal mode? How can one set that?

Better to put the global variable in the Starter service, though it is not related to this error.
Does it matter, in which Sub Process_Globals a variable is declared? At the moment it is declared in Main, but initialized in Starter.

You can close and open the database. Are you using any async methods? Your code will break them.
I do use async methods; though not very often yet. The query which seems to crash sometimes ist not async though.
Under what circumstances async methods are breaking?

Is there a way to ensure the database is opened correctly? I could test IsInitialized before each call, but that woud be quite expensive.
Would it be useful to test IsInitialized after the Initialize-Call?
Would it help to call a sub in Starter which makes the Close/Copy/Initialize-Sequence?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Oh... there is a journal mode? How can one set that?

Does it matter, in which Sub Process_Globals a variable is declared? At the moment it is declared in Main, but initialized in Starter.
Only for better code organization.

Under what circumstances async methods are breaking?
Closing the SQL object while there is an ongoing async method will break the async method.

Note that you don't really need to close the SQL object before making a backup.
 
Upvote 0

Creideiki

Active Member
Licensed User
Longtime User
Ok, sorry... and interesting. Though not for this project.
Closing the SQL object while there is an ongoing async method will break the async method.
Well... that's plausible. I'm quite sure that's not my problem.
Note that you don't really need to close the SQL object before making a backup.
Oh, really? That means I can be definitivly sure the database file is in a consistent state as far as no transaction is in progress?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Ok, sorry... and interesting. Though not for this project.
It is. You should set the journal mode to DELETE.

That means I can be definitivly sure the database file is in a consistent state as far as no transaction is in progress?
Yes. Even if there is a transaction.
 
Upvote 0
Top