Bug #85317 execute Batch will throw null pointer exception where the column is Blob!
Submitted: 6 Mar 2017 8:07 Modified: 7 Apr 2022 22:21
Reporter: David Gao Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:MySQL 5.6.35 Connector/J 5.1.40 OS:Any
Assigned to: CPU Architecture:Any
Tags: batch, BLOB

[6 Mar 2017 8:07] David Gao
Description:
the create table SQL is “create table test(k varchar(127) not null, v blob not null, primary key(k)) default charset utf8”
the modify table SQL is “replace into test(k,v) values(?,?)”

where use executeBatch to process the sql to insert blob,it will throw null pointer exception in file PreparedStatement.java line 1666, the function is computeMaxParameterSetSizeAndBatchSize(int numBatchedArgs), the line is int paramLength = paramArg.parameterStrings[j].length;
I am confused, it is StreamBatch, But it use parameterStrings,it is maybe wrong!

How to repeat:
MySQL version is 5.6.35
Connector/J version is 5.1.40
the connect url must contain rewriteBatchedStatements=true
the create table SQL is “create table test(k varchar(127) not null, v blob not null, primary key(k)) default charset utf8”
the modify table SQL is “replace into test(k,v) values(?,?)”

the Batch process SQL is
String sql = "replace into test(k,v) values(?,?)";
byte[] ba = "abcdefg".getBytes();
Connection conn = .....
conn.setAutoCommit(false);
PreparedStatement ps = comm.prepareStatement(sql);
for(int i=1;i<10000;i++) {
  ps.setString(1, String.valueOf(i));
  ps.setBlob(2, new ByteArrayInputStream(ba))
  ps.addBatch();
}
ps.executeBatch();
ps.close();
conn.commit();

Suggested fix:
Modify the computeMaxParameterSetSizeAndBatchSize to avoid throw null pointer exception
[6 Mar 2017 9:43] Chiranjeevi Battula
Hello David Gao,

Thank you for the bug report and test case.
Verified this behavior on MySQL Connector / J 5.1.40.

Thanks,
Chiranjeevi.
[6 Mar 2017 9:43] Chiranjeevi Battula
5.1.40
run:
java.lang.NullPointerException
	at com.mysql.jdbc.PreparedStatement.computeMaxParameterSetSizeAndBatchSize(PreparedStatement.java:1666)
	at com.mysql.jdbc.PreparedStatement.computeBatchSize(PreparedStatement.java:1623)
	at com.mysql.jdbc.PreparedStatement.executeBatchedInserts(PreparedStatement.java:1490)
	at com.mysql.jdbc.PreparedStatement.executeBatchInternal(PreparedStatement.java:1248)
	at com.mysql.jdbc.StatementImpl.executeBatch(StatementImpl.java:958)
	at javaapplication3.bug_85317.main(bug_85317.java:33)
BUILD SUCCESSFUL (total time: 2 seconds)
[7 Apr 2022 22:21] Daniel So
Posted by developer:
 
Added an entry to the Connector/J 8.0.29 changelog: 

"When the connection property rewriteBatchedStatements was set to true, inserting a BLOB using a prepared statement and executeBatch() resulted in a NullPointerException."