You are here: Replication With VOD > Create A Database

Setup JDO for VOD

This guide shows how to setup JDO for VOD.

This topic assumes that you've already installed the VOD database. Visit the official website to download VOD and follow the installation guide.

Create A Database

The first step is to create the database. If you already have a database you can skip this step. The official documentation on managing the VOD database can be found in the VOD installation-folder.

You can create the database with the 'Administration Console' which is shipped with VOD. Start the 'Administration Console', choose 'File'->'Create Database...' and follow the wizard to create a new database.

As alternative you can create a new database in the command line with these commands:

makedb databaseName@localhost
createdb databaseName@localhost startdb databaseName@localhost

JDO Setup

The replication system uses the JDO API to replicate objects between db4o and VOD. Therefore you need to setup the VOD JDO implementation. Again the official documentation is part of the VOD installation. This is just a very short guide to setting JDO up and doesn't cover all details.

First you need to add the JDO-libraries to you project. The JDO-libraries are in the VOD-directory under the 'lib'-subdirectory. You need to add at least these jars.

Second you need to provide the JDO configuration. You can keep this information in a property file. For example in a versant.properties-file which is stored along with your code. There are three important configuration settings you need to set, the factory-class, the connection-URL and information which classes are enhanced.

For more properties and settings read the JDO and VOD documentation. The example property-file looks like this:

## The implementation of the JDO standard
javax.jdo.PersistenceManagerFactoryClass=com.versant.core.jdo.BootstrapPMF
# The connection-string
javax.jdo.option.ConnectionURL=versant:dRSVodExample
# Files which contain a list of meta information for the persistent
# classes. You can list more with versant.metadata.1= versant.metadata.2= ... versant.metadata.n
versant.metadata.0=com/db4odoc/drs/vod/package.jdo
versant.properties: JDO configuration for VOD

Additionally you need to have a package description where you declare all persistent classes:

<jdo>
    <package  name="com.db4odoc.drs.vod">
        <class  name="com.db4odoc.drs.vod.Car"/>
        <class  name="com.db4odoc.drs.vod.Pilot"/>
    </package>
</jdo>
package.jdo: List all persistent classes of this package

The last preparation step is to get the enhancer in place. JDO requires that classes implements the PersistenceCapable interface. Implementing this by hand would be extremely painful. That's why you can use an enhancer which does this for you. Create an Ant build script which does that.

<project  name="tutorial"  default="build-and-enhance"  basedir=".">

    <property  file="machine.properties"/>
    <!--The location of the build-result (the compiled class-files).
     Depends on you're build-system / IDE.
     For plain Eclipse projects it's usually "./bin" -->
    <property  name="dir.build.output"  value="./bin"/>
    <property  name="dir.build.source"  value="./src"/>
    <property  name="dir.lib"  value="./lib"/>

    <property  name="dir.jdo.lib"  value="${dir.vod.install.location}\lib"/>
    <property  name="dir.jdo.sdklib"  value="${dir.vod.install.location}\sdk\lib"/>

    <path  id="enhancerClassPath">
        <pathelement  path="${dir.build.output}"/>
        <fileset  dir="${dir.jdo.lib}"  includes="**/*.jar"/>
        <fileset  dir="${dir.jdo.sdklib}"  includes="**/*.jar"/>
    </path>

    <target  name="build-and-enhance"  depends="compile, enhance"/>

    <target  name="enhance"  description="Enhance the JOD classes">
        <taskdef  resource="versant.tasks">
            <classpath>
                <path  refid="enhancerClassPath"/>
            </classpath>
        </taskdef>

        <jdo-enhance  outputdir="${dir.build.output}"
                     classpathref="enhancerClassPath"  >
            <persistentaware  dir="${dir.build.output}"/>
        </jdo-enhance>
    </target>

    <target  name="compile">
        <mkdir  dir="${dir.build.output}"  />
        <javac  srcdir="${dir.build.source}"
            destdir="${dir.build.output}"
            target="6"
			source="6"
			debug="on"
			encoding="UTF-8">
                <classpath>
                    <pathelement  path="${dir.jdo.lib}/jdo2-api-2.1.jar"/>
                    <pathelement  path="${dir.jdo.lib}/vodjdo.jar"/>
                    <pathelement  path="${dir.jdo.lib}/asm-all-3.1.jar"/>
                    <pathelement  path="${dir.lib}/*"/>
                </classpath>
			</javac>
        <copy  todir="${dir.build.output}">
            <fileset  dir="${dir.build.source}"  casesensitive="yes">
              <include  name="**/*.*"/>
              <exclude  name="**/*.java"/>
            </fileset>
        </copy>
    </target>
</project>
build.xml: Enhancer script

Along to the Ant-script add a machine.properties-file, which contains the installation specific information. Like the installation-location of VOD.

# The installation folder of VOD
dir.vod.install.location=C:/progs/Versant/8
machine.properties: The properties for our project

Using JDO

After running the enhancer you can start using JDO. First you need to create a PersistenceManagerFactory-instance. This can be created with the static helper methods from the JDOHelper-class:

InputStream propertySource = Thread.currentThread()
        .getContextClassLoader().getResourceAsStream(VERSANT_PROPERTY_FILE);
if (null == propertySource) {
    throw  new RuntimeException("Couldn't find resource '" + VERSANT_PROPERTY_FILE + "' in the classpath");
}
try {
    return JDOHelper.getPersistenceManagerFactory(propertySource);
} finally {
    try {
        propertySource.close();
    } catch (IOException ignored) {
    }
}
JDOUtilities.java: Opening the persistence factory

After that you can create persistence manager with the factory and use it to store, query and update objects with JDO:

PersistenceManagerFactory factory = JDOUtilities.createPersistenceFactory();

PersistenceManager persistence = factory.getPersistenceManager();
persistence.currentTransaction().begin();

Pilot john = new Pilot("John",42);
Car car = new Car(john,"Fiat Punto");

persistence.makePersistent(car);

persistence.currentTransaction().commit();
persistence.close();
StoreObjectsWithJDO.java: Use the persistence manager to store objects

For more information and help on JDO read the official VOD documentation and take a look the JOD API.