Thursday, July 13, 2006

 

Making a datasource available to the application.

Whilst JDO is an API that allows for non-relational databases to be the backend, such as object-oriented ones, JPOX is geared solely towards using a relational datasource as the actual back-end implementation (at the moment anyway, it is their intention to support OO ones at some point). The "standard" way to make a relational (i.e. JDBC) datasource available to an application in Java EE, is to add it to the servlet container / application server and make it available through a JNDI lookup. One of the key benefits of doing this is that it gives you database pooling without any further ado. The JDO API supports the notion of adding, then looking up a PersistenceManagerFactory in JNDI, but JPOX doesn't include the supporting classes to do this! Anyway, for this project for greater harmonization with Java EE projects in general we've chosen to make the underlying datasource available as a widely understood JDBC javax.sql.Datasource. Alternatively it is possible just to include the JDBC driver jar in the war itself and obtain a JDBC connection directly.

Whatever underlying datasource we're using, we initialize our PersistenceManagerFactory via a jpox.properties file. The key differences are that for a datasource configured without JNDI, specific properties look like this:

No JNDI

...
javax.jdo.option.ConnectionDriverName=org.apache.derby.jdbc.ClientDriver
javax.jdo.option.ConnectionURL=jdbc:derby://localhost/db/trecx-store
...

JNDI

...
javax.jdo.option.ConnectionDriverName=org.jpox.driver.JPOXDriver
javax.jdo.option.ConnectionURL=jpox:java:comp/env/jdbc/TrecxStore
...

So, where as in the no-JNDI configuration we specify the JDBC user, password, driver and connection URL in jpox.properties, with the JNDI configuration we specify it in the file
/META-INF/context.xml which ends up in the war file. This file also contains instructions including where it should go in the default JNDI context (this is how you add a datasource to JNDI with Tomcat; other servlet containers do something similar). Although some what opaque(!) the specification of a driver of the type org.jpox.driver.JPOXDriver and a connection URL of jpox:java:comp/env/jdbc/TrecxStore informs the JDO helper class that there is a javax.sql.Datasource instance available in the default JNDI context at jdbc/TrecxStore.

Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?