{"record":{"id":"4bcf2c257bbfaa29","repo":"mybatis/mybatis-3","slug":"there-was-an-error-configuring-jndidatasourcetrans","errorCode":null,"errorMessage":"There was an error configuring JndiDataSourceTransactionPool. Cause: ${cause}","messagePattern":"There was an error configuring JndiDataSourceTransactionPool\\. Cause: (.+?)","errorType":"exception","errorClass":"DataSourceException","httpStatus":null,"severity":"critical","filePath":"src/main/java/org/apache/ibatis/datasource/jndi/JndiDataSourceFactory.java","lineNumber":59,"sourceCode":"  public void setProperties(Properties properties) {\n    try {\n      InitialContext initCtx;\n      Properties env = getEnvProperties(properties);\n      if (env == null) {\n        initCtx = new InitialContext();\n      } else {\n        initCtx = new InitialContext(env);\n      }\n\n      if (properties.containsKey(INITIAL_CONTEXT) && properties.containsKey(DATA_SOURCE)) {\n        Context ctx = (Context) initCtx.lookup(properties.getProperty(INITIAL_CONTEXT));\n        dataSource = (DataSource) ctx.lookup(properties.getProperty(DATA_SOURCE));\n      } else if (properties.containsKey(DATA_SOURCE)) {\n        dataSource = (DataSource) initCtx.lookup(properties.getProperty(DATA_SOURCE));\n      }\n\n    } catch (NamingException e) {\n      throw new DataSourceException(\"There was an error configuring JndiDataSourceTransactionPool. Cause: \" + e, e);\n    }\n  }\n\n  @Override\n  public DataSource getDataSource() {\n    return dataSource;\n  }\n\n  private static Properties getEnvProperties(Properties allProps) {\n    Properties contextProperties = null;\n    for (Entry<Object, Object> entry : allProps.entrySet()) {\n      String key = (String) entry.getKey();\n      String value = (String) entry.getValue();\n      if (key.startsWith(ENV_PREFIX)) {\n        if (contextProperties == null) {\n          contextProperties = new Properties();\n        }\n        contextProperties.put(key.substring(ENV_PREFIX.length()), value);","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/datasource/jndi/JndiDataSourceFactory.java#L41-L77","documentation":"JndiDataSourceFactory throws DataSourceException when JNDI lookup of the configured DataSource fails with a NamingException. The factory reads the 'initial_context' and 'data_source' properties (and any prefixed 'env.' properties as JNDI environment), does InitialContext.lookup, and wraps any NamingException with the message 'There was an error configuring JndiDataSourceTransactionPool. Cause: <exception>'. The cause string identifies the exact JNDI problem (NameNotFoundException, NoInitialContextException, etc.).","triggerScenarios":"Configuring type=\"JNDI\" in mybatis-config.xml with an initial_context/data_source name that does not exist in the application server's JNDI tree; running outside a container (standalone app or unit test) with no JNDI provider; misspelled JNDI names; missing 'env.' prefixed properties needed to reach a remote JNDI provider; DataSource not deployed/bound in the app server.","commonSituations":"Migrating an app from a full app server (where java:comp/env/jdbc/... is bound) to standalone/Spring Boot without a JNDI provider; typo in the JNDI name in XML config; the datasource not yet deployed at MyBatis initialization time; wrong env entries (INITIAL_CONTEXT_FACTORY/PROVIDER_URL) for remote JNDI.","solutions":["Read the appended Cause in the exception message: NameNotFoundException means bad name, NoInitialContextException means no JNDI provider","Verify the exact JNDI name with your container's admin tools (e.g., WildFly/JBoss jboss-cli, Tomcat's context.xml Resource name) and fix the <property name=\"data_source\"> value","For remote JNDI, supply 'env.' prefixed properties (env.initial_context_factory, env.provider_url, etc.) so InitialContext is constructed with them","If running standalone (no container), switch to a DataSourceFactory that creates connections directly (UNPOOLED/POOLED) instead of JNDI","Ensure the DataSource is bound before MyBatis builds its Configuration (deployment ordering in the app server)"],"exampleFix":"<!-- before: wrong name / no provider -->\n<dataSource type=\"JNDI\">\n  <property name=\"data_source\" value=\"java:comp/env/jbdc/Users\"/> <!-- typo -->\n</dataSource>\n\n<!-- after -->\n<dataSource type=\"JNDI\">\n  <property name=\"initial_context\" value=\"java:comp/env\"/>\n  <property name=\"data_source\" value=\"jdbc/UsersDS\"/>\n</dataSource>","handlingStrategy":"try-catch","validationCode":"// Optionally verify JNDI binding before MyBias builds the DataSource:\ntry {\n  InitialContext ctx = new InitialContext();\n  Object ds = ctx.lookup(\"java:comp/env/jdbc/UsersDS\");\n  if (!(ds instanceof DataSource)) throw new IllegalStateException(\"Not a DataSource\");\n} catch (NamingException e) {\n  // bind/fix before proceeding\n}","typeGuard":null,"tryCatchPattern":"try {\n  DataSourceFactory factory = new JndiDataSourceFactory();\n  factory.setProperties(props);\n} catch (DataSourceException e) {\n  Throwable cause = e.getCause(); // NamingException with details\n  // NameNotFound -> fix name; NoInitialContext -> run inside a container or set env.* properties\n}","preventionTips":["Validate JNDI names against the container's JNDI tree in an integration test","For remote JNDI, always provide env.initial_context_factory and env.provider_url","In standalone apps, avoid type=JNDI entirely"],"tags":["mybatis","jndi","datasource","configuration","naming"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}