SQLiteOpenHelper getWritableDatabse() fails with no Exception
Asked Answered
O

0

1

I have a very strange problem. It only shows from time to time, on several devices. Can't seem to reproduce it when I want, but had it so many times, that I think I know where I get it.

So I have a Loader which connects to sqlite through a singleton SQLiteOpenHelper:

try {
    Log.i(TAG, "Get details offline / db helper: "+DatabaseHelper.getInstance(getContext()));
    SQLiteDatabase db=DatabaseHelper.getInstance(this.getContext()).getWritableDatabase();
    Log.i(TAG, "Get details offline / db: "+db);
    //doing some work on the db...
} catch(SQLiteException e){
    e.printStackTrace();
    return null;
} catch(Exception e){
    e.printStackTrace();
    return null;
    //trying everything to grab some exception or whatever
}

My SQLIteOpenHelper looks something like this:

public class DatabaseHelper extends SQLiteOpenHelper { 

    private static DatabaseHelper mInstance = null;
    private static Context mCxt;

    public static DatabaseHelper getInstance(Context cxt) {
        //using app context as suggested by CommonsWare
        Log.i("DBHELPER1", "cxt"+mCxt+" / instance: "+mInstance);
        if (mInstance == null) {
            mInstance = new DatabaseHelper(cxt.getApplicationContext());
        }
        Log.i("DBHELPER2", "cxt"+mCxt+" / instance: "+mInstance);
        mCxt = cxt;
        return mInstance;
    }

    //private constructor
    private DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.mCxt = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //some tables created here
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //upgrade code here
    }
}

It really works great in most cases. But from time to time I get a log similar to this:

06-10 23:49:59.621: I/DBHELPER1(26499): cxtcom.myapp@407152c8 / instance: com.myapp.helpers.DatabaseHelper@40827560
06-10 23:49:59.631: I/DBHELPER2(26499): cxtcom.myapp@407152c8 / instance: com.myapp.helpers.DatabaseHelper@40827560
06-10 23:49:59.631: I/DetailsLoader(26499): Get event details offline / db helper: com.myapp.helpers.DatabaseHelper@40827560
06-10 23:49:59.631: I/DBHELPER1(26499): cxtcom.myapp@407152c8 / instance: com.myapp.helpers.DatabaseHelper@40827560
06-10 23:49:59.651: I/DBHELPER2(26499): cxtcom.myapp@407152c8 / instance: com.myapp.helpers.DatabaseHelper@40827560

This line Log.i(TAG, "Get details offline / db: "+db); never gets called! No Exceptions, silence. Plus, the thread with the Loader is not running anymore.

So nothing past the line:

SQLiteDatabase db=DatabaseHelper.getInstance(this.getContext()).getWritableDatabase(); 

gets executed.

What can possibly go wrong on this line?

Ocreate answered 10/6, 2012 at 22:6 Comment(6)
uhm, getWritableDatabase throws error when you cannot open database for write, specify closer when it throw this error. For what interaction with user.Lashondra
Well it never does, that's the weird thing. As you can see in my code I even tried to catch both SQLiteException (which should be thrown according to docs) and Exception. This part of the Log I posted - that's it - nothing else shows... The app is responsive, but the Loader thread becomes deadVarion
It might be a memory leak... this sounds especially likely since you seem to have a difficult time replicating the problem consistently. Try making the Context non-static and remove the line mCxt = cxt; from the getInstance method... if it doesn't help at all, update your code and I can take another look.Diathermy
Funny thing is I had it this way for quite some time and gc behaved ok. Never found any leak and I check every now and then. But I updated the code with normal cxt and will try to reproduce it today. If it does work - great :-)Varion
It's really hard to tell. I solved it and a bunch of other problems, but I really don't know which solution worked for which problem;) I made the context non-static, removed all sqlite.close() from my code and also changed the logic behind my, apparently, rubbish concept for offline/online Loader. Btw, if you had a minute, you could take a look at this concept here - #10856808.Varion
I see you've worked around this, but I thought I'd add that when I run into stuff like this I catch Throwable, which is really everything that can be thrown.Ratafia

© 2022 - 2024 — McMap. All rights reserved.