{"record":{"id":"77b6fb18d6009185","repo":"baomidou/mybatis-plus","slug":"connection-cannot-be-null","errorCode":null,"errorMessage":"connection cannot be null","messagePattern":"connection cannot be null","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/jdbc/DatabaseMetaDataWrapper.java","lineNumber":57,"sourceCode":"public class DatabaseMetaDataWrapper {\n\n    private static final Logger logger = LoggerFactory.getLogger(DatabaseMetaDataWrapper.class);\n\n    @Getter\n    private final Connection connection;\n\n    private final DatabaseMetaData databaseMetaData;\n\n    //TODO 暂时只支持一种\n    private final String catalog;\n\n    //TODO 暂时只支持一种\n    private final String schema;\n\n    public DatabaseMetaDataWrapper(Connection connection, String schemaName) {\n        try {\n            if (null == connection) {\n                throw new RuntimeException(\"connection cannot be null\");\n            }\n            this.connection = connection;\n            this.databaseMetaData = connection.getMetaData();\n            this.catalog = connection.getCatalog();\n            this.schema = schemaName;\n        } catch (SQLException e) {\n            throw new RuntimeException(e);\n        }\n    }\n\n    public void closeConnection() {\n        Optional.ofNullable(connection).ifPresent((con) -> {\n            try {\n                con.close();\n            } catch (SQLException sqlException) {\n                logger.error(\"close connection exception:\", sqlException);\n            }\n        });","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/baomidou/mybatis-plus/blob/bf67d907478c724120bf76292da54abf9e73c2b3/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/jdbc/DatabaseMetaDataWrapper.java#L39-L75","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before\nnew DatabaseMetaDataWrapper(conn, schema); // conn may be null\n\n// after\nObjects.requireNonNull(conn, \"JDBC connection must be established before wrapping\");\nnew DatabaseMetaDataWrapper(conn, schema);","handlingStrategy":"validation","validationCode":"Connection conn = dataSource.getConnection(); // throws loudly on failure\nObjects.requireNonNull(conn, \"connection acquisition returned null for \" + jdbcUrl);\nnew DatabaseMetaDataWrapper(conn, schemaName);","typeGuard":"boolean hasUsableConnection(Connection c) {\n    return c != null;\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["mybatis-plus","jdbc","null-check","metadata"],"backgroundTag":null,"analyzedSha":"bf67d907478c724120bf76292da54abf9e73c2b3","analyzedAt":"2026-08-14T15:17:09.543Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}