Saturday, November 27, 2010

Configuring JNDI data source in Tomcat

By using Java Naming and Directory Interface (JNDI) service, one can connect resources of different technologies.

A typical web application in Tomcat has a file called as context.xml in which the context path (root) of the web application is defined. Usually this file is located in META-INF directory of you web application (see figure 1). You can configure a JNDI data source in Tomcat by adding a declaration for your resource to this file. This is the application's context container, which enables you to specify application meta-data necessary for the server in order to deploy and run the application. There are various locations where you can specify context elements, such as your server's global i.e. tomcat-install-dir/conf/context.xml

[caption id="attachment_109" align="alignnone" width="264" caption="Figure 1: META-INF/context.xml"]context.xml[/caption]

Add the following Resource tag as a declaration for the JNDI resource. The code below is an example of how you can declare JNDI resource for MySQL database.

<?xml version="1.0"?>
<Context path="/abc">
<Resource name="jdbc/jndi-name"
auth="container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="yourusername"
password="yourpassword"
driverClass="com.mysql.jdbc.Driver"
url="jdbc:mysql://hostname:3306/databasename" />
</Context>

0 comments:

Post a Comment