Bug #85358 Exceptions are covered in PreparedStatement.executeBatch
Submitted: 8 Mar 2017 6:19 Modified: 8 Mar 2017 11:47
Reporter: Jun Sun Email Updates:
Status: Verified Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:mysql-connector 5.1.30 OS:Linux
Assigned to: CPU Architecture:Any

[8 Mar 2017 6:19] Jun Sun
Description:
I have always got an exception of 'No operations allowed after statement close' in my program.
It will happen when a socket timeout appears in my connection when i am trying to do executeBatch.

According to the exception thread stack, the exception was thrown from 'clearBatch', which is called finally in PreparedStatement.executeBatch method.

After reading your source code, I found that you may make a mistake in PreparedStatement.executeBatch, here comes the wrong code。

public int[] executeBatch() throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        
         try {
           // do executeBatch
           .......
         } finally {
            this.statementExecuting.set(false);
				
            clearBatch();
         }
    }
}

If communications exception happens when it is doing executeBatch in try, your code will close current connection in ConnectionImpl.execSQL, and all open statements will be closed too. But
your finally code does not handle the 'communications exception', it still will do clearBatch, and when it is trying to clear all batched arguments it will find connection has already been closed and will throw an exception of 'No operations allowed after statement close' instead of the original exception 'communications exception'.

How to repeat:
I set socket timeout to a small value, like 1 second, and then I get a connection with this configuration, before i try to execute a prepared statement with executeBatch, I block the port of my mysql server in a remote machine with iptables, so the 'socket timeout' situation  will easily be constructed, at last I got my expected 'No operations allowed after statement close' and the connection has already been closed indeed.

Suggested fix:
To my opinion, in such situation, you may just throw the original exception to the caller, you surely could cleanBatch finally, but do not throw any exception that will cover the original one. This does have much bad influence on my when I was trying to find out why I got  'No operations allowed after statement close'
[8 Mar 2017 11:47] Chiranjeevi Battula
Hello Jun Sun,

Thank you for the bug report.
Verified based on internal discussion with dev's.

Thanks,
Chiranjeevi.