{"record":{"id":"c5b5382aa569264d","repo":"redisson/redisson","slug":"unable-to-locate-redisson-instance-by-name-jn","errorCode":null,"errorMessage":"Unable to locate Redisson instance by name: \" + jndiName","messagePattern":"Unable to locate Redisson instance by name: \" \\+ jndiName","errorType":"exception","errorClass":"CacheException","httpStatus":null,"severity":"error","filePath":"redisson-hibernate/redisson-hibernate-52/src/main/java/org/redisson/hibernate/JndiRedissonRegionFactory.java","lineNumber":55,"sourceCode":"\r\n    private static final long serialVersionUID = -4814502675083325567L;\r\n\r\n    public static final String JNDI_NAME = CONFIG_PREFIX + \"jndi_name\";\r\n    \r\n    @Override\r\n    protected RedissonClient createRedissonClient(Properties properties) {\r\n        String jndiName = ConfigurationHelper.getString(JNDI_NAME, properties);\r\n        if (jndiName == null) {\r\n            throw new CacheException(JNDI_NAME + \" property not set\");\r\n        }\r\n        \r\n        Properties jndiProperties = JndiServiceImpl.extractJndiProperties(properties);\r\n        InitialContext context = null;\r\n        try {\r\n            context = new InitialContext(jndiProperties);\r\n            return (RedissonClient) context.lookup(jndiName);\r\n        } catch (NamingException e) {\r\n            throw new CacheException(\"Unable to locate Redisson instance by name: \" + jndiName, e);\r\n        } finally {\r\n            if (context != null) {\r\n                try {\r\n                    context.close();\r\n                } catch (NamingException e) {\r\n                    throw new CacheException(\"Unable to close JNDI context\", e);\r\n                }\r\n            }\r\n        }\r\n    }\r\n\r\n    @Override\r\n    public void stop() {\r\n    }\r\n\r\n}\r\n","sourceCodeStart":37,"sourceCodeEnd":72,"githubUrl":"https://github.com/redisson/redisson/blob/91188987c2a9023e7fedabff758681ad9b107f25/redisson-hibernate/redisson-hibernate-52/src/main/java/org/redisson/hibernate/JndiRedissonRegionFactory.java#L37-L72","documentation":"Thrown by JndiRedissonRegionFactory (hibernate-52 module) when context.lookup(jndiName) fails with a NamingException: no object is bound at the configured hibernate.cache.redisson.jndi_name, the JNDI provider cannot be reached, or the bound object cannot be returned as a RedissonClient. The NamingException is preserved as the cause and identifies which case applies.","triggerScenarios":"Hibernate 5.2+ startup with JndiRedissonRegionFactory where the jndi_name value does not exist in the JNDI tree (NameNotFoundException), the naming provider is unreachable (CommunicationException), or JNDI environment properties (java.naming.factory.initial / provider.url) are wrong so lookup targets the wrong registry.","commonSituations":"The component that creates and rebinds the RedissonClient runs after Hibernate initialization; deploying a WAR expecting java:comp/env/... names that require resource-ref mappings; local tests without any JNDI provider; environment-specific JNDI names hard-coded for one container but deployed to another.","solutions":["Decode the cause: NameNotFoundException → wrong name; CommunicationException → provider URL/connectivity; then fix accordingly.","Guarantee startup order: create Redisson.create(config) and rebind it in JNDI before the SessionFactory bean is constructed (e.g. @DependsOn / BeanFactoryPostProcessor in Spring).","Match the exact JNDI name including prefix (java:/ vs java:comp/env/) between binder and Hibernate config.","Supply required java.naming.* properties through the Hibernate properties so they reach the InitialContext."],"exampleFix":"// before: client created after Hibernate starts\n@Bean RedissonClient redisson() { return Redisson.create(config); }\n\n// after: bind before SessionFactory and declare dependency\n@Bean(destroyMethod = \"shutdown\") RedissonClient redisson() throws Exception {\n    RedissonClient r = Redisson.create(config);\n    new InitialContext().rebind(\"java:/redisson/RedissonClient\", r);\n    return r;\n}\n@Bean LocalContainerEntityManagerFactoryBean emf(@Autowired RedissonClient unused) { ... }","handlingStrategy":"validation","validationCode":"try (InitialContext ctx = new InitialContext(jndiProps)) {\n    Object o = ctx.lookup(jndiName);\n    if (!(o instanceof RedissonClient)) throw new IllegalStateException(\"Not a RedissonClient at \" + jndiName);\n} catch (NamingException e) {\n    throw new IllegalStateException(\"JNDI lookup failed for \" + jndiName, e);\n}","typeGuard":"private Optional<RedissonClient> lookupRedisson(String name) {\n    try (InitialContext ctx = new InitialContext()) {\n        return Optional.ofNullable(ctx.lookup(name))\n            .filter(RedissonClient.class::isInstance)\n            .map(RedissonClient.class::cast);\n    } catch (NamingException e) {\n        return Optional.empty();\n    }\n}","tryCatchPattern":"catch (CacheException e) {\n    NamingException cause = (NamingException) e.getCause();\n    if (cause instanceof NameNotFoundException) { /* correct the jndi_name value */ }\n    else if (cause instanceof CommunicationException) { /* fix provider connectivity */ }\n    else throw e;\n}","preventionTips":["Initialize and bind the RedissonClient before the SessionFactory bean; declare the dependency.","Pre-probe the JNDI binding in a startup check.","Externalize the JNDI name per environment instead of hard-coding."],"tags":["jndi","lookup","hibernate-52","redisson","startup"],"backgroundTag":null,"analyzedSha":"91188987c2a9023e7fedabff758681ad9b107f25","analyzedAt":"2026-08-14T11:39:42.619Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}