View source | View content page | Page history | Printable version   

Projects:JNDI Service/Technical Documentation


JNDI Service - Technical Documentation

General Considerations


To get the Connection through a JNDI remote resource in the application server, we need to implement a new Java class in our code that should get the remote resource from the server, also we need to make changes in some configuration files to get this resource.

Changes in the configuration needed

We need to do changes in the files below:

Application server configuration file

A JNDI resource should be define on it. The resource should contain all the data needed to get a connection

<Context docBase="openbravo" path="/openbravo" reloadable="true" source="org.eclipse.jst.j2ee.server:openbravo">

And this is the server configuration in tomcat for Postgresql

<Context docBase="openbravo" path="/openbravo" reloadable="true"    source="org.eclipse.jst.j2ee.server:openbravo">
     	<Resource name="jdbc/openbravo" auth="Container"
       type="javax.sql.DataSource" driverClassName="org.postgresql.Driver"
       username="postgres" password="postgres" maxActive="20" maxIdle="10"
Changes in Web.xml file

We need to define the JNDI resource:

	<description>Oracle Datasource example</description>
Changes in Openbravo.Properties

Needed to identify the exact name of the resource and we have to decide if we want to use the JNDI service on not


JNDI.usage=yes  -- or no

Changes in the Java code

We must implement the Java interface ConnectionProvider to build our JNDIConnectionProvider. The way to access the remote JNDI resource is very easy.

Context initctx = new InitialContext();
Context ctx = (Context) initctx.lookup("java:/comp/env");
DataSource ds = (DataSource) ctx.lookup(jndiResourceName);
con = ds.getConnection();con = ds.getConnection(); 

these 4 lines are the core code of our class and allow us to get a connection defined at the application server.

Also we have to change all the references to the all ConnectionProviderImpl (that gets connection information through the Openbravo properties file) in the classes that only works in run time, for example the HTTPBaseServlet Java class has been changed an now get the connection using the new JNDIConnectionProvider:

public void makeConnection() throws PoolNotFoundException {
   if (myPool != null) {
     try {
     } catch (Exception ignored) {
     myPool = null;
   try {
     String strPoolFile = prefix + "/" + strBaseConfigPath + "/" +  PoolFileName;
   myPool = new JNDIConnectionProvider(strPoolFile, (!strPoolFile.startsWith("/") && !strPoolFile.substring(1, 1).equals(":")));
   } catch (Exception e) {
     throw new PoolNotFoundException(e.getMessage());

Notice that the connection pool has been built using the class JNDIConnectionProvider.

Jar needed for Tomcat

we need to add the JDBC driver for Oracle and postgresql in the location below


Retrieved from ""

This page has been accessed 5,588 times. This page was last modified on 8 June 2012, at 05:28. Content is available under Creative Commons Attribution-ShareAlike 2.5 Spain License.