MyCATApache/Mycat-Server · error · SQLException

Unexpected exception

Error message

Unexpected exception: ${e.getMessage()}

What it means

MongoDriver.connect wraps construction of MongoConnection in a try-catch and rethrows any exception as SQLException("Unexpected exception: <message>", e). It signals that the underlying MongoDB connection URI/URI parsing or MongoConnection construction failed; the original cause is preserved as the chained cause.

Solutions

  1. Check the chained cause (e.getCause()) to see the real failure — the wrapper message is generic
  2. Validate the URL is a well-formed mongodb:// URI with host[:port] before configuring the MyCat dataNode
  3. Verify MongoDB server reachability and credentials in the URI
  4. Check parseURL in MongoDriver for the exact URI format this driver expects

Example fix

// before
jdbc:mysql://... (mongodb node configured with url = "mongodb:host1:27017/db")

// after
url = "mongodb://host1:27017/db"
Defensive patterns

Strategy: validation

Validate before calling

if (!url.startsWith("mongodb://")) {
    throw new IllegalArgumentException("MongoDB JDBC url must start with mongodb:// : " + url);
}
// also validate host:port presence
URI u = new URI(url.substring("mongodb:".length()));
if (u.getHost() == null) throw new IllegalArgumentException("missing host in mongodb url");

Try / catch

try {
    Connection c = driver.connect(url, info);
} catch (SQLException e) {
    if (e.getMessage() != null && e.getMessage().startsWith("Unexpected exception")) {
        Throwable cause = e.getCause();
        // log cause; fix url/credentials
    } else {
        throw e;
    }
}

Prevention

When it happens

Trigger: new MongoConnection(mcu, url) throws inside connect() — typically bad MongoClientURI parsing (malformed mongodb:// URL, bad hosts/credentials) or a MongoClient constructor failure.

Common situations: Typo in the JDBC URL (e.g. 'mongodb:host1' instead of 'mongodb://host1'), unparseable host/port or username/password options, missing MongoDB server so the URI parser or client setup throws.

Understand the failure class

Background: 'Something went wrong' / 'Request failed (500)' / 'HTTP error! status: 404' — what failed HTTP requests actually mean and how to find the real cause — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at src/main/java/io/mycat/backend/jdbc/mongodb/MongoDriver.java:49

		}catch (SQLException e){
		    LOGGER.error("initError",e);
		}
	}


	@Override
	public Connection connect(String url, Properties info) throws SQLException {
		MongoClientURI mcu = null;
		if ((mcu = parseURL(url, info)) == null) {
			return null;
		}
		
		MongoConnection result = null;
		//System.out.print(info);
		try{
			result = new MongoConnection(mcu, url);
		}catch (Exception e){
			throw new SQLException("Unexpected exception: " + e.getMessage(), e);
		}
		
		return result;
	}
	
	private MongoClientURI parseURL(String url, Properties defaults) {
		if (url == null) {
			return null;
		}
		
		if (!StringUtils.startsWithIgnoreCase(url, PREFIX)) {	
			return null;
		}
		
		//删掉开头的 jdbc:
		//url = url.replace(URL_JDBC, "");

		//替换user:

View on GitHub (pinned to 65f8d8beb7)