title: android: Save to database in separate thread causes exception db is closed
body: <p>So Im trying to save data to database using separate thread but it causes an exception:</p>
  
  <blockquote>
    <p>java.lang.IllegalStateException: database not open</p>
  </blockquote>
  
  <p>I assume this is not a task for a <code>Loader</code> since I don't need any resulting cursor, it has just to save my data. The save to db code looks as follows:</p>
  
  <pre><code>Thread thread = new Thread(new Runnable() {
      @Override
      public void run() {
          dbHelper.saveMessages(messages);
      }
  });
  thread.setPriority(Thread.MIN_PRIORITY);
  thread.start();
  </code></pre>
  
  <p>If I remove this DB operation out from a Thread everything works good but it causes UI lags. The same things happens if I'm trying to read from DB from a separate thread.<br>
  this is a part of a DB writing code:</p>
  
  <pre><code>    SQLiteDatabase db = dbAdapter.getWritableDatabase());
      try {
          if (existingMessage != null)
              insertId = db.update(Tables.Messages.TABLE_NAME, values, Tables.Messages.SERVER_ID + "=?", new String[]{String.valueOf(message.serverId)});
          else
              insertId = db.insertOrThrow(Tables.Messages.TABLE_NAME, null, values);
      } catch (Exception e) {
          e.printStackTrace();
      }
  </code></pre>
  
  <p>Actually exception happens at db.update() or at db.insertOrThrow() line. Its not guaranteed to throw exception but it does from time to time. It could write for instance 5 objects and then fail on 6th.<br>
  What am I doing wrong or whats wrong with SQLiteOpenHelper?
  The full exception log:</p>
  
  <pre><code>10-30 22:34:52.746  10431-10536/com.myapp W/System.err﹕ java.lang.IllegalStateException: database not open
  10-30 22:34:52.746  10431-10536/com.myapp W/System.err﹕ at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1224)
  10-30 22:34:52.746  10431-10536/com.myapp W/System.err﹕ at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184)
  10-30 22:34:52.746  10431-10536/com.myapp W/System.err﹕ at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1264)
  10-30 22:34:52.746  10431-10536/com.myapp W/System.err﹕ at com.myapp.db.DatabaseHelper.saveMessages(DatabaseHelper.java:478)
  10-30 22:34:52.746  10431-10536/com.myapp W/System.err﹕ at com.myapp.db.DatabaseHelper.requestMessages(DatabaseHelper.java:1245)
  10-30 22:34:52.746  10431-10536/com.myapp W/System.err﹕ at com.myapp.activity.LoginActivity$9.run(LoginActivity.java:425)
  10-30 22:34:52.746  10431-10536/com.myapp W/System.err﹕ at java.lang.Thread.run(Thread.java:1096)
  </code></pre>
  
  <p><strong>UPDATE:</strong><br>
  I'd discovered that its all about reading from db actually. In my code first I read from db to ensure there is no such record. After that I decide what to do - insert or update. And the issue happens with readable db. Right after db is opened for read and the next line should read from db it appears "not opened". And there is no thread for reading, everything happens in one thread: 1st read then write. However reading randomly fails from time to time. So its not writing as I mentioned before.</p>