title: When to close Sqlite database
body: <p>I have some listviews in every activity in my app. For every getView(), I create a thread to open a Sqlite database and close it after(in each thread). But, I got database locked error.</p>
  
  <p><strong>Note: The app can have like 6 threads running at the same time using each there own SQLiteOpenHelper</strong> </p>
  
  <p>So when Android said to close the database when not needed, is he talking about closing when the app is in background or for every request using transaction or not?</p>
  
  <p>Code:</p>
  
  <pre><code>public class TemporarySqliteDB extends SQLiteOpenHelper{
  
    public TemporarySqliteDB(Context context) {
      super(context, "Temporary", null, 1);
    }
    //..........
  }
  
  public class DaoImage{
    private SQLiteDatabase writableDatabase;
  
    public DaoImage(Context ctx) {
      writableDatabase = new TemporarySqliteDB(ctx).getWritableDatabase();
    }
  
    public void close() {
      writableDatabase.close();
    }
   }
  </code></pre>
  
  <p>In my thread run()</p>
  
  <pre><code>//this give locked error while instanciating
  DaoImage daoImage = null;
              try {
                  daoImage = new DaoImage(ctx.get());
  
                  //.....
              }finally {
                  if(daoImage != null)
                      daoImage.close();
              }
  
  //This without error
  DaoImage daoImage = new DaoImage(ctx.get());
  //....
  </code></pre>
  
  <p><strong>Without closing the database I see the memory use is ok. But I don't know if it's best practice to leave the database open.</strong></p>