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

  1. Check the connection for null before building the wrapper and fail with a clear 'could not obtain JDBC connection' error including the URL.
  2. Fix the underlying connection failure (wrong URL, credentials, network) — a null here almost always means the real error was swallowed earlier.
  3. 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

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


AI-assisted analysis of baomidou/mybatis-plus@bf67d90747 (2026-08-14). Data as JSON: /api/errors/77b6fb18d6009185. Report an issue: GitHub.