Guide  Reference  Contents  Previous  Next  Index



Programming Examples

This chapter contains programming examples used as illustrations in earlier chapters of this guide. The examples are organized according to the chapters in which they are discussed. The files for the examples are in the chapter name subdirectory of the programming samples directory. See Installation and Platform Notes for the location of the samples directory for your platform.

Some examples can be executed after creating a federated database and compiling the files. See the directions for particular examples for details.

In This Chapter

Getting Started
Application Objects
ODMG Application Objects
Storage Objects
Defining Persistence-Capable Classes
RentalFields Package
RentalMap Package
RentalRelations Package
PersistentInterface Package
Naming and Retrieving Persistent Objects
Sales Package
Traversal Package
Clustering Objects
Container-Pool Strategy
Cluster-By-Class Strategy

Getting Started

The example from Chapter 1, "Getting Started" is a complete application that uses all the elements introduced in "Objectivity for Java API". The example application is intended to be used by the agents and managers of a vehicle rental company. The operations supported are:

The application is implemented by four classes:

To execute this example, you need to:

  1. Compile the files Vehicle.java, Fleet.java, VrcInit.java, and Vrc.java located in the GettingStarted subdirectory of the programming samples directory. See the Installation and Platform Notes for your operating system for the location of the samples directory for your platform.

  2. Start an Objectivity/DB lock server.

  3. Create a federated database called Vrc in the GettingStarted sample directory.

  4. Execute VrcInit to initialize the federated database.

See the Objectivity/DB administration book for information on the tools used to create a federated database and start a lockserver.

Application Objects

The example discussed in Chapter 2, "Application Objects" illustrates how to use the application classes Connection and Session, in particular how to develop a multi-threaded application.

The MultipleThreadsSP programming example simulates a server application handling requests to look up or list root objects in a database. The server creates a new thread to handle each request. Instead of having each thread create a new session, the example shares a pool of sessions among the threads. Access to the pool of sessions is synchronized. The session pool is implemented in the SessionPool class . The example also illustrates the use of a restricted thread policy: before a thread uses a session that it retrieves from the pool, it executes a join operation.

This example can be executed after you compile the files and create a federated database named "Objects" in the Application subdirectory of the samples directory.

ODMG Application Objects

The example discussed in Chapter 3, "ODMG Application Objects" illustrates how to use the ODMG Application Classes: Database and Transaction, in particular how to develop a multi-threaded application.

The MultipleThreadsTP programming example simulates a server application handling requests to look up root objects in a database. The server creates a new thread to handle each request and each thread gets a transaction from a shared pool of transactions. The transaction pool is implemented in the TransactionPool class.

This example can be executed after you compile the files and create a federated database named "Objects" in the ODMGApplication subdirectory of the samples directory.

Storage Objects

The example discussed in Chapter 5, "Storage Objects" illustrates how to use multiple containers to improve scalability and concurrency. The example revisits the vehicle rental company example introduced in Chapter 1, "Getting Started". The implementation is changed as follows:

The vehicles are stored according to the container pool clustering strategy . This strategy is installed in the sessions used by the database initialization class VrcInit and the interactive application class Vrc .
A vehicle is deleted simply by removing its entry in the fleet map. Because the vehicles are stored in garbage collectible containers, when a vehicle is removed from its fleet, it will be garbage collected when oogc is executed.
All the containers are referenced from a named root (the container pool). As a consequence, they will not be garbage collected even if all their objects are deleted or garbage collected.

The files for this example are in the Storage subdirectory of the programming samples directory.

To execute this example, you need to:

  1. Compile the files Fleet.java, VrcInit.java, Vrc.java, and ContainerPool*.java in the Storage subdirectory of the programming samples directory.

  2. Start an Objectivity/DB lock server.

  3. Create a federated database called Vrc in the Storage directory.

  4. Execute VrcInit to initialize the federated database.

Defining Persistence-Capable Classes

The example in Chapter 1, "Getting Started," described how to define a persistence capable class using inheritance and developed a simple application that accessed objects of that class. The examples discussed in Chapter 6, "Defining Persistence-Capable Classes," illustrate:

RentalFields Package

The RentalFields package contains three classes:

RentalMap Package

The RentalMap package contains two classes:

RentalRelations Package

The RentalRelations package contains two classes:

PersistentInterface Package

The PersistentInterface package contains two classes:

Naming and Retrieving Persistent Objects

The examples discussed in Chapter 10, "Naming Persistent Objects," and Chapter 11, "Retrieving Persistent Objects," illustrate how to name and retrieve a persistent object. The examples in those chapters come from two packages:

Sales Package

The Sales package contains four classes:

Traversal Package

The Traversal package contains three classes, but the only one with methods is Tester. Tester can be executed after you compile all the classes and create a federated database named "Traverse" in the NamingAndRetrieving subdirectory of the samples directory.

Clustering Objects

The examples discussed in Chapter 12, "Clustering Objects" illustrate two application-specific clustering strategies. Both clustering strategies use container pools associated with particular databases.

Container-Pool Strategy

A container pool is an object of the ContainerPool class. Each database has two container pools, one containing garbage-collectible containers and the other containing non-garbage-collectible containers. A database identifies its container pools with the scope names "GCContainerPool" and "Non-GCContainerPool", respectively. When a container pool is created, parameters to its constructor specify its database, the number of containers in the pool, and whether the containers are garbage collectible.

The ContainerPoolStrategy class implements a clustering strategy that performs as follows:

Cluster-By-Class Strategy

ClusterByClassStrategy extends ContainerPoolStrategy to accept an application-specific clustering reason of the JustCreatedReason class. Such a clustering reason indicates that an object is being made persistent because it is a newly created object of a class whose objects are always persistent. The strategy decides where to cluster such an object based on its class:

Objects of these two classes are made persistent as soon as they are created.



Guide  Reference  Contents  Previous  Next  Index



Copyright © 2000, Objectivity, Inc. All rights reserved.