title: SQLite exception: Database is locked issue
body: <p>I'm having an issue with my SQLite database inside an Android app. It seems to happen every so often and I cannot reproduce it, but this is the report the Android Market gives.</p>
  
  <p>Basically I have first a splash screen activity that starts by checking if all the needed data is in the database. If not, it will install the data in a asynchronous thread and close the db connection.</p>
  
  <p>Then and only then can the main activity be started, which also opens and reads/writes the database.</p>
  
  <p>This code is executed from the onCreate method of the splash screen activity:</p>
  
  <pre><code>dbWord = new WordDBAdapter(this, myUI);
  dbWord.open();
  if (dbWord.isDataInstalled()) {
      dbWord.close();
      databaseInstalled = true;
      clickToStartView.setText(myUI.PRESS_TO_START);
      clickToStartView.startAnimation(myBlinkAnim);
  } else {
      // If not, try to install in asynchronous thread
      progressDialog = new ProgressDialog(this);
      progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
      progressDialog.setMessage(myUI.INSTALLING_DB);
      progressDialog.setCancelable(false);
      new InstallDBData().execute("");
  }
  </code></pre>
  
  <p>The code for the asynchronous thread is:</p>
  
  <pre><code>private class InstallDBData extends AsyncTask&lt;String, Integer, Integer&gt; {
  
      @Override
      protected Integer doInBackground(String... arg0) {
          progressDialog.setProgress(0);
          dbWord.installData(0, getApplicationContext());
          progressDialog.setProgress(20);
          dbWord.installData(1, getApplicationContext());
          progressDialog.setProgress(40);
          dbWord.installData(2, getApplicationContext());
          progressDialog.setProgress(60);
          dbWord.installData(3, getApplicationContext());
          progressDialog.setProgress(80);
          dbWord.installData(4, getApplicationContext());
          progressDialog.setProgress(100);
          return 1;
      }
  
      protected void onPreExecute() {
          progressDialog.show();
      }
  
      protected void onPostExecute(Integer x) {
          dbWord.close();
          progressDialog.hide();
          databaseInstalled = true;
          clickToStartView.setText(myUI.PRESS_TO_START);
          clickToStartView.startAnimation(myBlinkAnim);
      }
  }
  </code></pre>
  
  <p>These are important parts of the WordDBAdapter class, which is also used by the main activity:</p>
  
  <pre><code>public class WordDBAdapter {
      private DatabaseHelper mDbHelper;
      private SQLiteDatabase mDb;
  
      public WordDBAdapter open() throws android.database.SQLException {
          mDbHelper = new DatabaseHelper(mCtx);
          mDb = mDbHelper.getWritableDatabase();
          return this;
      }
  
      public void close() {
          mDbHelper.close();
      }
      ...
  }
  </code></pre>
  
  <p>I'm getting the following exceptions, which are similar but have a different message:</p>
  
  <p>First type of error message:</p>
  
  <pre><code>java.lang.RuntimeException: Unable to start activity 
    ComponentInfo{example.flashcards.medical/com.example.flashcards.common.SplashWindow}: 
    android.database.sqlite.SQLiteException: database is locked: BEGIN EXCLUSIVE;
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1830)
  ....
  Caused by: android.database.sqlite.SQLiteException: database is locked: BEGIN EXCLUSIVE;
  at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
  at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1870)
  at android.database.sqlite.SQLiteDatabase.beginTransactionWithListener(SQLiteDatabase.java:602)
  </code></pre>
  
  <p>Second type of error message:     </p>
  
  <pre><code>java.lang.RuntimeException: Unable to start activity 
    ComponentInfo{example.flashcards.medical/com.example.flashcards.common.SplashWindow}: 
    android.database.sqlite.SQLiteException: database is locked
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
  ....
  Caused by: android.database.sqlite.SQLiteException: database is locked
  at android.database.sqlite.SQLiteDatabase.native_setLocale(Native Method)
  at android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java:1987)
  at android.database.sqlite.SQLiteDatabase.&lt;init&gt;(SQLiteDatabase.java:1855)
  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
  at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)
  </code></pre>
  
  <p>I would really like to not have to create a ContentProvider, because I believe its overkill if a simpler solution is available. Also, only this application has access to the database.</p>
  
  <p>Any suggestions on how this can be fixed?</p>