RDBMS : Data Sources

DataNucleus requires a data source that represents the datastore in use. This is often just a URL defining the location of the datastore, but (in the case of RDBMS) there are in fact several ways of specifying this data source depending on the environment in which you are running.

Stand-Alone Environment : Connection Pooling

When running a stand-alone application (not within a J2EE environment), you would typically specify properties for your PersistenceManagerFactory as in the following example

datanucleus.ConnectionDriverName=com.mysql.jdbc.Driver
datanucleus.ConnectionURL=jdbc:mysql://localhost/myDB
datanucleus.ConnectionUserName=...
datanucleus.ConnectionPassword=...

So in this case we have a MySQL datastore, with the database called "myDB". This is perhaps the most common specification method, but does not use connection pool . You can specify connection pooling with this method - see the Connection Pooling Guide.

See also :



Java Client Environment : Nonmanaged Context

DataNucleus permits you to take advantage of using database connection pooling that is available on an application server. The application server could be a full J2EE server (e.g WebLogic) or could equally be a servlet engine (e.g Tomcat, Jetty). Here we are in a non-managed context, and we use the following properties when creating our PersistenceManagerFactory, and refer to the JNDI data source of the server.

If the data source is avaiable in WebLogic, the simplest way of using a data source outside the application server is as follows.

Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL,"t3://localhost:7001");
    
Context ctx = new InitialContext(ht);
DataSource ds = (DataSource) ctx.lookup("jdbc/datanucleus");
    
Map properties = new HashMap();
properties.setProperty("datanucleus.ConnectionFactory",ds);

PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(properties);

If the data source is avaiable in Websphere, the simplest way of using a data source outside the application server is as follows.

Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
ht.put(Context.PROVIDER_URL,"iiop://server:orb port");
    
Context ctx = new InitialContext(ht);
DataSource ds = (DataSource) ctx.lookup("jdbc/datanucleus");
    
Map properties = new HashMap();
properties.setProperty("javax.jdo.PersistenceManagerFactoryClass",
                    "org.datanucleus.api.jdo.JDOPersistenceManagerFactory");
properties.setProperty("datanucleus.ConnectionFactory",ds);

PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(properties);
Servlet Environment : Managed Context

As an example of setting up such a JNDI data source for Tomcat 5.0, here we would add the following file to $TOMCAT/conf/Catalina/localhost/ as "datanucleus.xml"

<?xml version='1.0' encoding='utf-8'?>
<Context docBase="/home/datanucleus/" path="/datanucleus">
    <Resource name="jdbc/datanucleus" type="javax.sql.DataSource"/>
    <ResourceParams name="jdbc/datanucleus">
        <parameter>
            <name>maxWait</name>
            <value>5000</value>
        </parameter>
        <parameter>
            <name>maxActive</name>
            <value>20</value>
        </parameter>
        <parameter>
            <name>maxIdle</name>
            <value>2</value>
        </parameter>

        <parameter>
            <name>url</name>
            <value>jdbc:mysql://127.0.0.1:3306/datanucleus?autoReconnect=true</value>
        </parameter>
        <parameter>
            <name>driverClassName</name>
            <value>com.mysql.jdbc.Driver</value>
        </parameter>
        <parameter>
            <name>username</name>
            <value>mysql</value>
        </parameter>
        <parameter>
            <name>password</name>
            <value></value>
        </parameter>
    </ResourceParams>
</Context>

With this Tomcat JNDI data source we would then specify the PMF ConnectionFactoryName as java:comp/env/jdbc/datanucleus .

   
Properties properties = new Properties();
properties.setProperty("datanucleus.ConnectionFactoryName","java:comp/env/jdbc/datanucleus");

PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(properties);

See also :

J2EE Environment : Managed Context

As in the above example, we can also run in a managed context, in a J2EE/Servlet environment, and here we would make a minor change to the specification of the JNDI data source depending on the application server or the scope of the jndi: global or component.

Using JNDI deployed in global environment:

   
Properties properties = new Properties();
properties.setProperty("datanucleus.ConnectionFactoryName","jdbc/datanucleus");

PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(properties);

Using JNDI deployed in component environment:

   
Properties properties = new Properties();
properties.setProperty("datanucleus.ConnectionFactoryName","java:comp/env/jdbc/datanucleus");

PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(properties);

See also :