{"record":{"id":"ccbffeb7736440c9","repo":"hibernate/hibernate-orm","slug":"logical-connection-is-closed","errorCode":null,"errorMessage":"Logical connection is closed","messagePattern":"Logical connection is closed","errorType":"exception","errorClass":"ResourceClosedException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/resource/jdbc/internal/LogicalConnectionManagedImpl.java","lineNumber":181,"sourceCode":"\t}\n\n\t@Override\n\tpublic void afterTransaction() {\n\t\tsuper.afterTransaction();\n\t\tif ( resolvedConnectionReleaseMode() != ON_CLOSE ) {\n\t\t\t// NOTE: we check for !ON_CLOSE here (rather than AFTER_TRANSACTION) to also catch:\n\t\t\t// - AFTER_STATEMENT cases that were circumvented due to held resources\n\t\t\t// - BEFORE_TRANSACTION_COMPLETION cases that were circumvented because a rollback occurred\n\t\t\t//   (we don't get a beforeTransactionCompletion event on rollback).\n\t\t\tCONNECTION_LOGGER.initiatingConnectionReleaseAfterTransaction( hashCode() );\n\t\t\treleaseConnectionIfNeeded();\n\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 {","sourceCodeStart":163,"sourceCodeEnd":199,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/resource/jdbc/internal/LogicalConnectionManagedImpl.java#L163-L199","documentation":"ResourceClosedException from LogicalConnectionManagedImpl.manualDisconnect(): session.disconnect() was invoked after the logical connection was already closed. It is a lifecycle usage error — you cannot disconnect an already-closed session; the disconnect operation itself is what failed, no JDBC work was attempted.","triggerScenarios":"session.disconnect() after session.close(); unconditional disconnect() in a finally block that also (or earlier elsewhere) closed the session; double disconnect in layered cleanup code.","commonSituations":"Old long-conversation disconnect/reconnect recipes (Hibernate 2/3 era) combined with container- or Spring-managed session closing; finally blocks calling disconnect() regardless of prior failure paths that already closed the session.","solutions":["Guard the call: if (session.isOpen()) session.disconnect();","Drop manual disconnect/reconnect entirely and let hibernate.connection.handling_mode manage acquisition/release","Ensure close() happens exactly once, in the single place that owns the session lifecycle"],"exampleFix":"// before\nfinally { session.disconnect(); } // throws ResourceClosedException if already closed\n\n// after\nfinally {\n  if (session.isOpen()) session.disconnect();\n  if (session.isOpen()) session.close();\n}","handlingStrategy":"validation","validationCode":"if (session.isOpen()) {\n  session.disconnect();\n}","typeGuard":"static boolean isUsable(org.hibernate.Session s) {\n  return s != null && s.isOpen();\n}","tryCatchPattern":"catch (org.hibernate.ResourceClosedException e) {\n  // in cleanup paths this is harmless: the session is already closed; log at debug and continue\n}","preventionTips":["Own the session lifecycle in one place — a single component closes it","Check isOpen() before any lifecycle operation in finally blocks","Prefer container/Spring-managed sessions over manual disconnect/reconnect patterns"],"tags":["hibernate","session","lifecycle","resource-closed"],"backgroundTag":"session-already-closed","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}