Presents your JAVA E-NEWSLETTER for July 24, 2003 <-------------------------------------------> TRY A NEW APPROACH TO OBJECT PERSISTENCE Object persistence is a problem that most Java programmers solve repeatedly throughout their careers. Programmers typically choose a database for this task and then write code to store objects or object state in a database. Prevalence is a revolutionary new way of persisting objects that's making inroads in the Java community. The most significant departure from the typical approach to object persistence is the dismissal of the database. There is no database layer; instead, there is a prevalance layer. In a prevalent system, all objects of interest are held in memory, and a snapshot of the prevalent system is periodically written to permanent storage (this is usually just a file or set of files). The first implementation of object prevalance for Java is a package called prevayler, which is open source, small, fast, and easy to integrate. From a programmer's perspective, managing objects in a prevalent system isn't much different than other persistence mechanisms. If you prefer, you can wrap access to the prevalent system in a Data Access Object (DAO) and work as you normally would. For instance, if you have a system that manages insurance policies, you might have a PolicyDAO type. Here's an example: ... public PrevalentPolicyDAO extends AbstractPrevalentSystem implements PolicyDAO { private Map policies = new HashMap(); public add(Policy p) { this.policies.put(getPolicyId(), p); } public Policy find(Long id) { return (Policy) this.policies.get(id); } ... } To add a new Policy object, you call the add() method; to retrieve a Policy object, you call the find() method with the ID of the Policy you want to get back. Somewhere else in the system, you'd have a few lines of code that would record a snapshot of your prevalent system object, PrevalentPolicyDAO, to disk at periodic intervals or whenever you decide that it should. The code to create your SnapshotPrevayler would look like this: SnapshotPrevayler prevayler = new SnapshotPrevayler(new PrevalentPolicyDAO(), "policies.dat"); The code to take a snapshot of the system would look something like this: prevayler.takeSnapshot(); If you're thinking there must be more to it, you're right. Although there are questions to ask and new problems to solve with a prevalent system, prevalance still definitely deserves consideration. It's simple and orders of magnitudes faster than database access. While it won't be right for every situation, it's fantastic for certain ones. There are already prevalent systems for Java, C#, Python, Ruby, and more. For more information about prevalent systems, visit Prevayler.org. http://www.prevayler.org/ David Petersheim is a Senior Java Developer with Genscape, Inc. He designs and develops server-side applications to acquire and process real-time energy data. ----------------------------------------