{"record":{"id":"b3fcb83a6f802f7e","repo":"MyCATApache/Mycat-Server","slug":"can-t-fetch-sequnce-in-db-sequnce-seqname-detai","errorCode":null,"errorMessage":"can't fetch sequnce in db,sequnce :{seqName} detail:{lastestError}","messagePattern":"can't fetch sequnce in db,sequnce :(.+?) detail:(.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"critical","filePath":"src/main/java/io/mycat/route/sequence/handler/IncrSequenceMySQLHandler.java","lineNumber":143,"sourceCode":"\t\t\tLOGGER.debug(\"get next segement of sequence from db for sequnce:\"\n\t\t\t\t\t+ seqVal.seqName + \" curVal \" + seqVal.curVal);\n\t\t}\n\t\t//设置正在获取\n\t\tboolean isLock = seqVal.fetching.compareAndSet(false, true);\n\t\tif(isLock) {\n\t\t\t//判断当前的是否有效。\n\t\t\tif(seqVal.successFetched == true) {\n\t\t\t\tLong nexVal = seqVal.nextValue();\n\t\t\t\tif (seqVal.isNexValValid(nexVal)) {\n\t\t\t\t\tseqVal.fetching.compareAndSet(true, false);\n\t\t\t\t\treturn nexVal;\n\t\t\t\t}\n\t\t\t}\n\t\t\t\t\t\t\n\t\t\t//发起请求sql 等待到返回  或者进行\n\t\t\tLong[] values = seqVal.fetchSequenceFromDB( mysqlSeqFetcher, 1, true); //只有一个线程可以进 并且有重试机制。\n\t\t\tif (values == null) {\n\t\t\t\tthrow new RuntimeException(\"can't fetch sequnce in db,sequnce :\"\n\t\t\t\t\t\t+ seqVal.seqName + \" detail:\"\n\t\t\t\t\t\t+ mysqlSeqFetcher.getLastestError(seqVal.seqName));\n\t\t\t} else {\n\t\t\t\t\tseqVal.setCurValue(values[0]); \n\t\t\t\t\tseqVal.maxSegValue = values[1];\n\t\t\t\t\tseqVal.successFetched = true; //设置successFetched\n\t\t\t\t\treturn values[0];\n\t\t\t\n\t\t\t}\n\t\t} else {\n\t\t\tlong count = 0 ;\n\t\t\t//正在获取 ，或者还未返回\n\t\t\twhile(seqVal.fetching.get() || seqVal.successFetched == false){\n\t\t\t\ttry {\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\tThread.sleep(10);\n\t\t\t\t\tif(++count > 10000L) {\n\t\t\t\t\t\treturn this.getSeqValueFromDB(seqVal);\n\t\t\t\t\t}","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/route/sequence/handler/IncrSequenceMySQLHandler.java#L125-L161","documentation":"IncrSequenceMySQLHandler.getSeqValueFromDB delegates to SequenceVal.fetchSequenceFromDB to load the next sequence segment from MySQL; a single thread enters with retry logic built in. If the fetch still returns null after retries, the handler throws this RuntimeException, appending the sequence name and the last recorded error from the MySQL fetcher. It means MyCat could not obtain a new ID segment from the database and cannot hand out IDs.","triggerScenarios":"Calling nextId (directly or via getNextValidSeqVal or recursion) when fetchSequenceFromDB(mysqlSeqFetcher, 1, true) returns null — i.e. the SELECT on the sequence table repeatedly fails (connection error, missing table, lock timeout, malformed sequence row).","commonSituations":"MySQL backend for sequences down or unreachable from MyCat; MYCAT_SEQUENCE table missing/dropped or row deleted; insufficient DB privileges for the MyCat sequence user; network timeouts under load exhausting the built-in retries.","solutions":["Inspect the detail appended in the message (mysqlSeqFetcher.getLastestError) and fix the root cause — typically connectivity, credentials, or SQL errors against the sequence DB.","Verify the sequence table and the row for seqName exist and are readable by MyCat's configured sequence datasource.","Restore/restart the MySQL backend and check network/firewall between MyCat and the DB, then retry ID generation.","Monitor DB load and tune timeouts/retries so transient lock waits don't exhaust the fetch attempts."],"exampleFix":"// before\nlong id = handler.nextId(\"ORDER_SEQ\"); // RuntimeException: can't fetch sequnce in db\n// after\n-- on the sequence DB: ensure definition exists and is reachable\nSELECT * FROM MYCAT_SEQUENCE WHERE name = 'ORDER_SEQ';\nGRANT SELECT, UPDATE ON mycat.* TO 'mycat_seq_user'@'%';","handlingStrategy":"retry","validationCode":"public static boolean canFetchSequence(DataSource ds, String seqName) {\n    try (Connection c = ds.getConnection(); PreparedStatement ps =\n            c.prepareStatement(\"SELECT current_value, increment FROM MYCAT_SEQUENCE WHERE name = ?\")) {\n        ps.setString(1, seqName);\n        try (ResultSet rs = ps.executeQuery()) { return rs.next(); }\n    } catch (SQLException e) { return false; }\n}","typeGuard":null,"tryCatchPattern":"try {\n    long id = handler.nextId(seqName);\n} catch (RuntimeException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"can't fetch sequnce in db\")) {\n        if (!canFetchSequence(seqDataSource, seqName)) {\n            throw new SequenceBackendUnavailableException(seqName, e); // fail fast, don't hot-loop\n        }\n        return handler.nextId(seqName); // backend healthy again: bounded retry\n    }\n    throw e;\n}","preventionTips":["Monitor the sequence MySQL backend (health checks on connection and the sequence table).","Grant the MyCat sequence user the required SELECT/UPDATE privileges on the sequence table.","Keep the built-in retry budget small and fail fast when the DB is down instead of recursing indefinitely.","Alert on repeated segment-fetch failures so the backend is fixed before ID generation exhausts in-memory segments."],"tags":["sequence","database","id-generation","fetch-failure"],"backgroundTag":"database-query-failed","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}