2-tier-architecture

COBRA makes little distinction between 2 and N tier applications. The main difference is that classes are included from uk.co.kimble.cobra. The initialisation process is slightly different and the COBRA layer runs in the context of the same Java Virtual Machine.

Initialisation

As was mentioned previously, normally all the bootstrap information is held in a properties file. The initialisation process is a case of initialising a PersistentConnection and initialising the connection broker as shown in the code fragment below:

Properties p = new Properties();

try {
   
FileInputStream f = new FileInputStream(property_file);
   
p.load(f);
} catch (Exception e) {
   
return;
}

PersistentConnection cobra_connection;

try {

cobra_connection = new PersistentConnection(

p.getProperty("driver"),

p.getProperty("url"),

p.getProperty("username"),

p.getProperty("password")

);

} catch (PersistentException e) {

return;

}

try {

ConnectionBroker.init(cobra_connection,

p.getProperty("database_table"));

} catch (Exception e) {

return;

}

Creating Persistent Objects

Any Java class that should be persistent must inherit from the class uk.co.cobra.PersistentObject. Each attribute should have a getter and setter method, these follow the pattern established by the Beans framework, that is, first letter in capitals the rest in lower case.

For example for the table:

Customer:
customer_id integer primary key
name character(40)

The class would look like:

public class Customer extends PersistentObject {
    int customer_id;
    String name;

    public Customer() {
        super();
    }

    public void setCustomer_id(int i) {
        customer_id = i;
    }

    public int getCustomer_id() {
        return customer_id;
    }

    public void setName(String v) {
        name = v;
    }

    public String getName() {
        return name;
    }
}

Persistent Object Meta Data

SQL supports a number of aggregate functions:

SUM(column),

AVG(column),

MAX(column),

MIN(column),

COUNT(column),

The keyword DISTINCT can be used with COUNT, SUM and AVG to eliminate duplicates. It should be possible to access this information through the PersistentObject class.

Adding Retrieval Restrictions

Normally Persistent Objects will be retrieved, saved or deleted based on their primary key information. Primary keys are one or more database columns that uniquely identify a row. The attributes that comprise a primary key are given by the Data Dictionary entry for the class. However for reasons of efficiency we often wish to retrieve or delete a number of rows simultaneously. SQL allows a range of restrictions to be applied to the SELECT, DELETE and UPDATE statements, these include:

  • Logical AND/OR operators for concatenating restrictions.

  • Mathmatical operators: = != > < >= <

  • Fuzzy matching: LIKE

  • The subclause: ORDER BY can be used to sort the retrieved data by specific columns

Persistent Objects support these restrictions through the PersistentCriteria class to quickly return or delete a sets of objects.

Saving Objects

To save an object to the database create and instance of the object and set, as a minimum, its primary keys:

Customer c = new Customer();
c.setCustomer_id(10);
pos.save(c);

COBRA maitains state about the object so can decide whether to persist the object to the RDBMS using an INSERT (a new object) or UPDATE (an exsiting object).

Deleting Objects

To delete an object call the delete method, the primary key attributes must be set.

Customer c = new Customer();
c.setCustomer_id(10);
pos.delete(c);

Deleting with Criteria

For efficiency COBRA permits sets of objects to be deleted using criteria, for example to delete all orders belonging to a customer with customer_id of 10:

Order o = new Order();
PersistentCriteria criteria = new PersistentCriteria();
criteria.addEqualTo("customer_id", new Integer(10));
t.delete(o.getName(), criteria);

Retrieving Objects

To retrieve an object set its primary key attributes

Customer c = new Customer();
c.setCustomer_id(10);
c = pos.retrieve(c);

Retrieving with Criteria

Sets of objects can be retrieved based on certain criteria, for example to retrieve all customers whose names begin with the letter A:

Customer c = new Customer();
PersistentCriteria criteria = new PersistentCriteria();
criteria.addEqualTo("customer_name", "A*");
PersistentSet ps = pos.retrieve(c.getName(), criteria);

while (ps.hasMoreObjects()) {
    c = (Customer) ps.nextObject();
    System.out.println(c.getCustomer_name());
}// while

ps.close();

Remember to close a PersistentSet after use. This releases database resources immediately rather than waiting for garbage collection to occur.  All objects in a set can be retrieved by ommitting the criteria parameter in the above example.

Transactions

It is useful to group operations which alter the contents of the RDBMS into discrete units called Transactions.  This can be achieved using the Trans object.  For example we may wish to delete a customer and all its orders. If either the delete of the customer or order(s) fails the operation should fail as a whole, otherwise referential integrity may be affected in the database. Note another way of achieving this is using a cascading delete implemented as a stored procedure where this is supported by the RDBMS:

Customer c = new Customer();
Trans t = new uk.co.kimble.cobra.Trans();

t.begin();

c.setCustomer_id(10);
t.delete(c);
Order o = new Order();
PersistentCriteria criteria = new PersistentCriteria();
criteria.addEqualTo("customer_id", new Integer(s));
t.delete(o.getName(), criteria);

t.commit();