title: How avoid of using same table at same time in database with different functions
body: <p>I have a database in my application and I have a service that is working in the background all the time (when the app is running or not).</p>
  
  <p>in service and some activities of Application exists functions that read and write into database tables. sometimes I have crashed in my app because it is possible that more than one function start to access to the same table!
  And I am using an external database.(I have a database in my asset folder and first of all, I will copy it into SD Card and going to use it).
  till now I did disable service when the app was open but I need service to be run all the time.</p>
  
  <p>UPDATE: Code Added:
  This is my code that how I access to my database:</p>
  
  <pre><code> //Creating folder for database in SD Card!
      new File(G.DIR_APP).mkdirs();
      File file = new File(G.DIR_APP, "database.sqlite");
  
      if (file.exists()) {
        //Do nothing
      } else {
        /*
        *
        * This part will start when db file is not exist on it's place
        * or
        * Deleted
        * or
        * ...
        *
         */
        try {
  
          AssetManager assetManager = getApplicationContext().getAssets();
          InputStream inputStream;
          inputStream = assetManager.open("database.sqlite");
          HelperIO.copyFile(inputStream, G.DIR_APP + "database.sqlite");
  
        } catch (IOException e) {
          e.printStackTrace();
        }
  
      }
  </code></pre>
  
  <p>Here is an method that i use to access my database:</p>
  
  <pre><code>private void insertMenuItemsIntoDB(ArrayList&lt;MenuItem&gt; appArray) {
  
      SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(G.DIR_APP + "/database.sqlite", null);
  
      for (int i = 0; i &lt; appArray.size(); i++) {
  
        boolean newFile = true;
  
        ArrayList&lt;MenuItem&gt; bootArray = G.bootAppsFromDB();
  
        ContentValues values = new ContentValues();
  
        values.put(G.MENU_ITEM_ID, appArray.get(i).getMenuItemId());
        values.put(G.MENU_ITEM_NAME, appArray.get(i).getMenuItemName());
        values.put(G.MENU_ITEM_DESC, appArray.get(i).getMenuItemDesc());
        values.put(G.MENU_ITEM_APP, appArray.get(i).getMenuItemApp());
        values.put(G.MENU_ITEM_PARENT, appArray.get(i).getMenuItemParent());
        values.put(G.MENU_ITEM_START_X, appArray.get(i).getMenuItemStartX());
        values.put(G.MENU_ITEM_START_Y, appArray.get(i).getMenuItemStartY());
        values.put(G.MENU_ITEM_SPAN_X, appArray.get(i).getMenuItemSpanX());
        values.put(G.MENU_ITEM_SPAN_Y, appArray.get(i).getMenuItemSpanY());
        values.put(G.MENU_ITEM_HAS_WIDGET, appArray.get(i).getMenuItemHasWidget());
        values.put(G.MENU_ITEM_WIDGET_ACTIVE, appArray.get(i).getMenuItemWidgetActive());
        values.put(G.MENU_ITEM_DELETED, appArray.get(i).getMenuItemDeleted());
        values.put(G.MENU_ITEM_LAST_CHANGE, appArray.get(i).getMenuItemLastChange());
        values.put(G.MENU_ITEM_PACKAGE_NAME, appArray.get(i).getMenuItemPackagename());
  
        for (MenuItem menuItem : bootArray) {
  
          if (menuItem.getMenuItemId().equals(appArray.get(i).getMenuItemId())) {
  
            newFile = false;
  
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateDB = menuItem.getMenuItemLastChange();
            String dateServer = appArray.get(i).getMenuItemLastChange();
  
            try {
              Date dateFromDB = sdf.parse(dateDB);
              Date dateFromServer = sdf.parse(dateServer);
  
              if (dateFromServer.compareTo(dateFromDB) &gt; 0) {
  
                db.update(G.DATABASE_TABLE_MENU_ITEM, values, G.MENU_ITEM_ID + "=" + appArray.get(i).getMenuItemId(), null);
  
                break;
              }
            } catch (ParseException e) {
              e.printStackTrace();
            }
          }
        }
  
        if (newFile == true) { //If newFile = true then this file is new
  
          db.insert(G.DATABASE_TABLE_MENU_ITEM, null, values);
        }
      }
   }
  </code></pre>
  
  <p>I really appreciate your answers.</p>