{"record":{"id":"d08ad1cf44f2fdef","repo":"flowable/flowable-engine","slug":"couldn-t-serialize-value-in-variable","errorCode":null,"errorMessage":"Couldn't serialize value '' in variable ''","messagePattern":"Couldn't serialize value '' in variable ''","errorType":"exception","errorClass":"ActivitiException","httpStatus":null,"severity":"error","filePath":"modules/flowable5-engine/src/main/java/org/activiti/engine/impl/variable/SerializableType.java","lineNumber":105,"sourceCode":"                        .getDbSqlSession()\n                        .addDeserializedObject(new DeserializedObject(this, valueFields.getCachedValue(), byteArray, (VariableInstanceEntity) valueFields));\n            }\n        }\n\n        super.setValue(byteArray, valueFields);\n    }\n\n    public byte[] serialize(Object value, ValueFields valueFields) {\n        if (value == null) {\n            return null;\n        }\n        ByteArrayOutputStream baos = new ByteArrayOutputStream();\n        ObjectOutputStream oos = null;\n        try {\n            oos = createObjectOutputStream(baos);\n            oos.writeObject(value);\n        } catch (Exception e) {\n            throw new ActivitiException(\"Couldn't serialize value '\" + value + \"' in variable '\" + valueFields.getName() + \"'\", e);\n        } finally {\n            IoUtil.closeSilently(oos);\n        }\n        return baos.toByteArray();\n    }\n\n    public Object deserialize(byte[] bytes, ValueFields valueFields) {\n        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);\n        try {\n            ObjectInputStream ois = createObjectInputStream(bais);\n            Object deserializedObject = ois.readObject();\n\n            return deserializedObject;\n        } catch (Exception e) {\n            throw new ActivitiException(\"Couldn't deserialize object in variable '\" + valueFields.getName() + \"'\", e);\n        } finally {\n            IoUtil.closeSilently(bais);\n        }","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable5-engine/src/main/java/org/activiti/engine/impl/variable/SerializableType.java#L87-L123","documentation":"Thrown by SerializableType.serialize when Java serialization of a variable value fails inside ObjectOutputStream. The original exception (NotSerializableException, IOException, etc.) is attached as the cause.","triggerScenarios":"Setting a process variable whose type falls back to the 'serializable' variable type but whose class does not implement Serializable, or whose object graph contains non-serializable members; serialization infrastructural IO failures.","commonSituations":"Storing Spring beans, EntityManager references, or streams in variables; a referenced class changed and is no longer Serializable; third-party object graphs containing non-serializable fields.","solutions":["Make the value's class (and all reachable fields) implement java.io.Serializable.","Mark non-serializable fields transient or restructure the object graph.","Store only a small DTO/identifier in the variable instead of a large live object.","Inspect the cause chain (NotSerializableException names the offending class) and fix that class.","Declare the variable with a custom VariableType (e.g. JSON type) instead of Java serialization."],"exampleFix":"// before\npublic class OrderData {\n    private transient DataSource ds; // fine\n    private Helper helper; // Helper not Serializable -> throws\n}\n// after\npublic class OrderData implements Serializable {\n    private Helper helper; // Helper now implements Serializable\n}","handlingStrategy":"validation","validationCode":"if (!(value instanceof java.io.Serializable)) throw new IllegalArgumentException(\"Variable value of type \" + value.getClass() + \" is not Serializable\");\nnew java.io.ObjectOutputStream(new java.io.ByteArrayOutputStream()).close(); // warm-up not required; rely on instanceof check","typeGuard":"boolean isSerializable(Object v) { return v instanceof java.io.Serializable; }","tryCatchPattern":"try { runtimeService.setVariable(id, \"order\", order); } catch (ActivitiException e) { log.error(\"Failed to serialize variable 'order': {}\", e.getCause(), e); }","preventionTips":["Implement Serializable on all variable DTOs and their nested types","Mark non-serializable fields transient","Keep variables small and value-like, not live service objects","Prefer JSON/custom variable types for long-lived data"],"tags":["serialization","java","variables"],"backgroundTag":"json-serialization-failed","analyzedSha":"d6d39ce1c69ff244f2d9dc6af756a9b95e865586","analyzedAt":"2026-09-11T06:41:19.413Z","contentChangedAt":"2026-09-11T06:41:19.413Z","schemaVersion":2},"datasetVersion":"2026-09-18T11:17:12.947Z"}