Creating a simple application for a Placement-Managed Federated Database

Printer-friendly version

Connect to the database

Connection connection = Connection.open(bootfile, oo.openReadWrite);
// where bootfile is the String value of the path to the bootfile
// and the second argument is the mode in which to connect
For more information on connections visit our FAQ.

Use a session

Session session = new Session();
session.begin();           // starts the transaction
...............   		   // performs database operations
session.commit();        // commits the transaction
For more information on sessions visit our FAQ.

Get a federated database

ooFDObj fd = session.getFD(); 
// gets the federated database of the session

Initialize objects and write them to the fdb

Person person = new Person("Person " + 1);  // initializes a Person object
person.persist(); // writes the object to the federated database

Change the type of transaction

session.setOpenMode(oo.openReadOnly); // changes the transaction from openReadWrite to openReadOnly

Scan objects and output them

// scans for all Person objects that end with a number
Iterator itr = fd.scan(Person.class.getName(), "name =~ 'Person [0-9]*'");
Person p1;
while(itr.hasNext()) {  // iterates through all the Person objects
  p1 = (Person) itr.next();
  System.out.println(p1.getName()); // and outputs their names
}

Sample application

This program initializes Person objects, puts them into the federated database, and searches the federated database for these Person objects and prints out all the Person objects' names that meet the search criteria.
import com.objy.db.app.*;
 
public class SimpleApp {
 
  public static void main(String[] args) {
    String bootfile = ".../Desktop/test.boot";  // the boot file
         
    Connection connection = null;
    Session session = null;
         
    try{
      // starts the connection and opens it for read/write mode
      connection = Connection.open(bootfile, oo.openReadWrite);
      session = new Session();
      session.begin(); // begins a transaction
             
      ooFDObj fd = session.getFD(); // gets the fdb of the session
             
      Person person = null;    
      for(int i = 0; i < 100; i++) {
        person = new Person("Person " + i);  // initializes Person objects
        person.persist(); // and writes them to the fdb
      }
             
      session.commit(); // commits the changes and ends a transaction
             
      // starts a read only transaction
      session.setOpenMode(oo.openReadOnly);     
      session.begin(); // begins a new transaction
             
      // scans for all Person objects that end with a number
      Iterator itr = fd.scan(Person.class.getName(), "name =~ 'Person [0-9]*'");
      Person p1;
      while(itr.hasNext()) {  // iterates through all the Person objects
        p1 = (Person) itr.next();
        System.out.println(p1.getName()); // and outputs their names
      }
             
      session.commit();  // commits the changes and ends a transaction
      
	}
    catch(Exception e) {
      e.printStackTrace();
      if(session != null)
        session.abort();
    }
 
  }
}
 
class Person extends ooObj {

  private String name;
     
  public Person(String name) {
    setName(name);
  }
     
  public String getName() {
    fetch();
    return name;
  }
     
  public void setName(String name) {
    markModified();
    this.name = name;
  }   
}
Date: 
Monday, September 15, 2014
Product: 
Objectivity/DB
Version: 
11.0