Skip to content

Commit 1b9de21

Browse files
committed
Fix for Bug#103303 (32766143), JAVA.LANG.CLASSCASTEXCEPTION WHEN
INSERTING BLOB WITH SERVER PREPARED STATEMENT.
1 parent eae0129 commit 1b9de21

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
Version 8.0.26
55

6+
- Fix for Bug#103303 (32766143), JAVA.LANG.CLASSCASTEXCEPTION WHEN INSERTING BLOB WITH SERVER PREPARED STATEMENT.
7+
68
- WL#14205, Support query attributes.
79

810
- WL#14411, Support for authentication_kerberos_client authentication plugin.

src/main/core-impl/java/com/mysql/cj/ClientPreparedQueryBindings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public void setBlob(int parameterIndex, Blob x) {
182182
setNull(parameterIndex);
183183
} else {
184184
try {
185-
setBinaryStream(parameterIndex, x.getBinaryStream());
185+
setBinaryStream(parameterIndex, x.getBinaryStream(), x.length());
186186
} catch (Throwable t) {
187187
throw ExceptionFactory.createException(t.getMessage(), t, this.session.getExceptionInterceptor());
188188
}

src/main/user-impl/java/com/mysql/cj/jdbc/ServerPreparedStatement.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,10 +720,21 @@ protected int setOneBatchedParameterSet(java.sql.PreparedStatement batchedStatem
720720
if (paramArg[j].isStream()) {
721721
Object value = paramArg[j].value;
722722

723-
if (value instanceof InputStream) {
723+
if (value instanceof byte[]) {
724+
batchedStatement.setBytes(batchedParamIndex++, (byte[]) value);
725+
} else if (value instanceof InputStream) {
724726
batchedStatement.setBinaryStream(batchedParamIndex++, (InputStream) value, paramArg[j].getStreamLength());
725-
} else {
727+
} else if (value instanceof java.sql.Blob) {
728+
try {
729+
batchedStatement.setBinaryStream(batchedParamIndex++, ((java.sql.Blob) value).getBinaryStream(), paramArg[j].getStreamLength());
730+
} catch (Throwable t) {
731+
throw ExceptionFactory.createException(t.getMessage(), this.session.getExceptionInterceptor());
732+
}
733+
} else if (value instanceof Reader) {
726734
batchedStatement.setCharacterStream(batchedParamIndex++, (Reader) value, paramArg[j].getStreamLength());
735+
} else {
736+
throw ExceptionFactory.createException(WrongArgumentException.class,
737+
Messages.getString("ServerPreparedStatement.18") + value.getClass().getName() + "'", this.session.getExceptionInterceptor());
727738
}
728739
} else {
729740
switch (paramArg[j].bufferType) {

src/test/java/testsuite/regression/StatementRegressionTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
import java.util.function.ToIntFunction;
107107

108108
import javax.sql.XAConnection;
109+
import javax.sql.rowset.serial.SerialBlob;
109110

110111
import org.junit.jupiter.api.Disabled;
111112
import org.junit.jupiter.api.Test;
@@ -11538,4 +11539,49 @@ public void testBug20391550() throws Exception {
1153811539
}
1153911540
}
1154011541
}
11542+
11543+
/**
11544+
* Test fix for Bug#103303 (32766143), JAVA.LANG.CLASSCASTEXCEPTION WHEN INSERTING BLOB WITH SERVER PREPARED STATEMENT.
11545+
*
11546+
* @throws Exception
11547+
*/
11548+
@Test
11549+
public void testBug103303() throws Exception {
11550+
createTable("testBug103303", "(id INT AUTO_INCREMENT PRIMARY KEY, blob1 MEDIUMBLOB)");
11551+
11552+
Connection con = null;
11553+
Properties props = new Properties();
11554+
11555+
for (boolean rewriteBS : new boolean[] { true, false }) {
11556+
for (boolean useSSPS : new boolean[] { true, false }) {
11557+
props.setProperty(PropertyKey.rewriteBatchedStatements.getKeyName(), "" + rewriteBS);
11558+
props.setProperty(PropertyKey.useServerPrepStmts.getKeyName(), "" + useSSPS);
11559+
11560+
try {
11561+
this.stmt.executeUpdate("truncate table testBug103303");
11562+
11563+
con = getConnectionWithProps(props);
11564+
11565+
PreparedStatement statement = con.prepareStatement("INSERT INTO testBug103303(blob1) VALUES(?)", PreparedStatement.RETURN_GENERATED_KEYS);
11566+
statement.setBlob(1, new SerialBlob("test1".getBytes()));
11567+
statement.addBatch();
11568+
statement.setBlob(1, new SerialBlob("test2".getBytes()));
11569+
statement.addBatch();
11570+
statement.executeBatch();
11571+
11572+
this.rs = this.stmt.executeQuery("select blob1 from testBug103303 order by id");
11573+
assertTrue(this.rs.next());
11574+
assertEquals("test1", this.rs.getString(1));
11575+
assertTrue(this.rs.next());
11576+
assertEquals("test2", this.rs.getString(1));
11577+
assertFalse(this.rs.next());
11578+
11579+
} finally {
11580+
if (con != null) {
11581+
con.close();
11582+
}
11583+
}
11584+
}
11585+
}
11586+
}
1154111587
}

0 commit comments

Comments
 (0)