{"record":{"id":"3d12f6f308dcbbe6","repo":"hibernate/hibernate-orm","slug":"cannot-manually-reconnect-unless-connection-was-or","errorCode":null,"errorMessage":"Cannot manually reconnect unless Connection was originally supplied by user","messagePattern":"Cannot manually reconnect unless Connection was originally supplied by user","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/resource/jdbc/internal/LogicalConnectionManagedImpl.java","lineNumber":193,"sourceCode":"\t\t}\n\t}\n\n\t@Override\n\tpublic Connection manualDisconnect() {\n\t\tif ( closed ) {\n\t\t\tthrow new ResourceClosedException( \"Logical connection is closed\" );\n\t\t}\n\t\tfinal var connection = physicalConnection;\n\t\treleaseConnectionIfNeeded();\n\t\treturn connection;\n\t}\n\n\t@Override\n\tpublic void manualReconnect(@Nonnull Connection suppliedConnection) {\n\t\tif ( closed ) {\n\t\t\tthrow new ResourceClosedException( \"Logical connection is closed\" );\n\t\t}\n\t\tthrow new IllegalStateException( \"Cannot manually reconnect unless Connection was originally supplied by user\" );\n\t}\n\n\tprivate Connection acquire() {\n\t\tfinal var eventHandler = getJdbcSessionContext().getEventHandler();\n\t\teventHandler.jdbcConnectionAcquisitionStart();\n\t\ttry {\n\t\t\treturn getJdbcConnectionAccess().obtainConnection();\n\t\t}\n\t\tcatch ( SQLException e ) {\n\t\t\tthrow getExceptionHelper().convert( e, \"Unable to acquire JDBC Connection\" );\n\t\t}\n\t\tfinally {\n\t\t\teventHandler.jdbcConnectionAcquisitionEnd( physicalConnection );\n\t\t}\n\t}\n\n\tprivate void release(Connection connection) {\n\t\tfinal var eventHandler = getJdbcSessionContext().getEventHandler();","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/resource/jdbc/internal/LogicalConnectionManagedImpl.java#L175-L211","documentation":"IllegalStateException from LogicalConnectionManagedImpl.manualReconnect(): manual reconnection with a user-supplied Connection is only supported when the session was originally opened WITH a user-supplied connection (LogicalConnectionProvidedImpl). This session uses a Hibernate/pool-managed connection, so manual reconnect is structurally unsupported and always throws.","triggerScenarios":"Calling session.reconnect(connection) on any session opened normally — sessionFactory.openSession(), Spring/CDI-managed sessions, @PersistenceContext-injected managers — i.e. anything not opened via sessionFactory.openSession(userConnection).","commonSituations":"Disconnect/reconnect long-conversation code ported from very old Hibernate documentation applied to modern pooled sessions; attempts to hot-swap the JDBC connection under a live session (routing, testing) using reconnect.","solutions":["Remove the reconnect call and rely on Hibernate connection handling modes (hibernate.connection.handling_mode) for acquisition/release","If you truly need manual connection control, open the session with your own connection: sessionFactory.openSession(myConnection)","For per-tenant/dynamic connections use Hibernate multi-tenancy or a routing ConnectionProvider, not manual reconnect"],"exampleFix":"// before\nSession s = sessionFactory.openSession(); // pooled/managed connection\n...\ns.reconnect(myConnection); // IllegalStateException\n\n// after\n// choose the pattern once, at open time:\nSession s = manualControlNeeded\n    ? sessionFactory.openSession(myConnection)  // user-supplied: disconnect/reconnect allowed\n    : sessionFactory.openSession();             // pooled: let Hibernate manage the connection","handlingStrategy":"validation","validationCode":"// decide the pattern at open time; never call reconnect() on pooled sessions\ntry (Session s = manualConnectionControlNeeded\n        ? sessionFactory.openSession(userConnection)\n        : sessionFactory.openSession()) {\n  // ...\n}","typeGuard":null,"tryCatchPattern":"catch (IllegalStateException e) {\n  // manual reconnect is unsupported for pool-managed sessions: restructure to open a\n  // new session (optionally with a user-supplied connection) instead of reconnecting\n}","preventionTips":["Only sessions opened via openSession(connection) support manual disconnect/reconnect","Default to letting hibernate.connection.handling_mode manage connections","Use Hibernate multi-tenancy or separate sessions per tenant instead of swapping connections"],"tags":["hibernate","session","api-misuse","connection-management"],"backgroundTag":"invalid-session-state","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}