{"record":{"id":"4be6e1de855ec513","repo":"flowable/flowable-engine","slug":"cannot-work-with-scoped-associations-inside-comman","errorCode":null,"errorMessage":"Cannot work with scoped associations inside command context.","messagePattern":"Cannot work with scoped associations inside command context\\.","errorType":"exception","errorClass":"FlowableCdiException","httpStatus":null,"severity":"error","filePath":"modules/flowable-cdi/src/main/java/org/flowable/cdi/impl/context/DefaultContextAssociationManager.java","lineNumber":149,"sourceCode":"    protected List<Class<? extends ScopedAssociation>> getAvailableScopedAssociationClasses() {\r\n        ArrayList<Class<? extends ScopedAssociation>> scopeTypes = new ArrayList<>();\r\n        scopeTypes.add(ConversationScopedAssociation.class);\r\n        scopeTypes.add(RequestScopedAssociation.class);\r\n        return scopeTypes;\r\n    }\r\n\r\n    protected ScopedAssociation getScopedAssociation() {\r\n        return ProgrammaticBeanLookup.lookup(getBroadestActiveContext(), beanManager);\r\n    }\r\n\r\n    @Override\r\n    public void setExecution(Execution execution) {\r\n        if (execution == null) {\r\n            throw new FlowableCdiException(\"Cannot associate with execution: null\");\r\n        }\r\n\r\n        if (Context.getCommandContext() != null) {\r\n            throw new FlowableCdiException(\"Cannot work with scoped associations inside command context.\");\r\n        }\r\n\r\n        ScopedAssociation scopedAssociation = getScopedAssociation();\r\n        Execution associatedExecution = scopedAssociation.getExecution();\r\n        if (associatedExecution != null && !associatedExecution.getId().equals(execution.getId())) {\r\n            throw new FlowableCdiException(\"Cannot associate \" + execution + \", already associated with \" + associatedExecution + \". Disassociate first!\");\r\n        }\r\n\r\n        if (LOGGER.isTraceEnabled()) {\r\n            LOGGER.trace(\"Associating {} (@{})\", execution, scopedAssociation.getClass().getAnnotations()[0].annotationType().getSimpleName());\r\n        }\r\n        scopedAssociation.setExecution(execution);\r\n    }\r\n\r\n    @Override\r\n    public void disAssociate() {\r\n        if (Context.getCommandContext() != null) {\r\n            throw new FlowableCdiException(\"Cannot work with scoped associations inside command context.\");\r","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable-cdi/src/main/java/org/flowable/cdi/impl/context/DefaultContextAssociationManager.java#L131-L167","documentation":"setExecution() refuses to change the scoped association while code is executing inside a Flowable command context (Context.getCommandContext() != null). Scoped associations are CDI-side state manipulated by application code; the engine already manages its own execution context, so touching the association from within a command (e.g. inside a JavaDelegate or event listener) is considered a programming error.","triggerScenarios":"Calling BusinessProcess.associateExecution/setExecution (or methods that internally re-associate) from within a JavaDelegate, TaskListener, ExecutionListener, or other engine callback that runs inside a command.","commonSituations":"Trying to use CDI-injected BusinessProcess inside a service task delegate; using BusinessProcess in an async job executor thread that still has an open command context.","solutions":["Do not call BusinessProcess association APIs from engine callbacks; use Context.getProcessVariables / delegate execution APIs inside delegates.","If CDI integration is needed in delegates, use the flowable-cdi CommandContext handling (BusinessProcess can read variables via Context) but defer association changes to after the command completes.","Move the association logic to application-managed code outside the engine invocation."],"exampleFix":"// before\nclass MyDelegate implements JavaDelegate {\n  public void execute(DelegateExecution ex) { businessProcess.associateExecution(...); }\n}\n// after\nclass MyDelegate implements JavaDelegate {\n  public void execute(DelegateExecution ex) { ex.setVariable(\"k\", v); }\n}","handlingStrategy":"validation","validationCode":"if (org.flowable.engine.impl.context.Context.getCommandContext() != null) {\n    throw new IllegalStateException(\"Do not use BusinessProcess inside a command context\");\n}","typeGuard":"static boolean inCommandContext() { return org.flowable.engine.impl.context.Context.getCommandContext() != null; }","tryCatchPattern":"try { businessProcess.associateExecution(e); } catch (FlowableCdiException e) { /* inside engine callback; use DelegateExecution APIs instead */ }","preventionTips":["Never call BusinessProcess from JavaDelegates/listeners running inside engine commands","Use DelegateExecution variable APIs inside delegates","Restrict BusinessProcess usage to application-managed, CDI-active code"],"tags":["cdi","command-context","scoped-association"],"backgroundTag":"invalid-state-transition","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"}