Example #1
0
 /**
  * Stores the config object in the database. Please call this after any modifications to the
  * config, it is not done automatically because the user interface will usually change many values
  * at once.
  */
 public synchronized void storeAndCommit() {
   synchronized (mDB.lock()) {
     try {
       mDB.store(mStringParams, 3);
       mDB.store(mIntParams, 3);
       mDB.store(this);
       mDB.commit();
     } catch (RuntimeException e) {
       System.gc();
       mDB.rollback();
       Logger.error(this, "ROLLED BACK!", e);
       throw e;
     }
   }
 }
Example #2
0
  /**
   * Loads an existing Config object from the database and adds any missing default values to it,
   * creates and stores a new one if none exists.
   *
   * @return The config object.
   */
  public static Config loadOrCreate(WoT myWoT) {
    ExtObjectContainer db = myWoT.getDB();
    synchronized (db.lock()) {
      Config config;
      ObjectSet<Config> result = db.queryByExample(Config.class);

      if (result.size() == 0) {
        Logger.debug(myWoT, "Creating new Config...");
        config = new Config(myWoT);
        config.storeAndCommit();
      } else {
        if (result.size() > 1) /* Do not throw, we do not want to prevent WoT from starting up. */
          Logger.error(myWoT, "Multiple config objects stored!");

        Logger.debug(myWoT, "Loaded config.");
        config = result.next();
        config.initializeTransient(myWoT);
        config.setDefaultValues(false);
      }

      return config;
    }
  }