{"record":{"id":"023d7d95412d8af6","repo":"brettwooldridge/HikariCP","slug":"jndi-context-does-not-found-for-datasourcejndi","errorCode":null,"errorMessage":"JNDI context does not found for dataSourceJNDI : ${jndiName}","messagePattern":"JNDI context does not found for dataSourceJNDI : (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/zaxxer/hikari/HikariJNDIFactory.java","lineNumber":69,"sourceCode":"         return createDataSource(properties, nameCtx);\n      }\n      return null;\n   }\n\n   private DataSource createDataSource(final Properties properties, final Context context) throws NamingException\n   {\n      var jndiName = properties.getProperty(\"dataSourceJNDI\");\n      if (jndiName != null) {\n         return lookupJndiDataSource(properties, context, jndiName);\n      }\n\n      return new HikariDataSource(new HikariConfig(properties));\n   }\n\n   private DataSource lookupJndiDataSource(final Properties properties, final Context context, final String jndiName) throws NamingException\n   {\n      if (context == null) {\n         throw new RuntimeException(\"JNDI context does not found for dataSourceJNDI : \" + jndiName);\n      }\n\n      var jndiDS = (DataSource) context.lookup(jndiName);\n      if (jndiDS == null) {\n         final var ic = new InitialContext();\n         jndiDS = (DataSource) ic.lookup(jndiName);\n         ic.close();\n      }\n\n      if (jndiDS != null) {\n         var config = new HikariConfig(properties);\n         config.setDataSource(jndiDS);\n         return new HikariDataSource(config);\n      }\n\n      return null;\n   }\n}","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/brettwooldridge/HikariCP/blob/a4d93f4f85517f90e632b795486d7102e933d7ff/src/main/java/com/zaxxer/hikari/HikariJNDIFactory.java#L51-L87","documentation":"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.","triggerScenarios":"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).","commonSituations":"Migrating an app between servlet containers, missing <Resource> / resource-ref configuration, standalone tests exercising HikariJNDIFactory without a JNDI provider.","solutions":["Verify the container's JNDI Resource definition and that lookup happens inside a container-managed context","Ensure the web.xml resource-ref and context.xml Resource entries match the dataSourceJNDI name","If running outside a container, remove dataSourceJNDI and configure jdbcUrl/driver directly instead of a JNDI lookup","In tests, provide an InitialContextFactory (e.g. simple-jndi) or mock the Context"],"exampleFix":"// before (context.xml Resource params include dataSourceJndiName but context lookup context is null)\n<Parameter name=\"dataSourceJNDI\" value=\"java:comp/env/jdbc/mydb\" />\n\n// after: define the Resource properly so the container supplies a non-null Context\n<Resource name=\"jdbc/mydb\" auth=\"Container\"\n          factory=\"com.zaxxer.hikari.HikariJNDIFactory\"\n          type=\"javax.sql.DataSource\" ... />","handlingStrategy":"validation","validationCode":"Context ctx = null;\ntry { ctx = new InitialContext(); }\ncatch (NamingException e) { /* container JNDI missing: do not use dataSourceJNDI config */ }","typeGuard":null,"tryCatchPattern":"try { ds = (DataSource) new HikariJNDIFactory().getObjectInstance(...); }\ncatch (RuntimeException e) {\n    if (e.getMessage().contains(\"JNDI context does not found\")) { /* fix Resource config or drop JNDI */ }\n    throw e;\n}","preventionTips":["Declare JNDI Resources correctly in context.xml/web.xml","In non-container environments use jdbcUrl config instead of dataSourceJNDI","Provide an InitialContextFactory in tests"],"tags":["hikaricp","jndi","configuration","deployment"],"backgroundTag":null,"analyzedSha":"a4d93f4f85517f90e632b795486d7102e933d7ff","analyzedAt":"2026-08-14T12:11:37.292Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}