{"record":{"id":"37a42129deff0797","repo":"mybatis/mybatis-3","slug":"could-not-instantiate-cache-implementation-c","errorCode":null,"errorMessage":"Could not instantiate cache implementation ({}). Cause: {}","messagePattern":"Could not instantiate cache implementation \\((.+?)\\)\\. Cause: (.+?)","errorType":"exception","errorClass":"CacheException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/mapping/CacheBuilder.java","lineNumber":187,"sourceCode":"        }\n      }\n    }\n    if (InitializingObject.class.isAssignableFrom(cache.getClass())) {\n      try {\n        ((InitializingObject) cache).initialize();\n      } catch (Exception e) {\n        throw new CacheException(\n            \"Failed cache initialization for '\" + cache.getId() + \"' on '\" + cache.getClass().getName() + \"'\", e);\n      }\n    }\n  }\n\n  private Cache newBaseCacheInstance(Class<? extends Cache> cacheClass, String id) {\n    Constructor<? extends Cache> cacheConstructor = getBaseCacheConstructor(cacheClass);\n    try {\n      return cacheConstructor.newInstance(id);\n    } catch (Exception e) {\n      throw new CacheException(\"Could not instantiate cache implementation (\" + cacheClass + \"). Cause: \" + e, e);\n    }\n  }\n\n  private Constructor<? extends Cache> getBaseCacheConstructor(Class<? extends Cache> cacheClass) {\n    try {\n      return cacheClass.getConstructor(String.class);\n    } catch (Exception e) {\n      throw new CacheException(\"Invalid base cache implementation (\" + cacheClass + \").  \"\n          + \"Base cache implementations must have a constructor that takes a String id as a parameter.  Cause: \" + e,\n          e);\n    }\n  }\n\n  private Cache newCacheDecoratorInstance(Class<? extends Cache> cacheClass, Cache base) {\n    Constructor<? extends Cache> cacheConstructor = getCacheDecoratorConstructor(cacheClass);\n    try {\n      return cacheConstructor.newInstance(base);\n    } catch (Exception e) {","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/mapping/CacheBuilder.java#L169-L205","documentation":"CacheBuilder.newBaseCacheInstance() throws CacheException when the base cache class's constructor Cache(String id) was found but Constructor.newInstance(id) failed — the constructor threw, the class is abstract, or access failed. The cacheClass is named in the message and the original exception is chained.","triggerScenarios":"Specifying <cache type=\"com.x.MyCache\"/> where MyCache(String id) throws (e.g. it tries to connect to an external system or reads config that is missing); the class is abstract; or the constructor does something illegal (e.g. spawns threads that fail).","commonSituations":"Custom cache implementations doing heavy work in the constructor; abstract cache classes referenced by mistake; constructor logic depending on containers/resources not yet available at SqlSessionFactory build time.","solutions":["Check the chained cause for the exact constructor failure and fix it inside the cache class.","Make the constructor trivial: only store the id; do connections/initialization lazily or in initialize() via InitializingObject.","Confirm the class is concrete and public with a public Cache(String id) constructor."],"exampleFix":"// before\npublic class MyCache implements Cache {\n  public MyCache(String id) {\n    this.client = RedisClient.connect(id); // throws if server down -> build fails\n  }\n}\n\n// after\npublic class MyCache implements Cache {\n  private Client client;\n  public MyCache(String id) { this.id = id; }\n  private Client client() {\n    if (client == null) client = RedisClient.connect(id); // lazy\n    return client;\n  }\n}","handlingStrategy":"validation","validationCode":"// Assert the contract before registering the cache\nClass<? extends Cache> c = MyCache.class;\nif (java.lang.reflect.Modifier.isAbstract(c.getModifiers())) {\n  throw new IllegalStateException(c + \" must be concrete\");\n}\nc.getConstructor(String.class); // throws clearly if missing\nnew CacheBuilder(id).implementation(c).build();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep the Cache(String id) constructor trivial; move heavy work to initialize() or lazy paths.","Unit-test that new MyCache(\"test\") succeeds with no external systems running.","Register custom caches in a shared test config so constructor regressions surface in CI."],"tags":["mybatis","cache","instantiation","custom-cache"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}