I get a android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here when starting a new Activity using OrmLite.
I’m not using any Cursors myself, so it must be the low level code that ORMLite uses. Not sure what the relevant code would be here, but basically I’m using Query/DeleteBuilders with 2 Dao objects usually inside a transaction manager. Minimal example that leads to the problem (exception handling removed):
return new TransactionManager(connectionSource).callInTransaction(new Callable<List<ConversionData>>() {
public List<ConversionData> call() throws Exception {
QueryBuilder<ConversionData, Date> builder = getDataDao().queryBuilder();
builder.orderBy("date", /* desc */ true);
return builder.query();
}
});
Since neither the Dao nor the Builders have any close methods I’m not sure where exactly I should actually close.
There’s a close method in my Activity that extends OrmLiteBaseActivity, but then how would I open it again when I return from the other activity?
So after some back and forth, this turned out to be a problem with a data iterator on a lazy collection that was not being closed. @Voo was doing something like the following:
This problem happens once and a while. The documentation for iterators is explicit about it. To quote:
The issue with lazy collections is also documented:
To debug this problem, we first turned the lazy loaded collection into an eager one with
@ForeignCollection(eager = true). That removed the problem indicating that it probably was a improper break out of aforloop or some other bad pattern.Good lesson for others.