Projects:JNDI Service/Technical Documentation
Contents |
JNDI Service - Technical Documentation
General Considerations
Overview
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"> <Resource auth="Container" name="jdbc/openbravo" type="javax.sql.DataSource" driverClassName="oracle.jdbc.driver.OracleDriver" password="tad" maxIdle="5" maxWait="10000" username="tad" url="jdbc:oracle:thin:@localhost:1521:OB" maxActive="20" factory="org.apache.commons.dbcp.BasicDataSourceFactory"/> </Context>
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" url="jdbc:postgresql://127.0.0.1:5432/openbravo" username="postgres" password="postgres" maxActive="20" maxIdle="10" maxWait="-1"/> </Context>
Changes in Web.xml file
We need to define the JNDI resource:
<resource-ref> <description>Oracle Datasource example</description> <res-ref-name>jdbc/openbravo</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
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.resourceName=jdbc/openbravo 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 { myPool.destroy(); } 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
(tomcat_home)->common->lib