{"record":{"id":"bbee41c183e2a41e","repo":"mybatis/mybatis-3","slug":"sharedcache-failed-to-make-a-copy-of-a-non-seriali","errorCode":null,"errorMessage":"SharedCache failed to make a copy of a non-serializable object: {object}","messagePattern":"SharedCache failed to make a copy of a non-serializable object: (.+?)","errorType":"exception","errorClass":"CacheException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/cache/decorators/SerializedCache.java","lineNumber":56,"sourceCode":"\n  public SerializedCache(Cache delegate) {\n    this.delegate = delegate;\n  }\n\n  @Override\n  public String getId() {\n    return delegate.getId();\n  }\n\n  @Override\n  public int getSize() {\n    return delegate.getSize();\n  }\n\n  @Override\n  public void putObject(Object key, Object object) {\n    if ((object != null) && !(object instanceof Serializable)) {\n      throw new CacheException(\"SharedCache failed to make a copy of a non-serializable object: \" + object);\n    }\n    delegate.putObject(key, serialize((Serializable) object));\n  }\n\n  @Override\n  public Object getObject(Object key) {\n    Object object = delegate.getObject(key);\n    return object == null ? null : deserialize((byte[]) object);\n  }\n\n  @Override\n  public Object removeObject(Object key) {\n    return delegate.removeObject(key);\n  }\n\n  @Override\n  public void clear() {\n    delegate.clear();","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/cache/decorators/SerializedCache.java#L38-L74","documentation":"Thrown by SerializedCache.putObject() when the value being stored does not implement java.io.Serializable. SerializedCache is the decorator that activates when readOnly=false (or explicitly via <cache readOnly=\"false\"> / serialized settings): it deep-copies cached values via Java serialization, which is only possible for Serializable values, so a non-serializable object is rejected at put time, not silently stored.","triggerScenarios":"A second-level cache with default settings (readOnly=false enables serialization) on a mapper whose select returns objects that are not Serializable — e.g. a result type using Lombok @Builder without implementing Serializable, or java.time types without appropriate configuration, or entity classes missing 'implements Serializable'.","commonSituations":"Enabling <cache/> on a mapper for the first time and entities never needed Serializable before; adding fields of non-serializable types (e.g. a Supplier, an AtomicInteger, an unpicklable library type); Java records/entities in projects that never used the 2nd-level cache; switching readOnly=true to false.","solutions":["Make the cached result type implement java.io.Serializable (and ensure every nested field/element type is serializable too)","Mark non-data fields (locks, listeners) transient or exclude them from the result mapping","If deep-copy semantics are not needed and values are effectively immutable, set readOnly=\"true\" to bypass SerializedCache"],"exampleFix":"// before\npublic class User { private Long id; private String name; }\n\n// after\npublic class User implements Serializable {\n  private static final long serialVersionUID = 1L;\n  private Long id; private String name;\n}","handlingStrategy":"validation","validationCode":"Object result = executeQuery();\nif (cacheActive && result != null && !(result instanceof Serializable))\n  throw new IllegalStateException(\"type \" + result.getClass() + \" must implement Serializable before enabling the 2nd-level cache\");","typeGuard":"static boolean cacheableValueType(Class<?> c) { return Serializable.class.isAssignableFrom(c); }","tryCatchPattern":null,"preventionTips":["Make entities Serializable before adding <cache/>","Keep cached types free of non-serializable fields","Consider readOnly=true for immutable DTOs to skip serialization"],"tags":["mybatis","second-level-cache","serialization"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}