JNDI Example for Apache Tomcat

This article includes an an example of using a JNDI data source when you have installed  to run on Apache Tomcat. Other Java containers (such as single-user version of  based on Jetty) use different methods to define JNDI data sources. After you have defined your JNDI data source, the behavior in  is the same as using other data sources.
calac41
This article includes an an example of using a JNDI data source when you have installed 
CA Live API Creator
 to run on Apache Tomcat. Other Java containers (such as single-user version of
CA Live API Creator
 based on Jetty) use different methods to define JNDI data sources. After you have defined your JNDI data source, the behavior in
CA Live API Creator
 is the same as using other data sources.
In this article:
Configure for Database Connections using a JNDI Data Source
The following example illustrates a method for configuring for database connections using a JNDI data source by modifying the Tomcat XML configuration files. Another method is to scope the JNDI resource more narrowly and change its options. This example uses Apache Derby as your database. 
This method is not necessarily the best thing to do, but it is the easiest.
For more information about the other methods, see the Apache Tomcat documentation.
Verify the Prerequisites
Before you connect to another database using a JNDI data source, ensure that you have completed the following:
  • You have defined a JNDI data source in your application server.
  • You have verified that the Derby JAR is in the 
    {CATALINA_HOME}/lib
     directory.
Configure your Admin Database
Open the 
{CATALINA_HOME}/conf/server.xml
 file, insert the following XML code within the
<GlobalNamingResources>
tag, save your changes, and then close the file:
<Resource accessToUnderlyingConnectionAllowed="true"
    auth="Container"
    defaultAutoCommit="false"
    driverClassName="org.apache.derby.jdbc.EmbeddedDriver"
    initialSize="10"
    logAbandoned="true"
    maxIdle="0"
    maxTotal="500"
    maxWaitMillis="15000"
    minIdle="0"
    name="jdbc/MyDB"
    removeAbandonedOnMaintenance="true"
    removeAbandonedTimeout="30"
    timeBetweenEvictionRunsMillis="30000"
    type="javax.sql.DataSource"
    url="jdbc:derby:/tmp/MyDB;create=true"
    username="MyDB"/>
The JNDI data source is configured.
Configure the Tomcat Container with JNDI Settings
Open the 
{CATALINA_HOME}/conf/context.xml
file, insert the following XML code within the
<Context>
tag, save your changes, and then close the file:
<ResourceLink global="jdbc/MyDB" name="jdbc/MyDB" type="javax.sql.DataSource"/>
The Tomcat container is configured with JNDI settings.
Connect to a Database using a JNDI Data Source
  1. In API Creator, create your API by connecting to an existing database. On the APIs page, click
    Create New API
    .
    The Connection wizard opens.
    For more information about how to create an API and connect to a JNDI data source, see Create your API.
  2. Click the 
    Database First
     approach.
  3. Select the
    JNDI Data Source
    option, and then specify a simple name for your JNDI data source. For example,
    jdbc/MyDB
    .
You are connected to a database using a JNDI data source.