baomidou/mybatis-plus · error · RuntimeException
connection cannot be null
Error message
connection cannot be null
What it means
DatabaseMetaDataWrapper's constructor throws RuntimeException("connection cannot be null") when handed a null java.sql.Connection. The wrapper immediately needs connection.getMetaData() and connection.getCatalog(), so a null connection is rejected up front. Note the null check runs inside a try whose catch (SQLException) also wraps it, but the message is preserved.
Source
Thrown at mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/jdbc/DatabaseMetaDataWrapper.java:57
public class DatabaseMetaDataWrapper {
private static final Logger logger = LoggerFactory.getLogger(DatabaseMetaDataWrapper.class);
@Getter
private final Connection connection;
private final DatabaseMetaData databaseMetaData;
//TODO 暂时只支持一种
private final String catalog;
//TODO 暂时只支持一种
private final String schema;
public DatabaseMetaDataWrapper(Connection connection, String schemaName) {
try {
if (null == connection) {
throw new RuntimeException("connection cannot be null");
}
this.connection = connection;
this.databaseMetaData = connection.getMetaData();
this.catalog = connection.getCatalog();
this.schema = schemaName;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void closeConnection() {
Optional.ofNullable(connection).ifPresent((con) -> {
try {
con.close();
} catch (SQLException sqlException) {
logger.error("close connection exception:", sqlException);
}
});View on GitHub (pinned to bf67d90747)
Solutions
- Check the connection for null before building the wrapper and fail with a clear 'could not obtain JDBC connection' error including the URL.
- Fix the underlying connection failure (wrong URL, credentials, network) — a null here almost always means the real error was swallowed earlier.
- In tests, pass a real H2 connection or a properly stubbed Connection whose getMetaData() returns a mock.
Example fix
// before new DatabaseMetaDataWrapper(conn, schema); // conn may be null // after Objects.requireNonNull(conn, "JDBC connection must be established before wrapping"); new DatabaseMetaDataWrapper(conn, schema);
Defensive patterns
Strategy: validation
Validate before calling
Connection conn = dataSource.getConnection(); // throws loudly on failure Objects.requireNonNull(conn, "connection acquisition returned null for " + jdbcUrl); new DatabaseMetaDataWrapper(conn, schemaName);
Type guard
boolean hasUsableConnection(Connection c) {
return c != null;
} Prevention
- Never catch-and-continue around DriverManager.getConnection; let it throw so null never propagates.
- Acquire connections from a pool (DataSource) which guarantees non-null or exception.
- In tests, assert the connection is non-null before wrapping metadata helpers.
When it happens
Trigger: Constructing DatabaseMetaDataWrapper(null, schemaName), or passing a DataSource connection that already failed to open (e.g. DriverManager.getConnection returned null after a silently-swalled earlier error, or a connection factory returned null on auth failure).
Common situations: Custom generator code or integrations that wrap DatabaseMetaDataWrapper around a connection obtained lazily; the connection acquisition threw earlier and was caught, then null was forwarded; unit tests passing a mocked/null connection.
Related errors
- 获取自定义字段错误:
- 读取[%s]字段出错!
- Failed getting a databaseId
- %s already contains value for %s
- %s does not contain value for %s
AI-assisted analysis of baomidou/mybatis-plus@bf67d90747 (2026-08-14).
Data as JSON: /api/errors/77b6fb18d6009185.
Report an issue: GitHub.