{"record":{"id":"46918b1de7601d27","repo":"hibernate/hibernate-orm","slug":"unable-to-set-blob-bytes-after-creation","errorCode":null,"errorMessage":"Unable to set BLOB bytes after creation","messagePattern":"Unable to set BLOB bytes 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":75,"sourceCode":"\t * @return The created BLOB reference.\n\t */\n\tBlob createBlob() {\n\t\treturn lobCreationContext.fromContext( CREATE_BLOB_CALLBACK );\n\t}\n\n\t/**\n\t * Create a {@link Blob} object after reading a {@code byte[]}\n\t * array from a JDBC {@link ResultSet}.\n\t */\n\t@Override\n\tpublic Blob createBlob(byte[] bytes) {\n\t\tfinal Blob blob = createBlob();\n\t\ttry {\n\t\t\tblob.setBytes( 1, bytes );\n\t\t\treturn blob;\n\t\t}\n\t\tcatch ( SQLException e ) {\n\t\t\tthrow new JDBCException( \"Unable to set BLOB bytes after creation\", e );\n\t\t}\n\t}\n\n\t/**\n\t * Create a {@link Blob} 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 Blob createBlob(InputStream stream, long length) {\n\t\treturn NonContextualLobCreator.INSTANCE.createBlob( stream, length );\n\t}\n","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/BlobAndClobCreator.java#L57-L93","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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)"],"exampleFix":"# before\n# contextual LOB creation active, driver setBytes fails\n\n# after\n<property name=\"hibernate.jdbc.lob.non_contextual_creation\" value=\"true\"/>","handlingStrategy":"fallback","validationCode":"// opt out of contextual LOB creation for drivers with weak Blob support\nif ( driverHasWeakLobSupport ) {\n    settings.put(AvailableSettings.NON_CONTEXTUAL_LOB_CREATION, true);\n}","typeGuard":null,"tryCatchPattern":"try {\n    return lobCreator.createBlob(bytes);\n} catch (JDBCException e) {\n    // fallback: wrap bytes directly, no driver LOB involvement\n    Blob b = new SerialBlob(bytes);\n    return b;\n}","preventionTips":["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"],"tags":["hibernate","jdbc","blob","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"}