1

I have singleton database helper to access db. This part has no problem. However, I doubt that async threads of reading and writing/deleting ends up with problem.

If one thread is reading, and the other one is deleting; I am suspicious about reading one cannot read the value before deletion. Can anybody confirm this? And what should be the solution way for achieving this with singleton helper?

Any help is appreciated, thanks

public CategoryDatabaseConnection(Context context) {
        mDbOpenHelper = CategoryDatabaseOpenHelper.getInstance(context, null, null, 0);
        mOpenCounter = mDbOpenHelper.mOpenCounter;
    }

    public void open() throws SQLException {
        // open database in reading/writing mode
        int value = mOpenCounter.incrementAndGet();
         if(value == 1 || mDatabase==null) {
                // Opening new database
             mDatabase = mDbOpenHelper.getWritableDatabase();
            }
    }
tugce
  • 651
  • 3
  • 13
  • 28

1 Answers1

0

You database helper must be synchronized so that only one thread can access it at a time.

To implement synchronization just put keywod synchronized before your class.

Ex.

public static synchronized singletonDBHelper()
{
 // your code
}
Vipin Sharma
  • 594
  • 5
  • 19