1

I have incorporated Database Backup and restore in my App. This all works fine except that when I restore the database, due to the use of a singleton DBHelper along with only closing the database when the App closes, the in-memory copy of the Database continues to be used after the restore.

Closing the App and re-starting uses the new database. However, rather than tell the user to do this. I would like to seamlessly access the restored database.

This is the code that detects and then reports upon a successful restore :-

                if(copytaken && origdeleted && restoredone) {
                    errlist.add("Database successfully restored." +
                            "\n\nYou should close the ShopWise App and then restart it.");
                    resulttitle = "Restore was successful.";
                    //DBHelper.reopen(context); <== implemented as below
                }
                ..... Displays dialog with text from above

This is the DBHelper (note expand method is used to create/amend the tables)

class DBHelper extends SQLiteOpenHelper {

    private static final String DBNAME = DBConstants.DATABASE_NAME;

    /**
     * Consrtuctor
     *
     * @param context activity context
     * @param name    database name
     * @param factory cursorfactory
     * @param version database version
     */
    DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    /**
     * Instantiates a new Db helper.
     *
     * @param context the context
     */
    DBHelper(Context context) {
        super(context, DBConstants.DATABASE_NAME, null, 1);
    }

    private static DBHelper instance;

    /**
     * Gets helper.
     *
     * @param context the context
     * @return the helper
     */
    static synchronized DBHelper getHelper(Context context) {
        if(instance == null) {
            instance = new DBHelper(context);
        }
        return instance;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        expand(db, false);
    }
    .......

Thinking that you should close the database an reopen it. I tried adding a reopen method in the DBHelper as follows:-

public static void reopen(Context context) {
        instance.close();
        instance = null;
        instance = new DBHelper(context);
    }

and then invoked this from within the code when the restore was OK (as per comment). However, this results in the following :-

java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase:

02-16 16:41:20.938 2683-3050/mjt.shopwise E/SQLiteLog: (28) file unlinked while open: /data/data/mjt.shopwise/databases/ShopWise
02-16 16:41:25.171 2683-2683/mjt.shopwise D/AndroidRuntime: Shutting down VM
02-16 16:41:25.171 2683-2683/mjt.shopwise E/AndroidRuntime: FATAL EXCEPTION: main
                                                            Process: mjt.shopwise, PID: 2683
                                                            java.lang.RuntimeException: Unable to resume activity {mjt.shopwise/mjt.shopwise.MainActivity}: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/mjt.shopwise/databases/ShopWise
                                                                at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2986)
                                                                at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017)
                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                at android.os.Looper.loop(Looper.java:135)
                                                                at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                at java.lang.reflect.Method.invoke(Method.java:372)
                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                             Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/mjt.shopwise/databases/ShopWise
                                                                at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
                                                                at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1312)
                                                                at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
                                                                at mjt.shopwise.DBCommonMethods.getTableRows(DBCommonMethods.java:106)
                                                                at mjt.shopwise.DBCommonMethods.getTableRows(DBCommonMethods.java:59)
                                                                at mjt.shopwise.DBCommonMethods.getTableRowCount(DBCommonMethods.java:29)
                                                                at mjt.shopwise.DBCommonMethods.getTableRowCount(DBCommonMethods.java:43)
                                                                at mjt.shopwise.DBShopMethods.getShopCount(DBShopMethods.java:44)
                                                                at mjt.shopwise.MainActivity.getDBCounts(MainActivity.java:207)
                                                                at mjt.shopwise.MainActivity.onResume(MainActivity.java:163)
                                                                at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1257)
                                                                at android.app.Activity.performResume(Activity.java:6076)
                                                                at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2975)
                                                                at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017) 
                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347) 
                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                at android.os.Looper.loop(Looper.java:135) 
                                                                at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Cœur
  • 37,241
  • 25
  • 195
  • 267
MikeT
  • 51,415
  • 16
  • 49
  • 68

2 Answers2

2

The trick is very simple, do not close the database just reset the DBHelper.

So the reopen method could be:-

public static void reopen(Context context) {
        instance = new DBHelper(context);
    }

Of course, you could also do away with the text telling the user to close and restart the App.

So the code that detects and reports on a successful restore could be:-

if(copytaken && origdeleted && restoredone) {
                    errlist.add("Database successfully restored.");
                    resulttitle = "Restore was successful.";
                    DBHelper.reopen(context); <== implemented as below
                }
MikeT
  • 51,415
  • 16
  • 49
  • 68
  • Quastion asked 5min ago and you answer your own question instantly? – Vygintas B Feb 16 '17 at 06:02
  • @VygintasB he posted this question because it will help some one who will face this similar issue, we have to appreciate it :) – Manohar Feb 16 '17 at 06:12
  • Answering your own questions can be helpful. After doing a search and not finding anything and then determining the resolution, it could help others. That's why there is an Answer your own question checkbox available when asking a question as per *"Answer your own question – share your knowledge, Q&A-style"* – MikeT Feb 16 '17 at 06:13
  • @MikeT SO is place to get an answer,if we can find an answer here we have to add it after finding it – Manohar Feb 16 '17 at 06:49
0

the reopen code that worked for me was:

public void reopen(Context context, String DbFilePath) {
        instance = new DBHelper(context, DbFilePath);
    }

the difference from the previous answer is the removal of the "static" from the function definition which required the instance to be static as well. this prevented the actual reopening of the database

Elad
  • 1,523
  • 13
  • 10