How can I use JNDI in my application server?

by Michela Frigerio
5,668 views Published on Oct 24, 2011
Applies to: All versions
Table of contents

Introduction

The Java Naming and Directory Interface (JNDI) is an API for directory service that allows clients to discover and lookup data and objects via a name. Like all Java APIs that interface with host systems, JNDI is independent of the underlying implementation. Additionally, it specifies a service provider interface (SPI) that allows directory service implementations to be plugged into the framework.

Using JNDI for the database connection means that is the Application Server to manage the connection and not the Web Application. In this way the Application Server, depending on the connection pool, opens and provides the connection for all the applications that uses JNDI. This can be useful, for example, when the same database is used by different Web Applications and you do not want to specify the connection for each one of them.

JNDI in WebRatio

You can manage the database connection properties of your Web Project, selecting the Database node from the Outline and looking at the Properties View. This view has different tabs. The first tab manages the connection at compile time, which means that these information are used by WebRatio to connect to the database and to synchronize it. If you do not specify anything else, the same information are used at runtime. You can modify this settings choosing the "Runtime" tab and filling the connection information to use.

  • JNDI Name: The JNDI name of the connection. The JNDI name must be something like "java:comp/env/jdbc/MyDataSource", where MyDataSource is the name of your datasource.
  • Ignore Schema: A flag stating whether the Hibernate Mapping file should include the Schema declaration or not. This propery is useful when the database schema is not the same in test environment and production environment.

JNDI in the Application Server

Now you must configure your Application Server in order to manage the database connection. This configuration changes depending on the Application Server and the database. Here are the guidelines for Tomcat and Resin.

Tomcat 5.5

  • Modify the context.xml file placed under the directory $CATALINA_HOME/conf adding the following code fragment. The attributes values of this node are different depending on the database type. This sample shows how to connect to a PostgreSQL database. You can find information about other database types at this page.

<Resource name="jdbc/MyDataSource" auth="Container"

              type="javax.sql.DataSource" username="username" password="password"

              driverClassName="org.postgresql.Driver" url="jdbc:postgresql://localhost:5432/dbName"

              maxActive="8" maxIdle="4"/>

  • Copy the JDBC driver in the folder $CATALINA_HOME/common/lib.
  • Restart the application server in order to apply changes.

Resin 3.0

  • Modify the resin.conf file placed under the directory $RESIN_HOME/conf adding the following code fragment. The attributes values of this node are different depending on the database type. This sample shows how to connect to a PostgreSQL database. You can find information about other database types at this page.

         <database>

           <jndi-name>jdbc/MyDataSource</jndi-name>

           <driver type="org.postgresql.Driver">

             <url>jdbc:postgresql://127.0.0.1:5432/test</url>

             <user>username</user>

             <password>password</password>

            </driver>

            <prepared-statement-cache-size>8</prepared-statement-cache-size>

            <max-connections>20</max-connections>

            <max-idle-time>30s</max-idle-time>

          </database>

  • Copy the JDBC driver in the folder $RESIN_HOME/lib.
  • Restart the application server in order to apply changes.