Keywords: Tomcat | JNDI | ResourceLink | Datasource Configuration | Derby
Abstract: This article provides an in-depth analysis of the common JNDI error "Name [jdbc/mydb] is not bound in this Context" in Tomcat servers. Through a specific case study, it demonstrates how to configure global datasource resources and correctly reference them in web applications. The paper explains the role of ResourceLink in context.xml, compares configuration differences among server.xml, web.xml, and context.xml, and offers complete solutions with code examples to help developers understand Tomcat's resource management mechanisms.
In Java-based web development using Tomcat as the application server, configuring datasources and accessing them via JNDI (Java Naming and Directory Interface) is a common practice. However, developers frequently encounter errors such as "Name [jdbc/mydb] is not bound in this Context," often due to improper resource configuration or context referencing issues. This article explores the causes and solutions for this error through a detailed case study.
Error Scenario Analysis
A user attempting to access a database via a JSP page with Tomcat 7.0.52 and an embedded Derby database encountered an exception during JNDI lookup. The error stack trace indicates:
javax.naming.NameNotFoundException: Name [jdbc/mydb] is not bound in this Context. Unable to find [jdbc].
This shows that the resource named "jdbc/mydb" could not be found in the current JNDI context. Although the user correctly configured global resources in server.xml and declared resource references in web.xml, the issue persisted.
Detailed Resource Configuration
In Tomcat, datasource resources can be configured at multiple levels, including global (server.xml) and application-level (context.xml). The user's configuration included:
- server.xml: Defined a global datasource named "jdbc/mydb" within
<GlobalNamingResources>, specifying the Derby driver and database path. - web.xml: Declared a resource reference to "jdbc/mydb" in
<resource-ref>with typejavax.sql.DataSource. - context.xml: Initially empty, containing only default monitored resource settings.
Despite the global resource definition, web applications cannot directly access resources in GlobalNamingResources without a ResourceLink to link them.
Role and Configuration of ResourceLink
ResourceLink is a key mechanism in Tomcat for linking global resources to web application contexts. It allows applications to access server-level resources via local JNDI names without redefining them. In this case, the missing component was precisely this link configuration.
The solution involves adding the following to META-INF/context.xml (or application-specific context.xml):
<ResourceLink name="jdbc/mydb"
global="jdbc/mydb"
type="javax.sql.DataSource" />
This configuration creates a link from the application context to the global resource, enabling successful resolution of JNDI lookups for "jdbc/mydb." Here, the name attribute specifies the JNDI name used within the application, global points to the global resource name, and type ensures type consistency.
Code Examples and Best Practices
To illustrate more clearly, here is a corrected JSP code snippet demonstrating proper JNDI lookup for a datasource:
<%@ page import="javax.naming.*, java.sql.*, javax.sql.*" %>
<%
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:/comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/mydb");
Connection conn = ds.getConnection();
// Perform database operations
conn.close();
%>
For configuration, it is recommended to follow these best practices:
- Define datasources in
server.xmlunder<GlobalNamingResources>for sharing across multiple applications. - Use
ResourceLinkin each web application'scontext.xmlto link resources, avoiding duplication. - Declare resource references in
web.xmlto enhance application portability and configuration clarity. - Ensure database driver JAR files (e.g., Derby) are placed in the
Tomcat/libdirectory for server classloader access.
Common Issues and Troubleshooting
Beyond missing ResourceLink, other issues may cause similar errors:
- Driver Class Not Found: Verify
driverClassNameis correct and ensure driver JARs are inTomcat/lib. - Incorrect Database Path: Check that the database file path in the
urlproperty exists and is accessible. - Context Restart Required: After modifying
context.xml, restart Tomcat or redeploy the application if necessary. - Namespace Conflicts: Ensure JNDI names are unique at both global and application levels to avoid redefinition.
Through systematic configuration and troubleshooting, JNDI resource binding issues can be efficiently resolved, ensuring stable database access for web applications.