Bug #30550 ArithmeticException or NullPointerException in executeBatch
Submitted: 22 Aug 2007 2:42 Modified: 31 Aug 2007 14:19
Reporter: Sadao Hiratsuka (Basic Quality Contributor) Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:5.0.7 OS:Any
Assigned to: CPU Architecture:Any
Tags: ArithmeticException, executeBatch, NullPointerException, REWRITEBATCHEDSTATEMENTS

[22 Aug 2007 2:42] Sadao Hiratsuka
Description:
ArithmeticException or NullPointerException occurs on following environment.

* rewriteBatchedStatements = true
* Statement object has no SQL to execute.
* When executeBatch() is invoked.

How to repeat:
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(
    "jdbc:mysql://gx240/scott?rewriteBatchedStatements=true", "scott", "tiger");
connection.setAutoCommit(false);

PreparedStatement stmt = connection
    .prepareStatement("insert into batchtest (id, data) values (?, ?)");

stmt.executeBatch(); // *** CASE 1 ***

for (int id = 1; id <= 100; id++) {
  stmt.clearParameters();
  stmt.setInt(1, id);
  stmt.setString(2, "data" + id);

  stmt.addBatch();
}
stmt.executeBatch();
stmt.executeBatch(); // *** CASE 2 ***

connection.commit();
stmt.close();

connection.close();

----------------------------------------
CASE 1

Exception in thread "main" java.lang.NullPointerException
	at com.mysql.jdbc.PreparedStatement.executeBatchedInserts(PreparedStatement.java:999)
	at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:939)
	at ExecBatchTest.main(ExecBatchTest.java:29)

----------------------------------------
CASE 2

Exception in thread "main" java.lang.ArithmeticException: / by zero
	at com.mysql.jdbc.PreparedStatement.executeBatchedInserts(PreparedStatement.java:1032)
	at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:939)
	at ExecBatchTest.main(ExecBatchTest.java:36)

Suggested fix:
If rewriteBatchedStatements = false, no error occurred.
So I think the same behavior is appropriate.
[27 Aug 2007 20:47] Sveta Smirnova
Thank you for the report.

Verified as described.
[27 Aug 2007 20:47] Sveta Smirnova
test case

Attachment: bug30550.java (text/plain), 1022 bytes.

[29 Aug 2007 14:23] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/33322
[30 Aug 2007 17:03] Mark Matthews
Will be in 5.0.8 when it's released.
[31 Aug 2007 14:19] MC Brown
A note has been added to the 5.0.8 changelog: 

An ArithmeticException or NullPointerException is raised when executeBatch is called on a statement that has not been completed and rewriteBatchedStatements=true.