title: Singleton database: "attempt to re-open an already-closed object: SQLiteDatabase” when working from both app and service
body: <p>So I have two contexts (service and UI activity) running from the same process, and also I use traditional's wrapped way to get write handle:</p>
  
  <pre><code>package me.soexample.sobad;
  import java.util.concurrent.atomic.AtomicInteger;
  
  import android.content.Context;
  import android.database.sqlite.SQLiteDatabase;
  
  import android.database.sqlite.SQLiteOpenHelper;
  import android.util.Log;
  public class DatabaseManager {
  
      private static AtomicInteger mOpenCounter = new AtomicInteger();
  
      private static DatabaseManager instance;
      private static DatabaseHelper mDatabaseHelper;
      private SQLiteDatabase mDatabase;
  
      public static synchronized void initializeInstance(DatabaseHelper helper) {
          if (instance == null) {
              instance = new DatabaseManager();
              mDatabaseHelper = helper;
          }
      }
  
      public static synchronized DatabaseManager getInstance() {
          if (instance == null) {
              throw new IllegalStateException(DatabaseManager.class.getSimpleName() +
                      " is not initialized, call initializeInstance(..) method first.");
          }
  
          return instance;
      }
  
  
  
      public SQLiteDatabase openDatabase() {
          if(mOpenCounter.incrementAndGet() == 1) {
              // Opening new database
              mDatabase = mDatabaseHelper.getWritableDatabaseAlt();
          }
          return mDatabase;
      }
  
      public void closeDatabase() {
          if(mOpenCounter.decrementAndGet() == 0) {
              // Closing database
              mDatabase.close();
  
          }
      }
  }
  </code></pre>
  
  <p>As if it can not be expected, it often results in:</p>
  
  <pre><code>04-11 04:24:51.738: E/gsmspy(24331): java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /storage/sdcard0/my.db
  04-11 04:24:51.738: E/gsmspy(24331):    at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
  04-11 04:24:51.738: E/gsmspy(24331):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1310)
  04-11 04:24:51.738: E/gsmspy(24331):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
  04-11 04:24:51.738: E/gsmspy(24331):    at me.soexample.sogood.somethod2(sogood.java:51)
  04-11 04:24:51.738: E/gsmspy(24331):    at me.soexample.sogood.somethod(sogood.java:41)
  </code></pre>
  
  <p>As you can see, there are no references to DatabaseManager; so it seems to be a multithreading problem.
  Now, what do I do to have a guaranteed ability to write to DB object returned from such wrapper?</p>