{"record":{"id":"97ada15ecfe97991","repo":"hibernate/hibernate-orm","slug":"can-t-deal-with-clobs-larger-than-integer-max-val","errorCode":null,"errorMessage":"Can't deal with Clobs larger than 'Integer.MAX_VALUE'","messagePattern":"Can't deal with Clobs larger than 'Integer\\.MAX_VALUE'","errorType":"exception","errorClass":"SQLException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/engine/jdbc/proxy/ClobProxy.java","lineNumber":148,"sourceCode":"\t\t}\n\t\tif ( length < 0 ) {\n\t\t\tthrow new SQLException( \"Length must be great-than-or-equal to zero.\" );\n\t\t}\n\t\tfinal String string = characterStream.asString();\n\t\tfinal long endIndex = Math.min( start + length - 1, string.length() );\n\t\treturn string.substring( (int) start - 1, (int) endIndex );\n\t}\n\n\t@Override\n\tpublic Reader getCharacterStream(long start, long length) throws SQLException {\n\t\tif ( start < 1 ) {\n\t\t\tthrow new SQLException( \"Start position 1-based; must be 1 or more.\" );\n\t\t}\n\t\tif ( start > length() + 1 ) {\n\t\t\tthrow new SQLException( \"Start position [\" + start + \"] cannot exceed overall CLOB length [\" + length() + \"]\" );\n\t\t}\n\t\tif ( length > Integer.MAX_VALUE ) {\n\t\t\tthrow new SQLException( \"Can't deal with Clobs larger than 'Integer.MAX_VALUE'\" );\n\t\t}\n\t\tif ( length < 0 ) {\n\t\t\t// javadoc for getCharacterStream(long,int) specifies that the start+length must not exceed the\n\t\t\t// total length (this is at odds with the behavior of getSubString(long,int))\n\t\t\tthrow new SQLException( \"Length must be greater than or equal to zero\" );\n\t\t}\n\t\treturn DataHelper.subStream( getCharacterStream(), start-1, (int) length );\n\t}\n\n\t@Override\n\tpublic void free() throws SQLException {\n\t\tcharacterStream.release();\n\t}\n\n\tprotected void resetIfNeeded() {\n\t\ttry {\n\t\t\tif ( needsReset ) {\n\t\t\t\tcharacterStream.asReader().reset();","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/engine/jdbc/proxy/ClobProxy.java#L130-L166","documentation":"ClobProxy.getCharacterStream(long start, long length) can only window a region that fits an int, because it casts length to int for DataHelper.subStream. A length above Integer.MAX_VALUE characters is rejected with this SQLException instead of being truncated.","triggerScenarios":"Calling clob.getCharacterStream(1, clob.length()) on a Clob longer than Integer.MAX_VALUE characters; passing Long.MAX_VALUE as an 'until the end' marker; forwarding a long limit from a framework streaming API.","commonSituations":"Very large text stores (document archives, logs) held as CLOBs; 'read all' helpers that use a huge sentinel; migration from drivers that silently clamp to Hibernate's fail-fast proxy.","solutions":["Read in chunks with length capped well below Integer.MAX_VALUE (e.g. 64K)","Use getCharacterStream() (no args) and stream-copy for whole-content reads","Replace sentinel sizes with the actual remaining character count"],"exampleFix":"// before\nReader all = clob.getCharacterStream(1, Long.MAX_VALUE); // throws\n\n// after\nReader all = clob.getCharacterStream(); // full reader, copy incrementally","handlingStrategy":"validation","validationCode":"static final long MAX_CHARS = 64L * 1024;        // 64K characters per window\nstatic long capLength(long requested, long remaining) {\n    if (requested < 0) throw new IllegalArgumentException(\"length must be >= 0\");\n    return Math.min(Math.min(requested, remaining), MAX_CHARS);\n}\n// usage: clob.getCharacterStream(start, capLength(len, clob.length() - start + 1))","typeGuard":null,"tryCatchPattern":"try {\n    r = clob.getCharacterStream(start, length);\n} catch (SQLException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"Integer.MAX_VALUE\")) {\n        r = clob.getCharacterStream();              // fall back to the full reader\n    } else {\n        throw e;\n    }\n}","preventionTips":["Cap positioned character windows at a few tens of KB and loop","Use the no-arg getCharacterStream() for whole-content streaming","Never forward 'unlimited' sentinels like Long.MAX_VALUE into LOB APIs"],"tags":["hibernate","jdbc","clob","lob","integer-overflow","large-objects","sql-exception"],"backgroundTag":"jdbc-lob-exceeds-max-int","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}