title: IllegalStateException: database not open at the second application entering, ORMLite
body: <p>I use my databaseHelper:</p>
  
  <pre><code>@Override
  public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) 
  {
       try
       {  
  
          TableUtils.createTable(connectionSource, Category.class);
          Log.i(DatabaseHandler.class.getName(), "created new entries in onCreate: " );
       }
       catch (SQLException e){
           Log.e(TAG, "error creating DB " + DATABASE_NAME);
           throw new RuntimeException(e);
       }
  }
  
  // Upgrading database
    @Override
     public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVer, int newVer)
    {
         try
          {         
  
             TableUtils.dropTable(connectionSource, Category.class, true);
             onCreate(db, connectionSource);
          }
         catch (SQLException e){
             Log.e(TAG,"error upgrading db "+DATABASE_NAME+"from ver "+oldVer);
             throw new RuntimeException(e);
         }
  
     }
   public Dao&lt;Category, Integer&gt; getCategoryDao() throws SQLException 
      {
          if (simpleCategoryDao == null) {
              simpleCategoryDao = getDao(Category.class);
          }
          return simpleCategoryDao;
      }
  
    public static synchronized DatabaseHandler getHelper(Context context) 
     {
          if (helper == null) 
          {
              helper = new DatabaseHandler(context);
          }
          usageCounter.incrementAndGet();
          return helper;
      }
  </code></pre>
  
  <p>And I get JSON data, wrap it to my model and save it in the ORMLite..My I do it in the AsyncTask inner class. My doInBackground method in it. </p>
  
  <pre><code>        protected Void doInBackground(String... param) {
               ContentRepository _contactRepo;
              UserFunctions userFunction = new UserFunctions();
              json = userFunction.loginUser(param[0], param[1]);
              try {
                  if ((json.getString(KEY_STATUS) != null) &amp; json.getString(KEY_MESSAGE).equals((String)"Success"))
                  {
  
                      String res = json.getString(KEY_STATUS);
                      if(Integer.parseInt(res) == 1)
                      {   
  
                          DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                          JSONParser p = new JSONParser();
                          appendLog(json.toString());
                          DataParsing u = new DataParsing();
                          List&lt;Category&gt; catlist = u.wrapCategories(json);
                          List&lt;ProcessStatus&gt; status = u.wrapStatuses(json);
                          List&lt;Level&gt; level = u.wrapLevels(json);
                          List&lt;Subject&gt; subject = u.wrapSubjects(json);
  
                          _contactRepo=new ContentRepository(getContentResolver(),getApplicationContext());
  
                          OrmLiteSqliteOpenHelper dbHelper=getHelper1();
                          getHelper1().open();
  
                          Dao&lt;Category, Integer&gt; daoCat=dbHelper.getDao(Category.class);
                          QueryBuilder&lt;Category,Integer&gt; catquery = daoCat.queryBuilder();
                          try{
                              getHelper1().open();    
                              if (catquery.query().isEmpty())
                              {
                                  _contactRepo.saveCategories(catlist);
                              }
                          }
                          catch (IllegalStateException e)
                          {
                              getHelper1().open();            
                              e.printStackTrace();
  
                          }
  
                          Log.i("category",catquery.query().toString());
  
                      }   
                  }
  </code></pre>
  
  <p>And getHelper1() method:</p>
  
  <pre><code>private DatabaseHandler getHelper1() {
          if (databaseHandler == null) {
              databaseHandler = DatabaseHandler.getHelper(this);
          }
          return (DatabaseHandler) databaseHandler;
      }
  </code></pre>
  
  <p>When I start the application, everything works fine. When I try to lauch it in second time I get:</p>
  
  <pre><code> 08-21 15:44:48.939: E/AndroidRuntime(1893): FATAL EXCEPTION: AsyncTask #2
  08-21 15:44:48.939: E/AndroidRuntime(1893): java.lang.RuntimeException: An error occured while executing doInBackground()
  08-21 15:44:48.939: E/AndroidRuntime(1893):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
  08-21 15:44:48.939: E/AndroidRuntime(1893):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
  08-21 15:44:48.939: E/AndroidRuntime(1893):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
  08-21 15:44:48.939: E/AndroidRuntime(1893):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
  08-21 15:44:48.939: E/AndroidRuntime(1893):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
  08-21 15:44:48.939: E/AndroidRuntime(1893):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
  08-21 15:44:48.939: E/AndroidRuntime(1893):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
  08-21 15:44:48.939: E/AndroidRuntime(1893):     at java.lang.Thread.run(Thread.java:1019)
  08-21 15:44:48.939: E/AndroidRuntime(1893): Caused by: java.lang.IllegalStateException: database not open
  08-21 15:44:48.939: E/AndroidRuntime(1893):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1343)
  08-21 15:44:48.939: E/AndroidRuntime(1893):     at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1324)
  08-21 15:44:48.939: E/AndroidRuntime(1893):     at com.j256.ormlite.android.AndroidCompiledStatement.getCursor(AndroidCompiledStatement.java:154)
  08-21 15:44:48.939: E/AndroidRuntime(1893):     at com.j256.ormlite.android.AndroidCompiledStatement.runQuery(AndroidCompiledStatement.java:57)
  08-21 15:44:48.939: E/AndroidRuntime(1893):     at com.j256.ormlite.stmt.SelectIterator.&lt;init&gt;(SelectIterator.java:55)
  08-21 15:44:48.939: E/AndroidRuntime(1893):     at com.j256.ormlite.stmt.StatementExecutor.buildIterator(StatementExecutor.java:206)
  08-21 15:44:48.939: E/AndroidRuntime(1893):     at com.j256.ormlite.stmt.StatementExecutor.query(StatementExecutor.java:155)
  08-21 15:44:48.939: E/AndroidRuntime(1893):     at com.j256.ormlite.dao.BaseDaoImpl.query(BaseDaoImpl.java:261)
  08-21 15:44:48.939: E/AndroidRuntime(1893):     at com.j256.ormlite.stmt.QueryBuilder.query(QueryBuilder.java:262)
  08-21 15:44:48.939: E/AndroidRuntime(1893):     at com.assignmentexpert.LoginActivity$LoginTask.doInBackground(LoginActivity.java:329)
  08-21 15:44:48.939: E/AndroidRuntime(1893):     at com.assignmentexpert.LoginActivity$LoginTask.doInBackground(LoginActivity.java:1)
  08-21 15:44:48.939: E/AndroidRuntime(1893):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
  08-21 15:44:48.939: E/AndroidRuntime(1893):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
  08-21 15:44:48.939: E/AndroidRuntime(1893):     ... 4 more
  08-21 15:44:54.589: I/destroy(1893): destroy
  08-21 15:44:54.839: E/WindowManager(1893): Activity com.assignmentexpert.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40668ff0 that was originally added here
  08-21 15:44:54.839: E/WindowManager(1893): android.view.WindowLeaked: Activity com.assignmentexpert.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40668ff0 that was originally added here
  08-21 15:44:54.839: E/WindowManager(1893):  at android.view.ViewRoot.&lt;init&gt;(ViewRoot.java:263)
  08-21 15:44:54.839: E/WindowManager(1893):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
  08-21 15:44:54.839: E/WindowManager(1893):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
  08-21 15:44:54.839: E/WindowManager(1893):  at android.view.Window$LocalWindowManager.addView(Window.java:424)
  08-21 15:44:54.839: E/WindowManager(1893):  at android.app.Dialog.show(Dialog.java:241)
  08-21 15:44:54.839: E/WindowManager(1893):  at android.app.ProgressDialog.show(ProgressDialog.java:107)
  08-21 15:44:54.839: E/WindowManager(1893):  at android.app.ProgressDialog.show(ProgressDialog.java:90)
  08-21 15:44:54.839: E/WindowManager(1893):  at com.assignmentexpert.LoginActivity$LoginTask.onPreExecute(LoginActivity.java:239)
  08-21 15:44:54.839: E/WindowManager(1893):  at android.os.AsyncTask.execute(AsyncTask.java:391)
  08-21 15:44:54.839: E/WindowManager(1893):  at com.assignmentexpert.LoginActivity$5.onClick(LoginActivity.java:195)
  08-21 15:44:54.839: E/WindowManager(1893):  at android.view.View.performClick(View.java:2485)
  08-21 15:44:54.839: E/WindowManager(1893):  at android.view.View$PerformClick.run(View.java:9080)
  08-21 15:44:54.839: E/WindowManager(1893):  at android.os.Handler.handleCallback(Handler.java:587)
  08-21 15:44:54.839: E/WindowManager(1893):  at android.os.Handler.dispatchMessage(Handler.java:92)
  08-21 15:44:54.839: E/WindowManager(1893):  at android.os.Looper.loop(Looper.java:123)
  08-21 15:44:54.839: E/WindowManager(1893):  at android.app.ActivityThread.main(ActivityThread.java:3687)
  08-21 15:44:54.839: E/WindowManager(1893):  at java.lang.reflect.Method.invokeNative(Native Method)
  08-21 15:44:54.839: E/WindowManager(1893):  at java.lang.reflect.Method.invoke(Method.java:507)
  08-21 15:44:54.839: E/WindowManager(1893):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
  08-21 15:44:54.839: E/WindowManager(1893):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
  08-21 15:44:54.839: E/WindowManager(1893):  at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>But when I start application one more time - everything works fine again. On the next time - I get exceptions again. </p>
  
  <p>My open() method in helper class:</p>
  
  <pre><code>  public void open() throws SQLException {
        helper.getReadableDatabase();
        helper.getWritableDatabase();
  
    }
  </code></pre>