{"record":{"id":"0c21130cc10b456d","repo":"hibernate/hibernate-orm","slug":"unable-to-set-clob-string-after-creation","errorCode":null,"errorMessage":"Unable to set CLOB string after creation","messagePattern":"Unable to set CLOB string after creation","errorType":"exception","errorClass":"JDBCException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/BlobAndClobCreator.java","lineNumber":124,"sourceCode":"\t * @return The created NCLOB reference.\n\t */\n\tNClob createNClob() {\n\t\treturn lobCreationContext.fromContext( CREATE_NCLOB_CALLBACK );\n\t}\n\n\t/**\n\t * Create a {@link Clob} object after reading a {@code String}\n\t * from a JDBC {@link ResultSet}.\n\t */\n\t@Override\n\tpublic Clob createClob(String string) {\n\t\ttry {\n\t\t\tfinal Clob clob = createClob();\n\t\t\tclob.setString( 1, string );\n\t\t\treturn clob;\n\t\t}\n\t\tcatch ( SQLException e ) {\n\t\t\tthrow new JDBCException( \"Unable to set CLOB string after creation\", e );\n\t\t}\n\t}\n\n\t/**\n\t * Create a {@link Clob} object after reading an {@link InputStream}\n\t * from a JDBC {@link ResultSet}.\n\t *\n\t * @implNote\n\t * It's very inefficient to use JDBC LOB locator creation to create\n\t * a LOB with the contents of the given stream, since that requires\n\t * reading the whole stream. So instead just wrap the given stream,\n\t * just like what {@link NonContextualLobCreator} does.\n\t */\n\t@Override\n\tpublic Clob createClob(Reader reader, long length) {\n\t\treturn NonContextualLobCreator.INSTANCE.createClob( reader, length );\n\t}\n","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/BlobAndClobCreator.java#L106-L142","documentation":"The CLOB twin of the BLOB error: BlobAndClobCreator.createClob(String) creates an empty java.sql.Clob via Connection.createClob() then fills it with clob.setString(1, string). An SQLException from setString - unsupported operation, freed locator, closed connection - is wrapped as JDBCException('Unable to set CLOB string after creation') with the driver exception as cause.","triggerScenarios":"Contextual LOB creation enabled and the driver's Clob.setString(long, String) fails: driver limitation, LOB already freed, or the underlying connection closed between createClob() and setString().","commonSituations":"Same family as the BLOB failure: restrictive drivers, using the LobCreator outside the session/transaction scope, or very large strings hitting driver limits.","solutions":["Set hibernate.jdbc.lob.non_contextual_creation=true so Hibernate binds the String directly instead of using Connection.createClob()","Upgrade the JDBC driver","Keep LOB creation within the open connection/transaction scope (call session.getLobCreator() per-session, not cached)"],"exampleFix":"# before\nClob clob = session.getLobCreator().createClob(bigString); // driver setString fails\n\n# after - avoid driver LOB creation entirely\n<property name=\"hibernate.jdbc.lob.non_contextual_creation\" value=\"true\"/>\nClob clob = session.getLobCreator().createClob(bigString); // now wraps, no createClob()","handlingStrategy":"fallback","validationCode":"// opt out of contextual LOB creation for drivers with weak Clob support\nif ( driverHasWeakLobSupport ) {\n    settings.put(AvailableSettings.NON_CONTEXTUAL_LOB_CREATION, true);\n}","typeGuard":null,"tryCatchPattern":"try {\n    return lobCreator.createClob(text);\n} catch (JDBCException e) {\n    return new SerialClob(text.toCharArray()); // driver-independent fallback\n}","preventionTips":["Test LOB creation once per driver in an integration smoke test","Do not cache LobCreator instances across transactions/connections"],"tags":["hibernate","jdbc","clob","lob","lob-creator"],"backgroundTag":"lob-creation-failed","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}