I have found it necessary to take an existing application that is using ORMLite, and modify it so that if a user signs out and a different user signs in, they each have their own separate database with the same schema using the same helper class, in order that each user have their own separate data. I did consider doing this all using only one database, but decided against that due to wanting to keep the db schema simple, preserve existing code and avoid db migrations.
After a good bit of work, I have found a process that seems to work, and just wanted to check to see if I doing this properly.
I am using a class DatabaseHelper that extends OrmLiteSqliteOpenHelper.
It has a constructor:
public DatabaseHelper(Context context) {
super(context, SettingsManager.getDatabaseName(), null, SettingsManager.DATABASE_VERSION);
}
Where SettingsManager.getDatabaseName() returns a db name with the user id prepended to it.
When first trying to get this to work, I would call close() on an instance of the database helper on user signout, and then on user sign in call
OpenHelperManager.getHelper(context, DatabaseManager.class);
but that would inevitably fail with a "java.lang.IllegalStateException: attempt to re-open an already-closed object" exception, regardless of calls to getConnectionSource().releaseConnection(...) or anything similar. Using OpenHelperManager.getHelper would only call my DatabaseHelper constructor the first time it was called.
I eventually got this to work by simply never closing any OrmLiteSqliteOpenHelper or connection source, and on user sign out nulling out the instance of DatabaseHelper I have, and on user sign in, resetting the instance of DatabaseHelper to "new DatabaseHelper(context)", avoiding all usage of OpenHelperManager.getHelper().
This seems to work in initial testing, but I have the feeling I am not doing this as cleanly as I could, and do not know what the side effects of leaving connections open or not using OpenHelperManager.getHelper might be. Any advice about this or similar experiences would be appreciated. Thank you.