9.3. J2EE Deployment

Kodo JDO can be deployed into your J2EE applications in a number of ways. The simplest is just to include the Kodo libraries in a J2EE EAR file and call Kodo JDO directly from there using the standard JDOHelper.getPersistenceManagerFactory described in the previous section. This will be problematic, however, if you want Kodo JDO to share resources with other beans or if you have multiple beans deployed that need to use the same Kodo JDO resources.

The second way to integrate Kodo JDO is to configure and bind an instance of the PersistenceManagerFactory into JNDI. Although the mechanism by which you do this is up to you, the most common way is to create a startup class according to your application server's documentation that will create a factory and then bind it in JNDI. For example, in WebLogic you could write a startup class as follows:

Example 9.4.  Binding a PersistenceManagerFactory into JNDI via a WebLogic Startup Class

import java.util.*;
import javax.naming.*;

import weblogic.common.T3ServicesDef;
import weblogic.common.T3StartupDef;

/**
 * This startup class creates and binds an instance of a
 * Kodo JDBCPersistenceManagerFactory into JNDI.
 */
public class StartKodo
    implements T3StartupDef
{   
    private static final String PMF_JNDI_NAME  = "my.jndi.name";
    private static final String PMF_PROPERTY   = 
        "javax.jdo.PersistenceManagerFactoryClass";
    private static final String PMF_CLASS_NAME = 
        "kodo.jdbc.runtime.JDBCPersistenceManagerFactory";

    private T3ServicesDef services;

    public void setServices (T3ServicesDef services)
    {
        this.services = services;
    }

    public String startup (String name, Hashtable args) 
        throws Exception 
    {
        String jndi = (String) args.get ("jndiname");
        if (jndi == null || jndi.length () == 0)
            jndi = PMF_JNDI_NAME;

        Properties props = new Properties ();
        props.setProperty (PMF_PROPERTY, PMF_CLASS_NAME);

        // you could set additional properties here; otherwise, the defaults
        // from kodo.properties will be used (if the file exists)

        PersistenceManagerFactory factory = JDOHelper.
            getPersistenceManagerFactory (props);

        InitialContext ic = new InitialContext ();
        ic.bind (jndi, factory);

        // return a message for logging
        return "Bound PersistenceManagerFactory to " + jndi;
    }
}

Applications that utilize Kodo JDO can then obtain a handle to the PersistenceManagerFactory as follows:

Example 9.5. Looking up the PersistenceManagerFactory in JNDI

PersistenceManagerFactory factory = (PersistenceManagerFactory)
    new InitialContext ().lookup ("java:/MyKodoJNDIName");
PersistenceManager pm = factory.getPersistenceManager ();

The third way to deploy Kodo JDO in an application server is to utilize the Java Connector Architecture features of Kodo JDO as described in the next section.