{"record":{"id":"9b6df63e4d220fb1","repo":"hibernate/hibernate-orm","slug":"cannot-serialize-session-while-connected","errorCode":null,"errorMessage":"Cannot serialize Session while connected","messagePattern":"Cannot serialize Session while connected","errorType":"exception","errorClass":"HibernateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/JdbcCoordinatorImpl.java","lineNumber":497,"sourceCode":"\t\treturn owner;\n\t}\n\n\t@Override\n\tpublic JdbcResourceTransaction getResourceLocalTransaction() {\n\t\treturn logicalConnection.getPhysicalJdbcTransaction();\n\t}\n\n\t/**\n\t * JDK serialization hook\n\t *\n\t * @param oos The stream into which to write our state\n\t *\n\t * @throws IOException Trouble accessing the stream\n\t */\n\t@Override\n\tpublic void serialize(ObjectOutputStream oos) throws IOException {\n\t\tif ( !isReadyForSerialization() ) {\n\t\t\tthrow new HibernateException( \"Cannot serialize Session while connected\" );\n\t\t}\n\t\toos.writeBoolean( isUserSuppliedConnection );\n\t\tlogicalConnection.serialize( oos );\n\t}\n\n\t/**\n\t * JDK deserialization hook\n\t *\n\t * @param ois The stream into which to write our state\n\t * @param owner The Jdbc Session owner which owns the JdbcCoordinatorImpl to be deserialized.\n\t *\n\t * @return The deserialized {@code JdbcCoordinatorImpl}\n\t *\n\t * @throws IOException Trouble accessing the stream\n\t * @throws ClassNotFoundException Trouble reading the stream\n\t */\n\tpublic static JdbcCoordinatorImpl deserialize(ObjectInputStream ois, JdbcSessionOwner owner)\n\t\t\tthrows IOException, ClassNotFoundException {","sourceCodeStart":479,"sourceCodeEnd":515,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/JdbcCoordinatorImpl.java#L479-L515","documentation":"JdbcCoordinatorImpl.serialize(ObjectOutputStream) refuses to serialize while isReadyForSerialization() is false — i.e., the coordinator's logical connection is still connected (holding a pooled or user-supplied JDBC connection that cannot be carried through Java serialization). The result is a HibernateException telling you an open, connected Session is being serialized.","triggerScenarios":"Putting an open Session/EntityManager into an HttpSession that gets replicated (Spring Session, container clustering), passivating stateful beans, or any JDK serialization of a connected Session.","commonSituations":"Storing Hibernate Sessions in HTTP session for 'convenience'; servlet containers with session passivation; migrating from open-session-in-view to serialized stateful designs.","solutions":["Never serialize open Sessions: keep Sessions request-scoped and serialize detached entity data (DTOs) instead","If a Session must be serialized, disconnect() it first (reconnect on demand) so the coordinator is ready for serialization","Review what you actually put into the HTTP session; replace the Session with a detached entity graph or DTO"],"exampleFix":"// before: storing an open session in HTTP session\nhttpSession.setAttribute(\"hib\", session);\n\n// after: store detached data only\nsession.flush();\nhttpSession.setAttribute(\"data\", detachOrDto(entity));","handlingStrategy":"validation","validationCode":"// guard before serializing anything that might hold a Session\nif (value instanceof Session s && s.isOpen() && s.isConnected()) {\n    throw new IllegalStateException(\"cannot serialize connected Session; disconnect first\");\n}","typeGuard":"static boolean safeToSerialize(Object o) {\n    return !(o instanceof Session s) || !s.isOpen() || !s.isConnected();\n}","tryCatchPattern":null,"preventionTips":["Keep Sessions request-scoped; never store them in HTTP session","Store DTOs or detached entities instead of sessions","Call session.disconnect() if a session must cross serialization"],"tags":["serialization","session","connection","clustering","hibernate"],"backgroundTag":"session-serialization-failed","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}