Database Connection via JNDI

Connection to Database Using JNDI

A Java application can connect to a database through JNDI by defining a container-managed data source in the web application configuration and looking it up from a Java class.

Create the Environment

1

Sign in to the dashboard

Log in to the platform dashboard and create a Java environment.

2

Add a database node

Add a database server to the environment. The documented example uses MySQL.

Java environment with MySQL
Create the Java environment and include a MySQL database node.

Create the Database User

Create a database and user with the following values used in the source example:

Database namejelasticDb
Usernamejelastic
Passwordjelastic
!

Example credentials

The values above are taken from the documentation example. Use strong and unique credentials in a real environment.

Configure context.xml

Add the JNDI data-source resource to the application context.xml file:

<Context antiJARLocking="true" path="/JNDI">
    <Resource name="jdbc/jelasticDb"
              auth="Container"
              type="javax.sql.DataSource"
              maxActive="100"
              maxIdle="30"
              maxWait="10000"
              username="jelastic"
              password="jelastic"
              driverClassName="com.mysql.jdbc.Driver"
              url="jdbc:mysql://mysql-jndi-example.{hoster_domain}/jelasticDb"/>
</Context>

Configure web.xml

Declare the JNDI resource reference in the application web.xml file:

<resource-ref>
    <description>MySQL Datasource example</description>
    <res-ref-name>jdbc/jelasticDb</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

Keep the names identical

The resource name in context.xml, the reference name in web.xml, and the JNDI lookup name in the Java class must refer to the same data source.

Create the Java Connection Class

Create a Java class that retrieves the data source from the application-server naming context and returns database connections:

public class MyConnection {
    private DataSource dataSource;

    public MyConnection() {
        try {
            InitialContext context = new InitialContext();
            dataSource = (DataSource) context.lookup(
                "java:comp/env/jdbc/jelasticDb"
            );
        } catch (NamingException ex) {
            Logger.getLogger(
                MyConnection.class.getName()
            ).log(Level.SEVERE, null, ex);
        }
    }

    public Connection getConnection() {
        Connection conn = null;

        try {
            conn = dataSource.getConnection();
        } catch (SQLException ex) {
            Logger.getLogger(
                MyConnection.class.getName()
            ).log(Level.SEVERE, null, ex);
        }

        return conn;
    }
}

The lookup path used by the class is:

java:comp/env/jdbc/jelasticDb

Expected Result

The Java application obtains a container-managed DataSource through JNDI and uses it to create database connections to the configured MySQL database.

Important Notes

  • Create the database node before configuring the web application.
  • Replace {hoster_domain} with the domain used by the hosting provider.
  • Keep the JNDI resource name consistent across all configuration files and Java code.
  • Ensure that the MySQL JDBC driver is available to the application server.
  • Replace the example username and password with secure credentials.
  • Review application-server and application logs when the JNDI lookup fails.

Common Issues and Solutions

JNDI name is not foundCheck that context.xml, web.xml, and the Java lookup use the same resource name.
Database connection failsVerify the database hostname, database name, username, password, and database-node status.
JDBC driver is unavailableConfirm that the MySQL JDBC driver is installed and available to the application server.
NamingException occursReview the resource declaration, deployment context, XML syntax, and application-server logs.
SQLException occursCheck the database credentials, network access, user privileges, and database URL.

What’s Next?

  • Create Database Server
  • Connection to Database Using Hibernate
  • Database Configuration