{"record":{"id":"722d68fa06623ad3","repo":"hibernate/hibernate-orm","slug":"ioexception-occurred-reading-text","errorCode":null,"errorMessage":"IOException occurred reading text","messagePattern":"IOException occurred reading text","errorType":"exception","errorClass":"HibernateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DataHelper.java","lineNumber":73,"sourceCode":"\t *\n\t * @return The content as string\n\t */\n\tpublic static String extractString(Reader reader, int lengthHint) {\n\t\t// read the Reader contents into a buffer and return the complete string\n\t\tfinal int bufferSize = getSuggestedBufferSize( lengthHint );\n\t\tfinal var stringBuilder = new StringBuilder( bufferSize );\n\t\ttry {\n\t\t\tfinal char[] buffer = new char[bufferSize];\n\t\t\twhile (true) {\n\t\t\t\tint amountRead = reader.read( buffer, 0, bufferSize );\n\t\t\t\tif ( amountRead == -1 ) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tstringBuilder.append( buffer, 0, amountRead );\n\t\t\t}\n\t\t}\n\t\tcatch ( IOException ioe ) {\n\t\t\tthrow new HibernateException( \"IOException occurred reading text\", ioe );\n\t\t}\n\t\tfinally {\n\t\t\ttry {\n\t\t\t\treader.close();\n\t\t\t}\n\t\t\tcatch (IOException e) {\n\t\t\t\tCORE_LOGGER.unableToCloseStream( e );\n\t\t\t}\n\t\t}\n\t\treturn stringBuilder.toString();\n\t}\n\n\t/**\n\t * Extracts a portion of the contents of the given reader/stream as a string.\n\t *\n\t * @param characterStream The reader for the content\n\t * @param start The start position/offset (0-based, per general stream/reader contracts).\n\t * @param length The amount to extract","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DataHelper.java#L55-L91","documentation":"DataHelper.extractString(Reader) slurps a whole java.io.Reader (typically the character stream of a CLOB or long text column) into a String; any IOException from reader.read is rethrown as HibernateException('IOException occurred reading text'). The reader is closed in a finally block and close failures are only logged via CORE_LOGGER.unableToCloseStream, so the exception you see is always the read failure itself.","triggerScenarios":"Materializing a @Lob String / Clob / materialized_clob attribute when the stream breaks mid-read: the database connection was dropped or timed out, the LOB was freed by the driver, or the stream was already closed because the Session or transaction ended before the read.","commonSituations":"Touching a lazy LOB getter after session.close() or commit (classic lazy-LOB trap); long CLOB reads hitting socket timeouts; DB failover or network resets during large reads.","solutions":["Read LOB properties while the Session and transaction are still open — force initialization before commit","If lazy LOBs are intended, configure bytecode enhancement correctly, or drop lazy loading on large text columns","Catch HibernateException and inspect getCause() for the IOException type (SocketTimeoutException vs StreamClosed vs SocketException) and fix the underlying transport/timeout issue","For very large values, stream to disk or object storage instead of materializing a String"],"exampleFix":"// before\ntx.commit(); session.close();\nString body = entity.getBody(); // lazy @Lob read after close -> IOException\n\n// after\nString body = entity.getBody(); // materialize inside the transaction\ntx.commit(); session.close();","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    String body = entity.getBody(); // lazy @Lob materialization\n} catch (HibernateException e) {\n    if (e.getCause() instanceof IOException ioe) {\n        // transport or stream-lifetime problem: reopen session, retry once\n    }\n}","preventionTips":["Never access @Lob getters after the owning session closes","Keep LOB reads inside short, open transactions","Monitor and tune socket timeouts for large LOB tables"],"tags":["hibernate","lob","clob","io","stream"],"backgroundTag":"lob-stream-read-ioexception","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}