Bug #69308 PreparedStatement.executeBatchedInserts calls close() twice on batchedStatement
Submitted: 23 May 2013 4:34 Modified: 10 Jul 2013 23:04
Reporter: Jesse Barnum Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:5.1.22 OS:MacOS
Assigned to: Filipe Silva CPU Architecture:Any

[23 May 2013 4:34] Jesse Barnum
Description:
In the method executeBatchedInserts, the batchedStatement is closed on line 1801:

				} finally {
					if (batchedStatement != null) {
						batchedStatement.close();
					}
				}

Note that it does not set batchedStatement to null after it closes it. Then a little lower in the method, at line 1844, we again call:

				} finally {
					if (batchedStatement != null) {
						batchedStatement.close();
					}
				}

This leads to the checkClosed() method throwing an exception, which is caught and ignored by the close() method. Although this is not causing any serious problems, it seems like a mistake that has some performance impact.

It looks like there might be a similar problem in the executePreparedBatchAsMultiStatement() method, although I did not encounter that in my application.

How to repeat:
Set the rewriteBatchedStatements=true connection property, and then run a batch insert.

Suggested fix:
nullify batchStatement after calling close() on it the first time, so that it won't be attempted to be closed again.
[29 May 2013 13:45] Filipe Silva
Hi Jesse Barnum,

Thank you for this bug report. It was verified as reported.
[10 Jul 2013 23:04] Daniel So
Added entry to the Connector/J 5.1.25 changelog:

"With the connection option rewriteBatchedStatements=true, after a batch insert, the batchStatement was closed twice. This caused some unnecessary throwing and catching of errors, impacting code performance. This fix nullifies the batchStatement after the first calling of the close() method in order to avoid a second closing."