{"record":{"id":"8a17f0863062bc9e","repo":"alibaba/COLA","slug":"the-state-machine-with-id-machineid-is-alread","errorCode":null,"errorMessage":"The state machine with id [${machineId}] is already built, no need to build again","messagePattern":"The state machine with id \\[(.+?)\\] is already built, no need to build again","errorType":"exception","errorClass":"StateMachineException","httpStatus":null,"severity":"error","filePath":"cola-components/cola-component-statemachine/src/main/java/com/alibaba/cola/statemachine/StateMachineFactory.java","lineNumber":20,"sourceCode":"\nimport com.alibaba.cola.statemachine.impl.StateMachineException;\n\nimport java.util.Map;\nimport java.util.concurrent.ConcurrentHashMap;\n\n/**\n * StateMachineFactory\n *\n * @author Frank Zhang\n * @date 2020-02-08 10:21 PM\n */\npublic class StateMachineFactory {\n    static Map<String /* machineId */, StateMachine> stateMachineMap = new ConcurrentHashMap<>();\n\n    public static <S, E, C> void register(StateMachine<S, E, C> stateMachine){\n        String machineId = stateMachine.getMachineId();\n        if(stateMachineMap.get(machineId) != null){\n            throw new StateMachineException(\"The state machine with id [\"+machineId+\"] is already built, no need to build again\");\n        }\n        stateMachineMap.put(stateMachine.getMachineId(), stateMachine);\n    }\n\n    public static <S, E, C> StateMachine<S, E, C> get(String machineId){\n        StateMachine stateMachine = stateMachineMap.get(machineId);\n        if(stateMachine == null){\n            throw new StateMachineException(\"There is no stateMachine instance for \"+machineId+\", please build it first\");\n        }\n        return stateMachine;\n    }\n}\n","sourceCodeStart":2,"sourceCodeEnd":33,"githubUrl":"https://github.com/alibaba/COLA/blob/352e1a867538d9cd40e1b11e4028fa652fc97557/cola-components/cola-component-statemachine/src/main/java/com/alibaba/cola/statemachine/StateMachineFactory.java#L2-L33","documentation":"StateMachineFactory keeps a static registry of state machines keyed by machineId. register() throws this StateMachineException when a machine with the same id is already present in the map, to enforce one machine per id. It fires when the same machineId is built and registered twice.","triggerScenarios":"Calling StateMachineBuilder.builder(...) ... build(machineId) (or StateMachineFactory.register) twice with the same machineId, e.g. re-running initialization code on app restart within the same JVM or duplicate Spring bean creation.","commonSituations":"Hot-reload or repeated initialization of state machines in tests (multiple @BeforeAll building the same id); duplicate bean definitions; classloader re-loading in app servers.","solutions":["Use StateMachineFactory.get(machineId) first and only build/register when it returns null","Use unique machineIds (e.g. append a timestamp/UUID) if multiple instances are intentional","Guard initialization with a singleton/once flag so build code runs once per JVM","In tests, build in a static initializer or check the factory cache before rebuilding"],"exampleFix":"// before\nStateMachine<...> sm = builder.build(\"orderSM\"); // second call throws\n\n// after\nStateMachine<...> sm = StateMachineFactory.get(\"orderSM\");\nif (sm == null) {\n    sm = builder.build(\"orderSM\");\n}","handlingStrategy":"validation","validationCode":"if (StateMachineFactory.get(machineId) != null) {\n    throw new IllegalStateException(\"machineId already registered: \" + machineId);\n}","typeGuard":null,"tryCatchPattern":"try {\n    StateMachineFactory.register(sm);\n} catch (StateMachineException e) {\n    if (e.getMessage().contains(\"already built\")) {\n        sm = StateMachineFactory.get(machineId); // reuse existing\n    } else {\n        throw e;\n    }\n}","preventionTips":["Check StateMachineFactory.get(machineId) before building","Use a constant or UUID-based machineId scheme","Initialize state machines once (singleton/static init)","In tests, reuse the factory cache across test methods"],"tags":["statemachine","duplicate-registration","factory"],"backgroundTag":"file-already-exists","analyzedSha":"352e1a867538d9cd40e1b11e4028fa652fc97557","analyzedAt":"2026-09-08T00:46:23.972Z","contentChangedAt":"2026-09-08T00:46:23.972Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}