Oct
09

Configuring and Using JDBC Datasources on Tomcat 6 with JNDI

We are using Weblogic 9.2 in our development, testing, and production environments.  Its usually best to use the same application server when developing on your local machine, but it can eat up a lot of resources that your computer may not handle.

I’ve run into this problem before so I decided to use Tomcat 6.0.18 on my machine for testing locally.  The only thing I really had to worry about was using JNDI lookup to retrieve a DataSource for database connections.

I’ve used several references from the Tomcat documentation, but it wasn’t all too clear to me in the beginning.  This guide should walk you through step by step to configure and use JDBC DataSources on Tomcat 6 usign JNDI lookup.

I’ll be configuring a JDBC DataSource using the Oracle JDBC drivers.

Assuming you already have Tomcat installed and properly running, your directory structure should look like the following:

/bin
/conf
/lib
/logs
/temp
/webapps
/work

in the conf directory, you will see these files:

/conf
catalina.policy

catalina.properties

context.xml

logging.properties

server.xml

tomcat-users.xml

web.xml

You will need to modify the server.xml file

Inside the <GlobalNamingResources> block, you will need to add the following entry:

 <Resource name="jndi_name_goes_here" auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@db_host:db_port:db_sid"
username="user_id" password="pwd"/>

So, as an example, my entry looks like the following:

 <Resource name="jdbc/coast" auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@development_host:7000:coast"
username="coast" password="coast" maxActive="20" maxIdle="10"
maxWait="-1"/>

Since I am using Oracle’s JDBC drivers, I will need to add their jar file in the tomcat’s lib directory.
You can download it from here:
http://download.oracle.com/otn/utilities_drivers/jdbc/10204/ojdbc14.jar
or
http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/htdocs/jdbc_10201.html

Next, assuming you already have a web application created, your directory structure should have these directories and files:

web_app_directory
    /WEB-INF
/WEB-INF/web.xml
/META-INF
/META-INF/context.xml

Next, you will need to add a META-INF directory to your web project.

Inside that META-INF directory, you will need to add a context.xml file.

Your context.xml file should look like the following:

<Context>

<ResourceLink name="jdbc/coast" global="jdbc/coast" type="javax.sql.DataSource" />

</Context>

Next, you will need to update the web.xml file in the WEB-INF folder.

You should add an entry in the end of the file before closing the <web-app> tag like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
...
...
<resource-ref>
<description>CoastDatasource</description>
<res-ref-name>jdbc/coast</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

</web-app>

You should now be able to retrieve the DataSource within your web application.

You can test your configuration by creating a small jsp with the following code:

<%@ page import="javax.naming.Context" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.sql.DataSource" %>

<%

Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/myoracle");
Connection conn = ds.getConnection();

%>

Apr
20

Displaytag issues when migrating from Weblogic 8.1 to 9.2

We use displaytag heavily in our application. We are in the process of upgrading our application server from Weblogic 8.1 SP4 to Weblogic 9.2 MP1. During our tests we’ve run into some issues with displaytag on 9.2.

Take for example, this simple code from their website:

<display table id="row" name="mylist">
  <display:column title="row number" >
    <c:out value="${row_rowNum}"/>
  </display:column>
  <display:column title="name" >
    <c:out value="${row.first_name}"/>
    <c:out value="${row.last_name}"/>
  </display:column>
</display:table>

This would work fine in 8.1, showing the row number in the first column and the name in the second column.

However, in 9.2, everything would appear blank.

Here’s a snippet from the tld file:

<attribute>
  <name>id</name>
  <required>false</required>
  <rtexprvalue>true</rtexprvalue>
  <description>See "uid".</description>
</attribute>

<attribute>
  <name>uid</name>
  <required>false</required>
  <rtexprvalue>true</rtexprvalue>
  <description>
Unique id used to identify this table. The object representing the current
row is also added to the pageContext under this name, so that you can refer
to it in column bodies using ${uid}. You can also use uid_rowNum to refer
to the current row number. Two tables in the same page can't have the same
id (paging and sorting will affect both). If no "htmlId" is specified the
same value will be used for the html id of the generated table.
  </description>
</attribute>

In the code sample, we are using the “id” attribute. However, if you look at the generated java code from the jsp in 8.1, you will see that it calls the “setUid” method instead of the”setId” method. In 9.2, it calls the “setId” method instead (and rightfully so). I still have yet to determine why the jsp compiler in 8.1 calls the “setUid” method since we never used the attribute to begin with.

So to get the same results in 9.2, we have to use the “uid” attribute instead of the “id” attribute in the <display:table> element

<display table uid=”row” name=”mylist”>
  <display:column title=”row number” >
    <c:out value=”${row_rowNum}”/>
  </display:column>
  <display:column title=”name” >
    <c:out value=”${row.first_name}”/>
    <c:out value=”${row.last_name}”/>
  </display:column>
</display:table>

» Newer posts