{"record":{"id":"4a7a69a1076f23fa","repo":"hibernate/hibernate-orm","slug":"could-not-create-jdbc-blob","errorCode":null,"errorMessage":"Could not create JDBC Blob","messagePattern":"Could not create JDBC Blob","errorType":"exception","errorClass":"JDBCException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/BlobAndClobCreator.java","lineNumber":172,"sourceCode":"\t * Obtain a {@link Blob} instance which can be written to a JDBC\n\t * {@link java.sql.PreparedStatement} using\n\t * {@link java.sql.PreparedStatement#setBlob(int, Blob)}.\n\t */\n\t@Override\n\tpublic Blob toJdbcBlob(Blob blob) {\n\t\ttry {\n\t\t\tif ( useConnectionToCreateLob ) {\n//\t\t\t\tfinal Blob jdbcBlob = createBlob();\n//\t\t\t\tblob.getBinaryStream().transferTo( jdbcBlob.setBinaryStream(1) );\n//\t\t\t\treturn jdbcBlob;\n\t\t\t\treturn createBlob( blob.getBytes( 1, (int) blob.length() ) );\n\t\t\t}\n\t\t\telse {\n\t\t\t\treturn super.toJdbcBlob( blob );\n\t\t\t}\n\t\t}\n\t\tcatch (SQLException e) {\n\t\t\tthrow new JDBCException( \"Could not create JDBC Blob\", e );\n\t\t}\n//\t\tcatch (IOException e) {\n//\t\t\tthrow new HibernateException( \"Could not create JDBC Blob\", e );\n//\t\t}\n\t}\n\n\t/**\n\t * Obtain a {@link Clob} instance which can be written to a JDBC\n\t * {@link java.sql.PreparedStatement} using\n\t * {@link java.sql.PreparedStatement#setClob(int, Clob)}.\n\t */\n\t@Override\n\tpublic Clob toJdbcClob(Clob clob) {\n\t\ttry {\n\t\t\tif ( useConnectionToCreateLob ) {\n//\t\t\t\tfinal Clob jdbcClob = createClob();\n//\t\t\t\tclob.getCharacterStream().transferTo( jdbcClob.setCharacterStream(1) );\n//\t\t\t\treturn jdbcClob;","sourceCodeStart":154,"sourceCodeEnd":190,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/BlobAndClobCreator.java#L154-L190","documentation":"BlobAndClobCreator.toJdbcBlob converts a Blob into a JDBC-usable form: with useConnectionToCreateLob it reads all bytes via blob.getBytes(1, (int) blob.length()) and writes them into a freshly created JDBC Blob; otherwise it delegates to the non-contextual parent. Any SQLException on that path is wrapped as JDBCException('Could not create JDBC Blob'). Note the (int) cast on length(): BLOBs larger than Integer.MAX_VALUE overflow before the driver is even reached.","triggerScenarios":"Binding a Blob to a PreparedStatement while contextual creation is enabled and either the read/create/write path throws SQLException, or blob.length() exceeds Integer.MAX_VALUE and the cast silently truncates/overflows the read length.","commonSituations":"Drivers whose createBlob/setBytes path fails (same root causes as errors 1317/1318); attempted binding of multi-gigabyte BLOBs; using a stale LobCreator after its connection closed.","solutions":["Enable hibernate.jdbc.lob.non_contextual_creation=true to use the non-contextual parent path (stream-based binding, no byte[] copy)","Upgrade the JDBC driver if createBlob()/setBytes() is the failing link","For BLOBs over 2GB, bind via setBinaryStream/stream LobCreator paths instead of toJdbcBlob","Check the wrapped SQLException to identify which call failed (getBytes vs setBytes)"],"exampleFix":"# before: contextual path, byte[] copy (also caps at 2GB via (int) cast)\n<property name=\"hibernate.jdbc.lob.non_contextual_creation\" value=\"false\"/>\n\n# after\n<property name=\"hibernate.jdbc.lob.non_contextual_creation\" value=\"true\"/>","handlingStrategy":"fallback","validationCode":"// guard the 2GB int-overflow and driver weakness before converting\nif ( blob.length() > Integer.MAX_VALUE ) {\n    throw new IllegalArgumentException(\"BLOB exceeds 2GB; bind via stream instead of toJdbcBlob\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    return lobCreator.toJdbcBlob(blob);\n} catch (JDBCException e) {\n    // fall back to stream binding, bypassing the byte[] copy entirely\n    ps.setBinaryStream(index, blob.getBinaryStream(), blob.length());\n}","preventionTips":["Enable hibernate.jdbc.lob.non_contextual_creation=true to avoid the byte[] copy path entirely","For very large BLOBs, prefer setBinaryStream-style binding over Blob conversion"],"tags":["hibernate","jdbc","blob","lob","lob-creator","large-objects"],"backgroundTag":"lob-creation-failed","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}