{"record":{"id":"a1a4e4329aca4237","repo":"hibernate/hibernate-orm","slug":"cannot-reconnect-using-a-null-connection","errorCode":null,"errorMessage":"cannot reconnect using a null connection","messagePattern":"cannot reconnect using a null connection","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/resource/jdbc/internal/LogicalConnectionProvidedImpl.java","lineNumber":118,"sourceCode":"\t}\n\n\t@Override\n\tpublic Connection manualDisconnect() {\n\t\terrorIfClosed();\n\t\ttry {\n\t\t\tresourceRegistry.releaseResources();\n\t\t\treturn providedConnection;\n\t\t}\n\t\tfinally {\n\t\t\tprovidedConnection = null;\n\t\t}\n\t}\n\n\t@Override\n\tpublic void manualReconnect(@Nonnull Connection connection) {\n\t\terrorIfClosed();\n\t\tif ( connection == null ) {\n\t\t\tthrow new IllegalArgumentException( \"cannot reconnect using a null connection\" );\n\t\t}\n\t\telse if ( connection == providedConnection ) {\n\t\t\t// likely an unmatched reconnect call (no matching disconnect call)\n\t\t\tCONNECTION_LOGGER.reconnectingSameConnectionAlreadyConnected();\n\t\t}\n\t\telse if ( providedConnection != null ) {\n\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\t\"Cannot reconnect to a new user-supplied connection because currently connected; must disconnect before reconnecting.\"\n\t\t\t);\n\t\t}\n\t\tprovidedConnection = connection;\n\t\tCONNECTION_LOGGER.manuallyReconnectedLogicalConnection();\n\t}\n\n\t@Override\n\tprotected Connection getConnectionForTransactionManagement() {\n\t\treturn providedConnection;\n\t}","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/resource/jdbc/internal/LogicalConnectionProvidedImpl.java#L100-L136","documentation":"IllegalArgumentException from LogicalConnectionProvidedImpl.manualReconnect(): session.reconnect(null) was called. The parameter is declared @Nonnull and this is a defensive rejection of a null connection; the transaction state is untouched.","triggerScenarios":"session.reconnect(null), e.g. reconnect(dataSource.getConnection()) where the producer returned null, or wrapper code forwarding a nullable stored connection field.","commonSituations":"Framework/wrapper code holding a nullable connection reference; stubs and test harnesses returning null from getConnection(); race where the connection field is cleared before reconnect runs.","solutions":["Null-check before reconnecting and fail with context (which producer returned null)","Fix the connection producer that returned null","For brand-new sessions use sessionFactory.openSession(connection) instead of reconnect"],"exampleFix":"// before\nsession.reconnect(getConnection()); // may pass null\n\n// after\nConnection c = Objects.requireNonNull(getConnection(), \"connection source returned null\");\nsession.reconnect(c);","handlingStrategy":"validation","validationCode":"Connection c = Objects.requireNonNull(connectionSource.get(), \"connection source returned null\");\nsession.reconnect(c);","typeGuard":null,"tryCatchPattern":"catch (IllegalArgumentException e) {\n  // null reached reconnect(): fix the nullable producer or guard above; nothing stateful happened\n}","preventionTips":["Never store connections in nullable fields that later feed reconnect()","Make test stubs return non-null connections","Centralize connection acquisition in one non-null-returning component"],"tags":["hibernate","session","null-check","illegal-argument"],"backgroundTag":"illegal-null-argument","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}