SQLiteOpenHelper getWritableDatabse() fails with no Exception

Posted by Michal K on Stack Overflow See other posts from Stack Overflow or by Michal K
Published on 2012-06-10T22:06:42Z Indexed on 2012/06/10 22:40 UTC
Read the original article Hit count: 213

Filed under:
|
|

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 ass 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.bananout.Bananout@407152c8 / instance: com.bananout.helpers.DatabaseHelper@40827560
06-10 23:49:59.631: I/DBHELPER2(26499): cxtcom.bananout.Bananout@407152c8 / instance: com.bananout.helpers.DatabaseHelper@40827560
06-10 23:49:59.631: I/DetailsLoader(26499): Get event details offline / db helper: com.bananout.helpers.DatabaseHelper@40827560
06-10 23:49:59.631: I/DBHELPER1(26499): cxtcom.bananout.Bananout@407152c8 / instance: com.bananout.helpers.DatabaseHelper@40827560
06-10 23:49:59.651: I/DBHELPER2(26499): cxtcom.bananout.Bananout@407152c8 / instance: com.bananout.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 this line SQLiteDatabase db=DatabaseHelper.getInstance(this.getContext()).getWritableDatabase(); gets executed.

What can possibly go wrong on this line?

© Stack Overflow or respective owner

Related posts about android

Related posts about sqlite