{"record":{"id":"4fbfbde0a145dec8","repo":"MyCATApache/Mycat-Server","slug":"invalid-datasource-activedindex","errorCode":null,"errorMessage":"Invalid DataSource:${activedIndex}","messagePattern":"Invalid DataSource:(.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/backend/datasource/PhysicalDBNode.java","lineNumber":146,"sourceCode":"\t\t\t\t\twriteSource.getConnection(schema, autoCommit,\n\t\t\t\t\t\t\thandler, attachment);\n\t\t\t\t\trrs.setCanRunInReadDB(false);\n\t\t\t\t}\n\t\t\t}else{\t// 没有  /*db_type=master/slave*/ 注解，按照原来的处理方式\n\t\t\t\tLOGGER.debug(\"rrs.getRunOnSlave() \" + rrs.getRunOnSlaveDebugInfo());\t// null\n\t\t\t\tif (rrs.canRunnINReadDB(autoCommit)) {\n\t\t\t\t\tdbPool.getRWBanlanceCon(schema,autoCommit, handler, attachment, this.database);\n\t\t\t\t} else {\n\t\t\t\t\tPhysicalDatasource writeSource =dbPool.getSource();\n\t\t\t\t\t//记录写节点写负载值\n\t\t\t\t\twriteSource.setWriteCount();\n\t\t\t\t\twriteSource.getConnection(schema, autoCommit,\n\t\t\t\t\t\t\thandler, attachment);\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\t} else {\n\t\t\tthrow new IllegalArgumentException(\"Invalid DataSource:\" + dbPool.getActivedIndex());\n\t\t\t}\n\t\t}\n\n//\tpublic void getConnection(String schema,boolean autoCommit, RouteResultsetNode rrs,\n//\t\t\tResponseHandler handler, Object attachment) throws Exception {\n//\t\tcheckRequest(schema);\n//\t\tif (dbPool.isInitSuccess()) {\n//\t\t\tif (rrs.canRunnINReadDB(autoCommit)) {\n//\t\t\t\tdbPool.getRWBanlanceCon(schema,autoCommit, handler, attachment,\n//\t\t\t\t\t\tthis.database);\n//\t\t\t} else {\n//\t\t\t\tdbPool.getSource().getConnection(schema,autoCommit, handler, attachment);\n//\t\t\t}\n//\n//\t\t} else {\n//\t\t\tthrow new IllegalArgumentException(\"Invalid DataSource:\"\n//\t\t\t\t\t+ dbPool.getActivedIndex());\n//\t\t}","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/backend/datasource/PhysicalDBNode.java#L128-L164","documentation":"In PhysicalDBNode.getConnection, when the dataHost's balance/switch configuration yields no recognized branch (neither the write host nor a matching read-source selection), Mycat falls into the else branch and throws IllegalArgumentException(\"Invalid DataSource:\" + activedIndex). It signals that the pool's active datasource index does not correspond to any configured source — the active index is out of sync with the actual dataSources array.","triggerScenarios":"Calling getConnection (from execute or heartbeat run) when dbPool.getActivedIndex() points at a source that is not among the pool's configured datasources — typically after a failed switch/heartbeat advanced activedIndex beyond the sources array, or with an empty/misconfigured dataHost.","commonSituations":"schema.xml dataHost with missing or invalid writeHost entries; a switchType failover set activedIndex to an index with no corresponding host; a config reload leaving the pool's active index stale.","solutions":["Check schema.xml dataHost writeHost/readHost configuration and fix missing or invalid host entries.","Restart/reload Mycat so the DBPool's activedIndex is re-initialized to a valid source.","Guard with a bounds check on activedIndex before dispatching, and fall back to the write host.","Enable heartbeat logs to see why the pool switched to an invalid index; adjust switchType/heartbeat settings."],"exampleFix":"// before\nthrow new IllegalArgumentException(\"Invalid DataSource:\" + dbPool.getActivedIndex());\n// after\nint idx = dbPool.getActivedIndex();\nPhysicalDatasource[] sources = dbPool.getSources();\nif (idx < 0 || idx >= sources.length || sources[idx] == null) {\n    LOGGER.warn(\"invalid activedIndex \" + idx + \", falling back to write source\");\n    dbPool.getSource().getConnection(schema, autoCommit, handler, attachment);\n    return;\n}\nthrow new IllegalArgumentException(\"Invalid DataSource:\" + idx);","handlingStrategy":"fallback","validationCode":"int idx = dbPool.getActivedIndex();\nif (idx < 0 || idx >= dbPool.getSources().length || dbPool.getSources()[idx] == null) {\n    throw new IllegalStateException(\"activedIndex \" + idx + \" is not a configured datasource\");\n}","typeGuard":"boolean hasValidActiveSource(PhysicalDBPool pool) {\n    int i = pool.getActivedIndex();\n    return i >= 0 && i < pool.getSources().length && pool.getSources()[i] != null;\n}","tryCatchPattern":"try {\n    node.getConnection(schema, autoCommit, handler, attachment);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"Invalid DataSource\")) {\n        LOGGER.warn(\"invalid active index, retrying on write host\");\n        node.getDbPool().getSource().getConnection(schema, autoCommit, handler, attachment);\n    } else { throw e; }\n}","preventionTips":["Ensure every dataHost has at least one valid writeHost in schema.xml.","Prefer switchType settings you have tested; monitor heartbeat/switch logs.","Reload or restart Mycat after manual edits to dataHost entries so activedIndex is recomputed.","Alert on 'Invalid DataSource' occurrences — they indicate config/failover desync."],"tags":["connection-pool","failover","config","illegal-argument"],"backgroundTag":"invalid-config-value","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"}