hibernate/hibernate-orm · error · JDBCException
Unable to set BLOB bytes after creation
Error message
Unable to set BLOB bytes after creation
What it means
BlobAndClobCreator.createBlob(byte[]) (the contextual LOB creator) first creates an empty java.sql.Blob via Connection.createBlob() and then fills it with blob.setBytes(1, bytes). If the driver's Blob implementation rejects setBytes - throws SQLException - Hibernate wraps it as JDBCException('Unable to set BLOB bytes after creation') keeping the cause.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/BlobAndClobCreator.java:75
* @return The created BLOB reference.
*/
Blob createBlob() {
return lobCreationContext.fromContext( CREATE_BLOB_CALLBACK );
}
/**
* Create a {@link Blob} object after reading a {@code byte[]}
* array from a JDBC {@link ResultSet}.
*/
@Override
public Blob createBlob(byte[] bytes) {
final Blob blob = createBlob();
try {
blob.setBytes( 1, bytes );
return blob;
}
catch ( SQLException e ) {
throw new JDBCException( "Unable to set BLOB bytes after creation", e );
}
}
/**
* Create a {@link Blob} 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 Blob createBlob(InputStream stream, long length) {
return NonContextualLobCreator.INSTANCE.createBlob( stream, length );
}
View on GitHub (pinned to fad1729dce)
Solutions
- Disable contextual LOB creation: hibernate.jdbc.lob.non_contextual_creation=true - Hibernate then wraps the byte[] directly instead of using Connection.createBlob()
- Upgrade the JDBC driver to one whose createBlob()/setBytes() works
- Ensure the LobCreator is used while its connection is open (bind LOB creation to the transaction/session lifetime)
Example fix
# before # contextual LOB creation active, driver setBytes fails # after <property name="hibernate.jdbc.lob.non_contextual_creation" value="true"/>
Defensive patterns
Strategy: fallback
Validate before calling
// opt out of contextual LOB creation for drivers with weak Blob support
if ( driverHasWeakLobSupport ) {
settings.put(AvailableSettings.NON_CONTEXTUAL_LOB_CREATION, true);
} Try / catch
try {
return lobCreator.createBlob(bytes);
} catch (JDBCException e) {
// fallback: wrap bytes directly, no driver LOB involvement
Blob b = new SerialBlob(bytes);
return b;
} Prevention
- Set hibernate.jdbc.lob.non_contextual_creation=true when using drivers whose createBlob()/setBytes() is unreliable
- Always obtain the LobCreator from the live session and use it within the same connection scope
When it happens
Trigger: Contextual LOB creation is enabled and the JDBC driver's Connection.createBlob() returns a Blob whose setBytes(long, byte[]) fails: operation unsupported by the driver, the LOB freed before write, or the creating connection already closed when the write occurs.
Common situations: Drivers with read-only or locator-backed Blob implementations (some Oracle/DB2 modes, thin drivers behind proxies); LobCreator used after the transaction/connection closed; large byte arrays exceeding driver LOB limits.
Related errors
- Could not create JDBC Blob
- Unable to set CLOB string after creation
- Start position 1-based; must be 1 or more.
- Length must be great-than-or-equal to zero.
- Start position [<start>] cannot exceed overall CLOB length [
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/46918b1de7601d27.
Report an issue: GitHub.