{"record":{"id":"0eecb27ef9279f47","repo":"MyCATApache/Mycat-Server","slug":"can-t-find-existing-connection-maybe-fininshed-e","errorCode":null,"errorMessage":"can't find existing connection,maybe fininshed ${exitsCon}","messagePattern":"can't find existing connection,maybe fininshed (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/backend/datasource/PhysicalDBNode.java","lineNumber":72,"sourceCode":"\t}\n\n\tpublic String getDatabase() {\n\t\treturn database;\n\t}\n\n\t/**\n\t * get connection from the same datasource\n\t * \n\t * @param exitsCon\n\t * @throws Exception\n\t */\n\tpublic void getConnectionFromSameSource(String schema,boolean autocommit,\n\t\t\tBackendConnection exitsCon, ResponseHandler handler,\n\t\t\tObject attachment) throws Exception {\n\n\t\tPhysicalDatasource ds = this.dbPool.findDatasouce(exitsCon);\n\t\tif (ds == null) {\n\t\t\tthrow new RuntimeException(\n\t\t\t\t\t\"can't find existing connection,maybe fininshed \" + exitsCon);\n\t\t} else {\n\t\t\tds.getConnection(schema,autocommit, handler, attachment);\n\t\t}\n\n\t}\n\n\tprivate void checkRequest(String schema){\n\t\tif (schema != null\n\t\t\t\t&& !schema.equals(this.database)) {\n\t\t\tthrow new RuntimeException(\n\t\t\t\t\t\"invalid param ,connection request db is :\"\n\t\t\t\t\t\t\t+ schema + \" and datanode db is \"\n\t\t\t\t\t\t\t+ this.database);\n\t\t}\n\t\tif (!dbPool.isInitSuccess()) {\n\t\t\tdbPool.init(dbPool.activedIndex);\n\t\t}","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/backend/datasource/PhysicalDBNode.java#L54-L90","documentation":"PhysicalDBNode.getConnectionFromSameSource asks dbPool.findDatasouce(exitsCon) which physical datasource pool owns an existing backend connection; if it returns null Mycat throws this RuntimeException. It means the connection's datasource can no longer be located — typically because that datasource (host) was removed from the pool or the pool was reconfigured while the connection was in use.","triggerScenarios":"Calling getConnectionFromSameSource (via kill) with a BackendConnection whose host's datasource is no longer in the dbPool's dataSources — e.g. the datasource was removed/switched during a switch-type failover, or the pool was restarted/reinitialized before findDatasouce ran.","commonSituations":"Admin issues a kill command while a dataHost switch or reload of mycat config has just replaced the read/write hosts; stale backend connections from the old datasource are passed to getConnectionFromSameSource and the new pool no longer contains the matching datasource.","solutions":["Verify the connection's datasource still exists in dbPool before calling getConnectionFromSameSource (guard findDatasouce result).","Re-check the dataHost switch/heartbeat configuration so datasources aren't removed mid-flight; avoid reloads while kill/ha operations run.","Catch RuntimeException around getConnectionFromSameSource and clean up (close the stale connection) instead of propagating.","Upgrade Mycat version — newer releases fixed races between pool switching and connection lookup."],"exampleFix":"// before\nPhysicalDatasource ds = this.dbPool.findDatasouce(exitsCon);\nif (ds == null) {\n    throw new RuntimeException(\"can't find existing connection,maybe fininshed \" + exitsCon);\n}\n// after\nPhysicalDatasource ds = this.dbPool.findDatasouce(exitsCon);\nif (ds == null) {\n    LOGGER.warn(\"stale backend connection, closing: \" + exitsCon);\n    try { exitsCon.close(\"stale datasource\"); } catch (Exception ignore) {}\n    ds = this.dbPool.findDatasouce(exitsCon);\n    if (ds == null) { throw new RuntimeException(\"can't find existing connection,maybe fininshed \" + exitsCon); }\n}","handlingStrategy":"try-catch","validationCode":"PhysicalDatasource ds = dbPool.findDatasouce(exitsCon);\nif (ds == null) { throw new IllegalStateException(\"connection's datasource no longer in pool\"); }","typeGuard":"boolean hasDatasource(BackendConnection con, PhysicalDBNode node) {\n    return con != null && node != null && node.dbPool.findDatasouce(con) != null;\n}","tryCatchPattern":"try {\n    node.getConnectionFromSameSource(schema, autocommit, con, handler, attachment);\n} catch (RuntimeException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"can't find existing connection\")) {\n        LOGGER.warn(\"stale connection after switch, closing\", e);\n        con.close(\"stale datasource\");\n    } else { throw e; }\n}","preventionTips":["Avoid reloading/switching dataHost config while kill or failover operations are running.","Check pool logs for datasource removal before reusing cached backend connections.","Always close backend connections when their owning datasource is switched out.","Pin Mycat to a version with the pool-switch race fixed."],"tags":["connection-pool","datasource-lookup","failover","runtime-exception"],"backgroundTag":"resource-not-found","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"}