MyCATApache/Mycat-Server · error · RuntimeException

can't find existing connection,maybe fininshed

Error message

can't find existing connection,maybe fininshed ${exitsCon}

What it means

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.

Solutions

  1. Verify the connection's datasource still exists in dbPool before calling getConnectionFromSameSource (guard findDatasouce result).
  2. Re-check the dataHost switch/heartbeat configuration so datasources aren't removed mid-flight; avoid reloads while kill/ha operations run.
  3. Catch RuntimeException around getConnectionFromSameSource and clean up (close the stale connection) instead of propagating.
  4. Upgrade Mycat version — newer releases fixed races between pool switching and connection lookup.

Example fix

// before
PhysicalDatasource ds = this.dbPool.findDatasouce(exitsCon);
if (ds == null) {
    throw new RuntimeException("can't find existing connection,maybe fininshed " + exitsCon);
}
// after
PhysicalDatasource ds = this.dbPool.findDatasouce(exitsCon);
if (ds == null) {
    LOGGER.warn("stale backend connection, closing: " + exitsCon);
    try { exitsCon.close("stale datasource"); } catch (Exception ignore) {}
    ds = this.dbPool.findDatasouce(exitsCon);
    if (ds == null) { throw new RuntimeException("can't find existing connection,maybe fininshed " + exitsCon); }
}
Defensive patterns

Strategy: try-catch

Validate before calling

PhysicalDatasource ds = dbPool.findDatasouce(exitsCon);
if (ds == null) { throw new IllegalStateException("connection's datasource no longer in pool"); }

Type guard

boolean hasDatasource(BackendConnection con, PhysicalDBNode node) {
    return con != null && node != null && node.dbPool.findDatasouce(con) != null;
}

Try / catch

try {
    node.getConnectionFromSameSource(schema, autocommit, con, handler, attachment);
} catch (RuntimeException e) {
    if (e.getMessage() != null && e.getMessage().startsWith("can't find existing connection")) {
        LOGGER.warn("stale connection after switch, closing", e);
        con.close("stale datasource");
    } else { throw e; }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of MyCATApache/Mycat-Server@65f8d8beb7 (2026-09-11). Data as JSON: /api/errors/0eecb27ef9279f47. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/io/mycat/backend/datasource/PhysicalDBNode.java:72

	}

	public String getDatabase() {
		return database;
	}

	/**
	 * get connection from the same datasource
	 * 
	 * @param exitsCon
	 * @throws Exception
	 */
	public void getConnectionFromSameSource(String schema,boolean autocommit,
			BackendConnection exitsCon, ResponseHandler handler,
			Object attachment) throws Exception {

		PhysicalDatasource ds = this.dbPool.findDatasouce(exitsCon);
		if (ds == null) {
			throw new RuntimeException(
					"can't find existing connection,maybe fininshed " + exitsCon);
		} else {
			ds.getConnection(schema,autocommit, handler, attachment);
		}

	}

	private void checkRequest(String schema){
		if (schema != null
				&& !schema.equals(this.database)) {
			throw new RuntimeException(
					"invalid param ,connection request db is :"
							+ schema + " and datanode db is "
							+ this.database);
		}
		if (!dbPool.isInitSuccess()) {
			dbPool.init(dbPool.activedIndex);
		}

View on GitHub (pinned to 65f8d8beb7)