Package com.db4o

Examples of com.db4o.ObjectContainer


  }

  private void setupDatabase(DatabaseKey databaseKey) throws MasterKeysWrongPasswordException, MasterKeysFileSizeException, IOException {
    /* FIXME: Backup the database! */

    ObjectContainer database;

    File dbFileBackup = new File(dbFile.getPath()+".tmp");
    File dbFileCryptBackup = new File(dbFileCrypt.getPath()+".tmp");

    if(dbFileBackup.exists() && !dbFile.exists()) {
      if(!dbFileBackup.renameTo(dbFile)) {
        throw new IOException("Database backup file "+dbFileBackup+" exists but cannot be renamed to "+dbFile+". Not loading database, please fix permissions problems!");
      }
    }
    if(dbFileCryptBackup.exists() && !dbFileCrypt.exists()) {
      if(!dbFileCryptBackup.renameTo(dbFileCrypt)) {
        throw new IOException("Database backup file "+dbFileCryptBackup+" exists but cannot be renamed to "+dbFileCrypt+". Not loading database, please fix permissions problems!");
      }
    }

    try {
      if(securityLevels.getPhysicalThreatLevel() == PHYSICAL_THREAT_LEVEL.MAXIMUM) {
          if(dbFile.exists())
              FileUtil.secureDelete(dbFile);
          if(dbFileCrypt.exists())
              FileUtil.secureDelete(dbFileCrypt);
                if(dbFileBackup.exists())
                    FileUtil.secureDelete(dbFile);
                if(dbFileCryptBackup.exists())
                    FileUtil.secureDelete(dbFileCrypt);
          return; // No need to migrate
      } else if(dbFile.exists()) {
        // Just open it.
        database = Db4o.openFile(getNewDatabaseConfiguration(null), dbFile.toString());
      } else if((dbFileCrypt.exists())) {
        // Open encrypted, regardless of seclevel.
        database = openCryptDatabase(databaseKey);
      } else return;
    } catch (Db4oException e) {
      database = null;
      System.err.println("Failed to open database: "+e);
      e.printStackTrace();
    }
    // DUMP DATABASE CONTENTS
    if(logDEBUG && database != null) {
    try {
    System.err.println("DUMPING DATABASE CONTENTS:");
    ObjectSet<Object> contents = database.queryByExample(new Object());
    Map<String,Integer> map = new HashMap<String, Integer>();
    for(Object o: contents) {
      String name = o.getClass().getName();
      if((map.get(name)) != null) {
        map.put(name, map.get(name)+1);
      } else {
        map.put(name, 1);
      }
      // Activated to depth 1
      try {
        Logger.minor(this, "DATABASE: "+o.getClass()+":"+o+":"+database.ext().getID(o));
      } catch (Throwable t) {
        Logger.minor(this, "CAUGHT "+t+" FOR CLASS "+o.getClass());
      }
      database.deactivate(o, 1);
    }
    int total = 0;
    for(Map.Entry<String,Integer> entry : map.entrySet()) {
      System.err.println(entry.getKey()+" : "+entry.getValue());
      total += entry.getValue();
    }

    // Some structures e.g. collections are sensitive to the activation depth.
    // If they are activated to depth 1, they are broken, and activating them to
    // depth 2 does NOT un-break them! Hence we need to deactivate (above) and
    // GC here...
    System.gc();
    System.runFinalization();
    System.gc();
    System.runFinalization();
    System.err.println("END DATABASE DUMP: "+total+" objects");
    } catch (Db4oException e) {
      System.err.println("Unable to dump database contents. Treating as corrupt database.");
      e.printStackTrace();
      try {
        database.rollback();
      } catch (Throwable t) {} // ignore, closing
      try {
        database.close();
      } catch (Throwable t) {} // ignore, closing
      database = null;
    } catch (IllegalArgumentException e) {
      // Urrrrgh!
      System.err.println("Unable to dump database contents. Treating as corrupt database.");
      e.printStackTrace();
      try {
        database.rollback();
      } catch (Throwable t) {} // ignore, closing
      try {
        database.close();
      } catch (Throwable t) {} // ignore, closing
      database = null;
    }
    }

View Full Code Here


   *            Command line arguments. This appliation does not check any of
   *            these arguments.
   */
  public static void main(String[] args) {
    AbstractApplicationContext ctx = ExampleUtils.getContext();
    ObjectContainer db = (ObjectContainer) ctx.getBean(ExampleUtils.CONTAINER_BEAN_ID);
    try {
      storeFirstPilot(db);
      storeSecondPilot(db);
      retrieveAllPilots(db);
      retrievePilotByName(db);
View Full Code Here

   *            Command line arguments. This appliation does not check any of
   *            these arguments.
   */
  public static void main(String[] args) {
    AbstractApplicationContext ctx = ExampleUtils.getContext();
    ObjectContainer db = (ObjectContainer) ctx.getBean(ExampleUtils.CONTAINER_BEAN_ID);
    try {
      storeFirstPilot(db);
      storeSecondPilot(db);
      retrieveAllPilots(db);
      retrievePilotByName(db);
View Full Code Here

    if(oc == null) throw new Error("Unable to obtain db4o's object container");
    servletContext.setAttribute(KEY, oc);

    // re-stub the db
    log.info("re-stubbing test db..");
    final ObjectContainer dbref = injector.getInstance(ObjectContainer.class);
    final IDbShell dbs = injector.getInstance(IDbShell.class);
    dbs.restub(dbref);
  }
View Full Code Here

  @Override
  public void shutdown(ServletContext servletContext) {
    super.shutdown(servletContext);
    // kill the db
    final ObjectContainer oc = (ObjectContainer) servletContext.getAttribute(KEY);
    if(oc != null) {
      log.debug("Shutting down db4o..");
      oc.close();
    }

  }
View Full Code Here

      throws TransactionException {
    if (transactionDefinition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
      throw new InvalidIsolationLevelException("Db4o does not support an isolation level concept");
    }

    ObjectContainer container = null;

    try {
      Db4oTransactionObject txObject = (Db4oTransactionObject) transaction;
      if (txObject.getObjectContainerHolder() == null) {
        // use the given container
View Full Code Here

    }
  }
 

  private static void query() {
    ObjectContainer db = Db4o.openFile(DBFILENAME);
    try {
      ObjectSet set = db.queryByExample(TestDummy.class);
      if (ITERATIONS != set.size()) {
        System.err.println("Expected: " + ITERATIONS + ", actual: " + set.size());
      }
    } finally {
      db.close();
    }
  }
View Full Code Here

    }
  }

  private static void store() {
   
    ObjectContainer db = Db4o.openFile(DBFILENAME);
    try {
      for (int i=0; i<ITERATIONS; ++i) {
        db.store(new TestDummy("Dummy " + i));
        if (0 == i % 10) {
          db.commit();
        }
      }
    } finally {
      db.close();
    }
  }
View Full Code Here

  /**
   * @see org.springextensions.db4o.Db4oOperations#execute(org.springextensions.db4o.Db4oCallback,
   * boolean)
   */
  public Object execute(Db4oCallback callback, boolean exposeNativeContainer) throws DataAccessException {
    ObjectContainer cont = getObjectContainer();
    try {
      ObjectContainer container = (exposeNativeContainer ? cont : createContainerProxy(cont));
      Object result = callback.doInDb4o(container);
      // check out caching/query support
      return result;
    }
    catch (Db4oException ex) {
View Full Code Here

    servletContext.setAttribute(KEY, oc);
  }

  @Override
  public void shutdown(ServletContext servletContext) {
    final ObjectContainer oc = (ObjectContainer) servletContext.getAttribute(KEY);
    if(oc != null) {
      log.debug("Shutting down db4o..");
      oc.close();
    }
  }
View Full Code Here

TOP

Related Classes of com.db4o.ObjectContainer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.