{"record":{"id":"099e26d0289ecc61","repo":"flowable/flowable-engine","slug":"could-not-determine-an-active-context-to-associate","errorCode":null,"errorMessage":"Could not determine an active context to associate the current process instance / task instance with.","messagePattern":"Could not determine an active context to associate the current process instance / task instance with\\.","errorType":"exception","errorClass":"FlowableException","httpStatus":null,"severity":"error","filePath":"modules/flowable-cdi/src/main/java/org/flowable/cdi/impl/context/DefaultContextAssociationManager.java","lineNumber":123,"sourceCode":"    }\r\n\r\n    @Inject\r\n    private BeanManager beanManager;\r\n\r\n    protected Class<? extends ScopedAssociation> getBroadestActiveContext() {\r\n        for (Class<? extends ScopedAssociation> scopeType : getAvailableScopedAssociationClasses()) {\r\n            Annotation scopeAnnotation = scopeType.getAnnotations().length > 0 ? scopeType.getAnnotations()[0] : null;\r\n            if (scopeAnnotation == null || !beanManager.isScope(scopeAnnotation.annotationType())) {\r\n                throw new FlowableException(\"ScopedAssociation must carry exactly one annotation and it must be a @Scope annotation\");\r\n            }\r\n            try {\r\n                beanManager.getContext(scopeAnnotation.annotationType());\r\n                return scopeType;\r\n            } catch (ContextNotActiveException e) {\r\n                LOGGER.trace(\"Context {} not active.\", scopeAnnotation.annotationType());\r\n            }\r\n        }\r\n        throw new FlowableException(\"Could not determine an active context to associate the current process instance / task instance with.\");\r\n    }\r\n\r\n    /**\r\n     * Override to add different / additional contexts.\r\n     *\r\n     * @return a list of {@link Scope}-types, which are used in the given order to resolve the broadest active context (@link #getBroadestActiveContext()})\r\n     */\r\n    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","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable-cdi/src/main/java/org/flowable/cdi/impl/context/DefaultContextAssociationManager.java#L105-L141","documentation":"getBroadestActiveContext() iterates the candidate ScopedAssociation scope types and tries beanManager.getContext(scopeType) on each; if none of the CDI contexts are active (ContextNotActiveException for all), it fails hard with this FlowableException because the CDI integration has nowhere to store the current process instance / task association.","triggerScenarios":"Calling BusinessProcess methods (associateExecution, setExecution, getVariable, startTask, etc.) outside any active CDI context — e.g. from a non-managed thread, a static initializer, a timer, or before the CDI container provides request/conversation/session contexts.","commonSituations":"Invoking business-process APIs from background threads (scheduled jobs, message listeners) without opening a request context; unit tests without a CDI container (Weld/OpenWebBeans) booted; CDI contexts destroyed by an earlier conversation.end() call.","solutions":["Run the code inside an active CDI context (request/conversation/session); in web apps ensure the request goes through the CDI-enabled filter (Weld ContextFilter).","For background work, activate a context programmatically (e.g. Weld's BoundRequest / context activation APIs) before calling BusinessProcess methods.","Use engine services (RuntimeService/TaskService) directly instead of the CDI-scoped association when no CDI context exists."],"exampleFix":"// before\nexecutorService.submit(() -> businessProcess.associateExecution(executionId));\n// after\nexecutorService.submit(() -> runtimeService.setVariable(executionId, \"k\", v)); // no CDI context needed","handlingStrategy":"fallback","validationCode":"boolean cdiActive = javax.enterprise.inject.spi.CDI.current().select(BusinessProcess.class)\n    .get() != null; // plus run inside request/conversation scope","typeGuard":"null","tryCatchPattern":"try { return businessProcess.getVariable(name); } catch (FlowableException e) { return runtimeService.getVariable(executionId, name); }","preventionTips":["Only use BusinessProcess APIs from CDI-managed, context-active code (JSF/JAX-RS request threads)","In background threads, activate a CDI context explicitly or use engine services directly","In tests, boot a CDI container (Weld SE) instead of calling BusinessProcess raw"],"tags":["cdi","context-not-active","scope"],"backgroundTag":"cdi-context-not-active","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"}