Right now, to make sure that I'm writing to a correct and functional database at all times, I'm calling the following createDB function every time when I'm accessing the database for data insertion (which happens on a 15 second interval):
protected void createDB(String dbName){
// clear the singleton if forcing a switch
try{
mdbHelper = new DaoMaster.DevOpenHelper(mcontext, dbName, null);
database = mdbHelper.getWritableDatabase();
mdaoMaster = new DaoMaster(database);
dbSessionInstance = mdaoMaster.newSession();
}catch (Exception e){
System.out.println(TAG + e.getMessage());
}
}
This will attempt to create a database with the name given to dbName if it doesn't already exist; if it exists, then it'll just use it.
There are a few scenarios I would like to consider:
1) When the database app crashes, do I have to do anything to make sure the database doesn't get messed up?
2) Does it do more good or harm if I regularly and often call createDB?
3) Just in case, how do I safely perform secure database closures in the event that accidents happen?
When an app crashes, there's nothing it can do because it is no longer running.
As long as you keep the database open, it still exists.
Trying to (re)create the DB repeatedly while your app is running makes sense only if you suspect that somebody else is deleting the DB.
The boundary for guaranteed durability are not database connection but transactions. In SQLite, transactions are designed so that the database is in a consistent state even if the app crashes during a transaction (i.e., uncommitted transactions are rolled back when the DB is opened afterwards).