Level: Intermediate Dhanashri Kudgavkar (dkudgavk@in.ibm.com), Informix-Interoperability Team, India Software Lab, IBM Prasanna Alur Mathada (amprasanna@in.ibm.com), Informix-Interoperability Team, India Software Lab, IBM Amitava Chakraborty (amitacha@in.ibm.com), Informix-Interoperability Team, India Software Lab, IBM
07 Aug 2008 Over the years the popularity of Apache Tomcat has picked up, and so has IBM® Informix® Dynamic Server.
Many users are eager to use these two products together, and in fact are trying to
combine and integrate them, but face a few issues in the process. In this article, you’ll learn how to solve these problems as you follow the steps to
configure the JNDI DataSource on Tomcat for Informix Dynamic Server.
Introduction
IBM Informix Dynamic server (IDS) is known worldwide for being exceptionally easy to manage, offering
high availability and high performing online transaction processing capabilities. It also helps to simplify and automate many tasks associated with deploying databases instances for medium to enterprise business applications. The latest release of IBM Informix Dynamic server, IDS v11.50 (code named Cheetah 2), provides high-availability with the capabilities of HDR technology and MACH-11, a new, flexible, extensible system-monitoring and administration tool (OAT), and a good number of security features.
Apache Tomcat provides an HTTP Web server environment for Java™ code and has management tools to configure and edit configuration files. The latest release of Apache Tomcat 6.0 is designed to run on Java SE 5.0 and later versions, and uses the Eclipse JDT Java compiler to compile the JSP pages, which requires only JRE to run the Tomcat.
JNDI DataSource and its advantages in deployed applications
JNDI is an essential part of the Java 2 Platform, Enterprise Edition (J2EE) since it provides a standard interface to
locate Java objects, machines, users, and services, or to connect to databases. Naming and directory services are the
backbone of JDNI. A directory stores and organizes information in a hierarchical tree wherein each node in the tree
has a valid name with its own set of associated data. A directory service provides access to this information.
A naming service is a type of directory service which maps names of the network resources to their network addresses.
It is capable of attaching a name to an object in the network and also finding the object using this name. These
objects can be used by many applications. JNDI is used by Java-based clients to interact with naming and directory
systems. With JNDI, Java applications can read and write Java objects from directories.
A DataSource is a means of storing data. It is the representation of a data source in the Java programming language.
It has certain properties which describe the real world data source that it represents. Some of the data source properties
include:
- databaseName: The name of the database server to which application will connect
- dataSourceName: The logical name of the object being represented by the DataSource
- networkProtocol: The protocol used to communicate with the server
- password: The user's database password
- portNumber: The port to which the server is listening for requests
A DataSource object works with the JNDI naming service, and its creation and management is well separated from the application.
This makes the code more portable. Also, in case of any changes to the information about the data source, only the
data source properties are required to be changed instead of changing every application which uses this data source.
This helps in easy code maintenance.
Since a DataSource object is registered with a JNDI naming service, a Java
application can use a logical name for the data source and register this logical name with the JNDI naming service.
The JNDI naming service then supplies the DataSource object associated with this logical name to the applications, which
can then be used to connect to the data source it represents.
An added advantage of using the DataSource is that the application developer can use connection pooling feature to
improve performance and XADataSource for handling distributed transactions.
Configuring a JNDI DataSource on Informix
Step 1:
Declare one environment variable, CATALINA_HOME, pointing to the installation root directory of Tomcat.
Step 2:
Copy the driver files to $CATALINA_HOME/common/lib. .
So, for Informix JDBC driver ifxjdbc.jar and ifxjdbcx.jar should be copied to the above mentioned directory.
For JAVA Common Client (JCC) driver, db2jcc.jar should be copied to the above mentioned directory.
Step 3:
Open the file context.xml, which resides under $CATALINA_HOME/conf directory. Add the following entries to context.xml.
Listing 1. For Informix JDBC drivers :
<Resource name="jdbc/<JNDI Name>"
auth="Container" type="javax.sql.DataSource"
driverClassName="com.informix.jdbc.IfxDriver"
url="jdbc:informix-sqli://<Informix Host>:<Informix Socket Port>/<Database Name
>:INFORMIXSERVER=<Informix Server Name>"
username="<User Name>"
password="<Password>" maxActive="100" maxIdle="30" maxWait="10000"
removeAbandoned="true" removeAbandonedTimeout="60" logAbandoned="true"/>
|
Listing 2. For JAVA common client driver (IBM Data Server driver for JDBC and SQLJ)
<Resource name="jdbc/<JNDI Name>"
auth="Container" type="javax.sql.DataSource"
driverClassName="com.ibm.db2.jcc.DB2Driver"
url="jdbc:db2://<Informix Host>:<Informix DRDA Port>/<Database Name>"
username="<User Name>"
password="<Password>" maxActive="100" maxIdle="30" maxWait="10000"
removeAbandoned="true" removeAbandonedTimeout="60" logAbandoned="true"/>
|
Step 4:
Open the file context.xml, residing under the $CATALINA_HOME/conf directory. Add these entries under context.xml section.
Listing 3. For Informix JDBC Drivers
<Resource
name="jdbc/<JNDI Name>"
type="javax.sql.DataSource"
validationQuery="select * from systables"
url="jdbc:informix-sqli://<Informix Host>:<Informix Socket Port>/<Database Name
>:INFORMIXSERVER=<Informix Server Name>"
password="<Password>"
driverClassName="com.informix.jdbc.IfxDriver"
maxActive="4"
maxWait="5000"
maxIdle="2"
username="<UserName>"/>
|
Listing 4. For JAVA common client driver (IBM Data Server driver for JDBC and SQLJ):
<Resource
name="jdbc/<JNDI Name>"
type="javax.sql.DataSource"
validationQuery="select * from systables"
url="jdbc:db2://<Informix Host>:<Informix DRDA Port>/<Database Name>;"
password="<Password>"
driverClassName="com.ibm.db2.jcc.DB2Driver"
maxActive="4"
maxWait="5000"
maxIdle="2"
username="<Username>"/>
|
Step 5:
Now go to the WEB-INF directory under the Web Application installation location,
( for example, $CATALINA_HOME/webapps/jsp-examples/WEB-INF ). Open the file named web.xml.
Make an entry for the JNDI DataSource as shown in Listing 5:
Listing 5. For JAVA Common Client Drivers :
<description>Informix Test Connection</description>
<resource-ref>
<description>Informix DB Connection</description>
<res-ref-name>jdbc/<JNDI Name></res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
|
Step 6:
Create a JSP file as shown in Listing 6 to test the connectivity and place it
in the application virtual directory.
Listing 6. For JAVA common client drivers :
<%@page import="java.sql.*"%>
<%@page import="javax.sql.DataSource"%>
<%@page import="javax.naming.*"%>
<html>
<body>
<%
Context initCtx=new InitialContext();
if ( initCtx == null )
{
out.println("No Context <br>");
}
else
{
out.println("Context Found <br>");
Context envContext = (Context) initCtx.lookup("java:/comp/env");
DataSource db = (DataSource) envContext.lookup("jdbc/informix");
if ( initCtx == null )
{
out.println("No Informix JDBC JNDI Found <br>");
}
else
{
out.println("Informix JDBC JNDI Found <br>");
Connection conn = db.getConnection();
out.println("Got Connection <br>");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Orders");
// Assuming there is table in your databse and one of the column is order id
out.println("order id <br> ================================= <br> ");
while(rs.next()){
out.print(rs.getString(1)+"<br>");
}
out.println("==========Test Completed======================= <br> ");
rs.close();
stmt.close();
conn.close();
}
|
Step 7:
Restart the Tomcat server for the changes to take effect.
Step 8:
Open your test JSP file to see whether you are able to get the data. At this point you should be able to access data from
your Informix database.
Summary
In this article, we’ve described how you can use IDS with Tomcat. Also
we’ve examined JNDI and its advantages. After walking through the steps described in this article, you should be able to
configure a JNDI DataSource making use of both the Informix JDBC driver and Java Common Client (JCC) driver.
Resources Learn
Get products and technologies
Discuss
About the authors  | 
|  | Dhanshri Kudgavkar works for the Interoperability team, ISL, India. She possesses vast experience with IDS behaviors and features. |
 | 
|  | Prasanna is a certified Informix professional. He currently works for the Interoperability team, ISL, India. |
 | 
|  | Amitava has more than eight years of experience on the different Informix products. Currently, as a lead on the Informix Interoperability team, he is responsible for ensuring Informix scalability to other prime products. |
Rate this page
|