Configure Database Connections Using JNDI

You can configure for database connections using a Java Naming and Directory Interface (JNDI) data source when you have installed  to run on a Java container, such as Apache Tomcat. This article includes examples of how to configure for database connections when you have installed on Tomcat and on Oracle WebLogic Server.
lac42
You can configure for database connections using a Java Naming and Directory Interface (JNDI) data source when you have installed 
CA Live API Creator
 to run on a Java container, such as Apache Tomcat. This article includes examples of how to configure for database connections when you have installed on Tomcat and on Oracle WebLogic Server.
After you have configured for database connections using a JNDI data source, you can connect to databases using a JNDI data source in API Creator.
For more information about how to add a connection to a database using a JNDI data source, see Database Connectivity.
In this article:
Example for Tomcat
This example uses Apache Derby as your database. It illustrates one method to configuring for database connections using JNDI when you have installed 
CA Live API Creator
 to run on a Tomcat Java container. Other methods include scoping the JNDI resource more narrowly and changing its options.
For more information about these other methods, see the Apache Tomcat documentation.
Verify the Prerequisites
Before you can configure Tomcat with JNDI settings, ensure that you have completed the following prerequisite steps:
  • You have defined a JDBC data source through Tomcat's JNDI definition.
  • You have verified that the 
    derby-<version>.jar
     JAR file (the JDBC driver) is in the 
    {CATALINA_HOME}/lib
     directory.
    For more information about how to install the JavaDB/Apache Derby JDBC driver, see Derby Data Source.
Configure Tomcat with JNDI Settings
  1. Open the 
    {CATALINA_HOME}/conf/context.xml
    file, insert the following XML code within the
    <Context>
    tag, replace with your data source name, and then save your changes and close the file:
    <ResourceLink global="jdbc/
    data source name
    " name="jdbc/
    data source name
    " type="javax.sql.DataSource"/>
  2. Open the 
    {CATALINA_HOME}/conf/server.xml
     file, insert the following XML code within the 
    <GlobalNamingResources>
     tag, replace with your data source name, and then save and close the file:
    <Resource accessToUnderlyingConnectionAllowed="true" auth="Container" defaultAutoCommit="false"
        driverClassName="com.mysql.jdbc.Driver" initialSize="10" logAbandoned="true" maxTotal="500"
        maxIdle="0" maxWaitMillis="15000" minIdle="0" name="jdbc/
    data source name
    " password="mypassword"
        removeAbandonedOnMaintenance="true" removeAbandonedTimeout="30" timeBetweenEvictionRunsMillis="30000"
        type="javax.sql.DataSource" url="jdbc:mysql://localhost/
    data source name
    " username="myuser"/>
    If your database is MySQL, you might have to specify a URL in the following form:
    jdbc:mysql://dbserver.acme.com:3306/AuthTokensDB?sessionVariables=sql_mode%3DANSI_QUOTES
The Tomcat Java container is configured with JNDI settings.
Example for WebLogic Server
The following example illustrates one method to configuring for database connections using JNDI when you have installed 
CA Live API Creator
 to run on a WebLogic Server container. 
Verify the Prerequisites
Before you can configure the 
CALiveAPICreator.war
 WAR file with JNDI settings, ensure that you have completed the following prerequisite steps:
  • You have defined a JDBC data source through WebLogic Server's JNDI definition.
    For more information about how to do this, see the Oracle documentation.
  • You have access to the 
    jar
     command.
Configure the WAR File with JNDI Settings
  1. Copy of the 
    CALiveAPICreator.war
    WAR file to a temporary location.
  2. Extract the
    WEB-INF/web.xml
     file from the 
    CALiveAPICreator.war
     WAR file by issuing the following command:
    jar xvf CALiveAPICreator.war WEB-INF/web.xml
  3. Open the  
    WEB-INF/web.xml
     file. The following shows an example of the file contents:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" metadata-complete="true" version="3.0">
    <display-name>CA Live API Creator 5.0.00</display-name>
    <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
      <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <listener>
      <listener-class>com.kahuna.server.servlet.KahunaServletContextListener</listener-class>
    </listener>
    etc...
  4. Using a text editor, after the 
    </welcome-file-list>
     section, insert a 
    <resource-ref>
     section for each JNDI data source that WebLogic Server must access, replace the 
    <res-ref-name>
     attribute with the name of your JDBC data source, and then save your changes and close the file:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" metadata-complete="true" version="3.0">
    <display-name>CA Live API Creator 5.0.00</display-name>
    <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
      <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <resource-ref>
      <description>Example JNDI</description>
      <res-ref-name>jdbc/
    data source name
    </res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
    </resource-ref>
    <listener>
      <listener-class>com.kahuna.server.servlet.KahunaServletContextListener</listener-class>
    </listener>
    etc...
  5. Put the
    WEB-INF/web.xml
    file back into the 
    CALiveAPICreator.war
     WAR file by issuing the following command:
    jar uvf CALiveAPICreator.war WEB-INF/web.xml
  6. Deploy the 
    CALiveAPICreator.war
     WAR file into WebLogic Server.
The WebLogic Server Java container is configured with JNDI settings.