ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Prevalence: Transparent, Fault-Tolerant Object Persistence
Pages: 1, 2, 3, 4, 5

Testing the Completed System

Add the following code to Store so that it performs transactions of all types:

// restock an item
prevayler.execute(new ItemRestockTransaction(1, 25));
System.out.println("Item restocked");

// add a new Order
prevayler.execute(new OrderCreateTransaction());
System.out.println("Order added");

// add 2 BookItems  and 5 MultipackItems to 
// the Order - should have ids 1 and 3
prevayler.execute(new OrderAddItemTransaction(1, 1, 2));
prevayler.execute(new OrderAddItemTransaction(1, 3, 5));
System.out.println("Items added to Order");

Delete existing snapshots and transaction logs, and run Main and Store as before to create the persistent objects. View the snapshot again, which should look similar to Figure 13.

Snapshot for full system
Figure 13. Snapshot for the full system

Add the following code to Retrieve and run the class.

// get orders
List orders = ((PrevalentOrderSystem) prevayler.prevalentSystem())
for (Iterator io = orders.iterator(); io.hasNext();) {
    Order currentOrder = (Order) io.next();

The output should be similar to the following:

Reading data\0000000000000000001.transactionLog...
1 - class ordersystem.BookItem:123 in stock:
Java Database Best Practices
2 - class ordersystem.SoftwareItem:100 in stock:
SuSE Linux Professional
3 - class ordersystem.MultipackItem:100 in stock
(0 - class ordersystem.SoftwareItem:100 in stock:
Red Hat Enterprise Linux)
1 - class ordersystem.Order:Cost $6862.1

This clearly shows that the BookItem with id = 1 has been restocked and that the Order has been added.

Should I Be Using Prevalence?

Let's be clear about this--I am not in any way suggesting that everyone should abandon their databases and convert all their projects to prevalent systems. In most cases, this would be neither possible nor desirable. However, what I do suggest is that, in certain circumstances, it is worth considering prevalence as a simple alternative to using a database. The main characteristics of a suitable application are:

  • There is a requirement for fault-tolerant data persistence.
  • The data will not be shared with other systems--since the data is the business objects, it is very closely coupled to the system.
  • There is not too much data--just how much is too much will depend on hardware, but the data has to fit in working memory.

The main advantages of using prevalence are:

  • Simplicity of code and configuration; ideal for prototyping.
  • Performance.

The example system described here gives a flavor of how Prevayler and Preclipse can be used to provide persistence for business objects. Persistence is achieved without thinking of the data in terms of anything other than the business objects themselves--no database, no drivers, no schema definition language, no queries, no mappings from classes to tables, and so on. The persistent data is stored in a single folder that can be backed up easily.

It is worth exploring further examples to see how different types of applications can make use of prevalence. The Preclipse site has an example of a GUI application built using the plugin, while the Presto pet store example on the Prevayler site show how a web application can make use of prevalence. In the latter case, the prevalent system class is made an attribute of the application server context so that it can be accessed from JSPs.


Jim Paterson is a Lecturer at Glasgow Caledonian University in the UK , specializing in web development and object-oriented software development.

Return to ONJava.com.