title: Android Database helper singleton and synchronize
body: <p>I have created a DB helper using SQLiteOpenHelper. I have created the same in Singleton pattern to stop creating multiple the database object. As there is database operation from multiple threads, I maintain the object creation thread-safe, I make it synchronized. </p>
  
  <pre><code>    public class DatabaseManager{
  
  
      private static final String     DATABASE_NAME = "dbname";
  
      private static final int        DATABASE_VERSION = 1;
  
      private DatabaseHelper   helper;
      private SQLiteDatabase   db;
      private Context          context;
  
      private DatabaseManager(Context c){
          this.context = c;
          helper = new DatabaseHelper(context); 
      }
  
  
      private static final Object lock = new Object();
      private static volatile DatabaseManager instance;
  
      public static DatabaseManager getInstance(Context c){
      DatabaseManager r = instance;
      if (r == null) {
          synchronized (lock) {    // While we were waiting for the lock, another 
              r = instance;        // thread may have instantiated the object.
              if (r == null) {  
                  r = new DatabaseManager();
                  instance = r;
              }
          }
      }
      return r;
     }
  
  
  
  
      public Cursor getAll() {
              SQLiteDatabase db = _openHelper.getReadableDatabase();
              if (db == null) {
                  return null;
              }
              return db.rawQuery("select * from todos order by priority, title", null);
      }
  
      public void delete(long id) {
              SQLiteDatabase db = _openHelper.getWritableDatabase();
              if (db == null) {
                  return;
              }
              db.delete("todos", "_id = ?", new String[] { String.valueOf(id) });
              db.close();
      }
  
      public long add(String title, int priority) {
              SQLiteDatabase db = _openHelper.getWritableDatabase();
              if (db == null) {
                  return 0;
              }
              ContentValues row = new ContentValues();
              row.put("title", title);
              row.put("priority", priority);
              long id = db.insert("todos", null, row);
              db.close();
              return id;
      }
  
      public void update(long id, String title, int priority) {
              SQLiteDatabase db = _openHelper.getWritableDatabase();
              if (db == null) {
                  return;
              }
              ContentValues row = new ContentValues();
              row.put("title", title);
              row.put("priority", priority);
              db.update("todos", row, "_id = ?", new String[] { String.valueOf(id) } );
              db.close();
      }
  
  
      class DatabaseHelper extends SQLiteOpenHelper{
  
          DatabaseHelper(Context context){
                  super(context, DATABASE_NAME, null, DATABASE_VERSION);
          }
  
          @Override
          public void onCreate(SQLiteDatabase db){            
                  db.execSQL("create table todos (_id integer primary key autoincrement, title text, priority integer)");
          }
  
          @Override
          public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
  
          }
      }
  }
  </code></pre>
  
  <p>Now should I need to synchronized get(), add(), update(), delete() methods also for making them thread-safe?</p>