Software design: where to put database open and close


  1. public class DBManager
  2. {
  3.     // Variable to hold the database instance
  4.     private SQLiteDatabase db;
  5.     // Database open/upgrade helper
  6.     private DatabaseHelper dbHelper;
  7.  
  8.     public DBManager(Context _context)
  9.     {
  10.         Log.v("DBManager", "constructor");
  11.         dbHelper = new DatabaseHelper(_context, SqlConstants.DATABASE_NAME, null, SqlConstants.DATABASE_VERSION);
  12.     }
  13.  
  14.     public DBManager open() throws SQLException
  15.     {
  16.         Log.v("DBManager", "open");
  17.         db = dbHelper.getWritableDatabase();
  18.         return this;
  19.     }
  20.  
  21.     public void close()
  22.     {
  23.         Log.v("DBManager", "close");
  24.         db.close();
  25.     }
  26.  
  27.         ...
  28.  
  29.         /**
  30.      * Query all forms available locally.
  31.      * @return A list with all forms (form.name and form.FormId) available on local db
  32.      * or null if there was a problem.
  33.      */
  34.     public ArrayList<Form> getAllForms()
  35.     {
  36.         Log.v("DBManager", "getAllForms");
  37.         ArrayList<Form> list = null;
  38.         Cursor c = null;
  39.  
  40.         try
  41.         {
  42.             c = this.getAllFormsCursor();
  43.             if (c != null)
  44.             {
  45.                 int formNameIndex = c.getColumnIndex(SqlConstants.COLUMN_FORM_NAME);
  46.                 int formIdIndex = c.getColumnIndex(SqlConstants.COLUMN_FORM_ID);
  47.  
  48.                 c.moveToFirst();
  49.                 if (c.getCount() > 0)
  50.                 {
  51.                     list = new ArrayList<Form>(c.getCount());
  52.                     do
  53.                     {
  54.                         Form f = new Form();
  55.                         f.Name = c.getString(formNameIndex);
  56.                         f.FormId = c.getString(formIdIndex);
  57.                         list.add(f);
  58.                     }
  59.                     while (c.moveToNext());
  60.                 }
  61.             }
  62.         }
  63.         catch (Exception e)
  64.         {
  65.             e.printStackTrace();
  66.             list = null;
  67.         }
  68.         finally
  69.         {
  70.             if (c != null)
  71.                 c.close();
  72.         }
  73.         return list;
  74.     }
  75.  
  76.     private Cursor getAllFormsCursor()
  77.     {
  78.         Log.v("DBManager", "getAllFormsCursor");
  79.  
  80.         return db.query(SqlConstants.TABLE_FORM,
  81.                 new String[] {
  82.                 SqlConstants.COLUMN_FORM_ID,
  83.                 SqlConstants.COLUMN_FORM_NAME}, null, null, null, null, null);
  84.     }
  85. }
  86.        
  87. private class DbFormListAsyncTask extends AsyncTask<Void, Void, ArrayList<Form>>
  88. {
  89.     private Context mContext;
  90.     private ProgressDialog loadingDialog;
  91.     private DBManager dbMan;
  92.  
  93.     DbFormListAsyncTask(Context context)
  94.     {
  95.         this.mContext = context;
  96.         loadingDialog = new ProgressDialog(mContext);
  97.         loadingDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  98.         loadingDialog.setMessage("Retriving forms. Please wait...");
  99.         loadingDialog.setCancelable(false);
  100.         loadingDialog.show();
  101.     }
  102.  
  103.     @Override
  104.     protected ArrayList<Form> doInBackground(Void... arg0)
  105.     {
  106.         dbMan = new DBManager(mContext);
  107.         dbMan.open();
  108.  
  109.         return dbMan.getAllForms();
  110.     }
  111.  
  112.     protected void onPostExecute(ArrayList<Form> forms)
  113.     {
  114.         if (forms != null)
  115.         {
  116.             ListActivity act = (ListActivity) mContext;
  117.             act.setListAdapter(new AvaFormAdapter(act, R.layout.ava_list_item, forms));
  118.         }
  119.         else
  120.         {
  121.             TextView errorMsg = (TextView)
  122.                     ((FormsListActivity) mContext).findViewById(R.id.formErrorMsg);
  123.             errorMsg.setText("Problem getting forms. Please try again later.");
  124.         }
  125.         loadingDialog.dismiss();
  126.         if (dbMan != null)
  127.             dbMan.close();
  128.     }
  129. }
  130.        
  131. protected ArrayList<Form> doInBackground(Void... arg0)
  132. {
  133.     dbMan = new DBManager(mContext);
  134.     dbMan.open();
  135.     ArrayList<Form> resutl = dbMan.getAllForms();
  136.     dbMan.close();
  137.     return result;
  138. }


Learn More :