{"record":{"id":"ee0c1cf7d57ba964","repo":"mybatis/mybatis-3","slug":"could-not-instantiate-cache-decorator-cause","errorCode":null,"errorMessage":"Could not instantiate cache decorator ({}). Cause: {}","messagePattern":"Could not instantiate cache decorator \\((.+?)\\)\\. Cause: (.+?)","errorType":"exception","errorClass":"CacheException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/mapping/CacheBuilder.java","lineNumber":206,"sourceCode":"    }\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) {\n      throw new CacheException(\"Could not instantiate cache decorator (\" + cacheClass + \"). Cause: \" + e, e);\n    }\n  }\n\n  private Constructor<? extends Cache> getCacheDecoratorConstructor(Class<? extends Cache> cacheClass) {\n    try {\n      return cacheClass.getConstructor(Cache.class);\n    } catch (Exception e) {\n      throw new CacheException(\"Invalid cache decorator (\" + cacheClass + \").  \"\n          + \"Cache decorators must have a constructor that takes a Cache instance as a parameter.  Cause: \" + e, e);\n    }\n  }\n}\n","sourceCodeStart":188,"sourceCodeEnd":219,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/mapping/CacheBuilder.java#L188-L219","documentation":"CacheBuilder.newCacheDecoratorInstance() throws CacheException when a cache decorator's constructor Cache(Cache delegate) was located but newInstance(base) failed — the decorator constructor threw or the class could not be initialized. The decorator class and cause are included.","triggerScenarios":"A custom decorator listed in <cache-ref>/CacheBuilder.addDecorator (or the standard decorator chain) whose constructor throws, e.g. allocating resources based on the delegate's configuration.","commonSituations":"Custom decorator implementations that validate the delegate cache in the constructor and reject it (e.g. requires a TransactionalCache but receives a plain one); decorators with side effects (metrics registration) failing in constrained environments.","solutions":["Inspect the chained cause and fix the exception thrown inside the decorator constructor.","Keep decorator constructors side-effect free: only store the delegate; do work lazily.","Verify the decorator is designed for the delegate it wraps (check delegate type/interfaces before use)."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  Cache decorated = new CacheBuilder(id).implementation(MyCache.class)\n      .addDecorator(MyDecorator.class).build();\n} catch (CacheException e) {\n  throw new ConfigurationException(\"Decorator failed: \" + e.getCause(), e);\n}","preventionTips":["Decorator constructors must only store the delegate — no validation that throws, no resource acquisition.","Unit-test each decorator wrapping the exact base cache class used in production.","Check the chained cause first; it distinguishes constructor bugs from configuration issues."],"tags":["mybatis","cache","decorator","instantiation"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}