hibernate/hibernate-orm · error · JDBCException

Unable to set CLOB string after creation

Error message

Unable to set CLOB string after creation

What it means

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.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/BlobAndClobCreator.java:124

	 * @return The created NCLOB reference.
	 */
	NClob createNClob() {
		return lobCreationContext.fromContext( CREATE_NCLOB_CALLBACK );
	}

	/**
	 * Create a {@link Clob} object after reading a {@code String}
	 * from a JDBC {@link ResultSet}.
	 */
	@Override
	public Clob createClob(String string) {
		try {
			final Clob clob = createClob();
			clob.setString( 1, string );
			return clob;
		}
		catch ( SQLException e ) {
			throw new JDBCException( "Unable to set CLOB string after creation", e );
		}
	}

	/**
	 * Create a {@link Clob} object after reading an {@link InputStream}
	 * from a JDBC {@link ResultSet}.
	 *
	 * @implNote
	 * It's very inefficient to use JDBC LOB locator creation to create
	 * a LOB with the contents of the given stream, since that requires
	 * reading the whole stream. So instead just wrap the given stream,
	 * just like what {@link NonContextualLobCreator} does.
	 */
	@Override
	public Clob createClob(Reader reader, long length) {
		return NonContextualLobCreator.INSTANCE.createClob( reader, length );
	}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Set hibernate.jdbc.lob.non_contextual_creation=true so Hibernate binds the String directly instead of using Connection.createClob()
  2. Upgrade the JDBC driver
  3. Keep LOB creation within the open connection/transaction scope (call session.getLobCreator() per-session, not cached)

Example fix

# before
Clob clob = session.getLobCreator().createClob(bigString); // driver setString fails

# after - avoid driver LOB creation entirely
<property name="hibernate.jdbc.lob.non_contextual_creation" value="true"/>
Clob clob = session.getLobCreator().createClob(bigString); // now wraps, no createClob()
Defensive patterns

Strategy: fallback

Validate before calling

// opt out of contextual LOB creation for drivers with weak Clob support
if ( driverHasWeakLobSupport ) {
    settings.put(AvailableSettings.NON_CONTEXTUAL_LOB_CREATION, true);
}

Try / catch

try {
    return lobCreator.createClob(text);
} catch (JDBCException e) {
    return new SerialClob(text.toCharArray()); // driver-independent fallback
}

Prevention

When it happens

Trigger: 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().

Common situations: Same family as the BLOB failure: restrictive drivers, using the LobCreator outside the session/transaction scope, or very large strings hitting driver limits.

Related errors


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/0c21130cc10b456d. Report an issue: GitHub.