{"record":{"id":"05f08d1381449807","repo":"hibernate/hibernate-orm","slug":"unable-to-access-clob-stream","errorCode":null,"errorMessage":"Unable to access clob stream","messagePattern":"Unable to access clob stream","errorType":"exception","errorClass":"HibernateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ClobJavaType.java","lineNumber":131,"sourceCode":"\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\t// otherwise we need to build a CharacterStream...\n\t\t\t\t\treturn type.cast( value.getCharacterStream() );\n\t\t\t\t}\n\t\t\t}\n\t\t\telse if ( CharacterStream.class.isAssignableFrom( type ) ) {\n\t\t\t\tif (value instanceof ClobImplementer clobImplementer) {\n\t\t\t\t\t// if the incoming Clob is a wrapper, just pass along its CharacterStream\n\t\t\t\t\treturn type.cast( clobImplementer.getUnderlyingStream() );\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\t// otherwise we need to build a CharacterStream...\n\t\t\t\t\treturn type.cast( new CharacterStreamImpl( value.getCharacterStream(), value.length() ) );\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcatch ( SQLException e ) {\n\t\t\tthrow new HibernateException( \"Unable to access clob stream\", e );\n\t\t}\n\n\t\tthrow unknownUnwrap( type );\n\t}\n\n\tpublic <X> Clob wrap(X value, WrapperOptions options) {\n\t\tif ( value == null ) {\n\t\t\treturn null;\n\t\t}\n\t\telse {\n\t\t\tfinal LobCreator lobCreator = options.getLobCreator();\n\t\t\tif ( value instanceof Clob clob ) {\n\t\t\t\treturn lobCreator.wrap( clob );\n\t\t\t}\n\t\t\telse if ( value instanceof String string ) {\n\t\t\t\treturn lobCreator.createClob( string );\n\t\t\t}\n\t\t\telse if ( value instanceof Reader reader ) {","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ClobJavaType.java#L113-L149","documentation":"ClobJavaType.unwrap wraps any SQLException raised while calling getCharacterStream()/length() during Clob to String/Reader/CharacterStream conversions into HibernateException('Unable to access clob stream'). It is the character-LOB twin of the blob-stream error: the locator is dead or the driver refused the operation.","triggerScenarios":"Unwrapping a raw java.sql.Clob to String/Reader after its transaction or connection ended; converting a detached entity's Clob field; consuming a Clob returned by a stored function after the call completed.","commonSituations":"Post-commit access to Clob attributes; Oracle temporary CLOBs freed at commit; connection-pool recycling during long reads; OSIV disabled in web apps.","solutions":["Convert the Clob to String inside the transaction that loaded it.","Map the attribute as String with @Lob instead of java.sql.Clob.","Re-fetch the entity in a fresh session before any conversion."],"exampleFix":"// before\nClob clob = detachedItem.getText();\nString text = clob.getSubString(1, (int) clob.length()); // after tx -> wrapped SQLException\n\n// after\n@Lob private String text; // materialized by Hibernate during read\nString value = item.getText();","handlingStrategy":"try-catch","validationCode":"try {\n    clob.length(); // probe locator liveness before unwrap\n} catch (SQLException e) {\n    throw new IllegalStateException(\"Clob locator is dead; re-load the entity\", e);\n}","typeGuard":null,"tryCatchPattern":"try {\n    return clobJavaType.unwrap(clob, String.class, options);\n} catch (HibernateException e) {\n    if (e.getCause() instanceof SQLException) {\n        return reloadAndGetText(id); // fresh session inside a live tx\n    }\n    throw e;\n}","preventionTips":["Map @Lob String instead of java.sql.Clob unless streaming is required.","Materialize CLOB text inside the loading transaction.","Do not let entities with Clob handles become detached."],"tags":["hibernate","clob","lob","jdbc","unwrap"],"backgroundTag":"clob-stream-access","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}