brettwooldridge/HikariCP · error · RuntimeException

JNDI context does not found for dataSourceJNDI : ${jndiName}

Error message

JNDI context does not found for dataSourceJNDI : ${jndiName}

What it means

HikariJNDIFactory (used to create pools from JNDI-bound properties) throws RuntimeException when dataSourceJNDI is configured but the passed-in JNDI Context is null, so the lookup cannot even be attempted. This is a factory wiring error, not a missing name error.

Source

Thrown at src/main/java/com/zaxxer/hikari/HikariJNDIFactory.java:69

         return createDataSource(properties, nameCtx);
      }
      return null;
   }

   private DataSource createDataSource(final Properties properties, final Context context) throws NamingException
   {
      var jndiName = properties.getProperty("dataSourceJNDI");
      if (jndiName != null) {
         return lookupJndiDataSource(properties, context, jndiName);
      }

      return new HikariDataSource(new HikariConfig(properties));
   }

   private DataSource lookupJndiDataSource(final Properties properties, final Context context, final String jndiName) throws NamingException
   {
      if (context == null) {
         throw new RuntimeException("JNDI context does not found for dataSourceJNDI : " + jndiName);
      }

      var jndiDS = (DataSource) context.lookup(jndiName);
      if (jndiDS == null) {
         final var ic = new InitialContext();
         jndiDS = (DataSource) ic.lookup(jndiName);
         ic.close();
      }

      if (jndiDS != null) {
         var config = new HikariConfig(properties);
         config.setDataSource(jndiDS);
         return new HikariDataSource(config);
      }

      return null;
   }
}

View on GitHub (pinned to a4d93f4f85)

Solutions

  1. Verify the container's JNDI Resource definition and that lookup happens inside a container-managed context
  2. Ensure the web.xml resource-ref and context.xml Resource entries match the dataSourceJNDI name
  3. If running outside a container, remove dataSourceJNDI and configure jdbcUrl/driver directly instead of a JNDI lookup
  4. In tests, provide an InitialContextFactory (e.g. simple-jndi) or mock the Context

Example fix

// before (context.xml Resource params include dataSourceJndiName but context lookup context is null)
<Parameter name="dataSourceJNDI" value="java:comp/env/jdbc/mydb" />

// after: define the Resource properly so the container supplies a non-null Context
<Resource name="jdbc/mydb" auth="Container"
          factory="com.zaxxer.hikari.HikariJNDIFactory"
          type="javax.sql.DataSource" ... />
Defensive patterns

Strategy: validation

Validate before calling

Context ctx = null;
try { ctx = new InitialContext(); }
catch (NamingException e) { /* container JNDI missing: do not use dataSourceJNDI config */ }

Try / catch

try { ds = (DataSource) new HikariJNDIFactory().getObjectInstance(...); }
catch (RuntimeException e) {
    if (e.getMessage().contains("JNDI context does not found")) { /* fix Resource config or drop JNDI */ }
    throw e;
}

Prevention

When it happens

Trigger: Tomcat/JNDI resource factory invoked without a valid java:comp/env context; deploying in a container that never constructed the InitialContext; dataSourceJNDI property present in the Resource params while the factory receives a null Context (e.g. unit tests calling the factory directly).

Common situations: Migrating an app between servlet containers, missing <Resource> / resource-ref configuration, standalone tests exercising HikariJNDIFactory without a JNDI provider.

Related errors


AI-assisted analysis of brettwooldridge/HikariCP@a4d93f4f85 (2026-08-14). Data as JSON: /api/errors/023d7d95412d8af6. Report an issue: GitHub.