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. InitialisationAs 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 { 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 ObjectsAny 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: The class would look like: public class Customer extends PersistentObject { public Customer() { public void setCustomer_id(int i) { public int getCustomer_id() { public void setName(String v) { public String getName() { Persistent Object Meta DataSQL 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 RestrictionsNormally 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:
Persistent Objects support these restrictions through the PersistentCriteria class to quickly return or delete a sets of objects. Saving ObjectsTo save an object to the database create and instance of the object and set, as a minimum, its primary keys: Customer c = new Customer(); 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 ObjectsTo delete an object call the delete method, the primary key attributes must be set. Customer c = new Customer(); Deleting with CriteriaFor 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(); Retrieving ObjectsTo retrieve an object set its primary key attributes Customer c = new Customer(); Retrieving with CriteriaSets 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(); while (ps.hasMoreObjects()) { 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. TransactionsIt 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(); t.begin(); c.setCustomer_id(10); t.commit(); © Kimble Consultancy Services Ltd |