Bug #20029 NullPointerException with executeBatch() inserting into longtext column
Submitted: 23 May 2006 21:49 Modified: 16 Jun 2006 22:07
Reporter: P Eger Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:3.1.12 OS:Linux (RHEL 4)
Assigned to: CPU Architecture:Any

[23 May 2006 21:49] P Eger
Description:
mysql  Ver 14.12 Distrib 5.0.18, for unknown-linux-gnu (x86_64) using readline 5.0

JDBC driver 3.1.12

Using the below high-performance bulk-insert loop against the following table sometimes results in and NPE on executeBatch(). It seems to be sporadic and/or timing dependent, and only happens on a high-latency (but reliable) link. Am not able to reproduce with local 100mbit or 1gbit ethernet network. Any ideas?

create table tbl(c0 BINARY(16) not null,c1 tinyint unsigned,c2 longtext) ENGINE = MyISAM

connection options:

ds.setUser(username);
ds.setPassword(password);
ds.setServerName(server);
ds.setDatabaseName(dbname);
ds.setConnectTimeout(5*1000);
ds.setSocketTimeout(15*60*1000);
ds.setElideSetAutoCommits(true);

How to repeat:
....
....
java.lang.NullPointerException
        at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:880)
....
....

PreparedStatement ps = conn.prepareStatement("insert into tbl(c0,c1,c2) values(?,?,?)");

int insert_batch_size = 0;

while(CONDITION)
{
  ....
  ps.setBytes(1,the_bytes);
  ps.setInt(2,theint);
  ps.setString(3,thestr);

  ps.addBatch();

  if(++insert_batch_size >= 250)
  {
    ps.executeBatch();
    insert_batch_size = 0;
  }
}

if(insert_batch_size > 0)
  ps.executeBatch(); //<-- NPE thrown here

ps.close();
[26 May 2006 2:20] P Eger
Changing line 880 of PreparedStatement from 

if (this.connection.getContinueBatchOnError()) {

to

if (this.connection!=null && this.connection.getContinueBatchOnError()) {

lets the real exception get through, turns out the MySQL session/connection  was expired (correctly) after a delay in use. However, the exception is still being swallowed here, making debugging difficult and possibly not conforming to JDBC spec.  Also it seems that this same problem might occur elsewhere in PS.java?
[26 May 2006 4:04] Mark Matthews
It's not necessarily non-JDBC-compliant, but bad form to throw an NPE in this case (so we'll fix it). JDBC-4.0 is looking to address where specifically drivers need to check for closed connections or statements. 

In this particular case, the driver just needs to call checkClosed() before letting executeBatch() proceed further.
[16 Jun 2006 22:07] 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/7787
[16 Jun 2006 22:07] Mark Matthews
This should be fixed in the latest builds of Connector/J 5.0 (the fixes to make this robust were too large to go into 3.1).

The final release of 5.0.1 will have the fix, or alternatively check a nightly snapshot build from http://downloads.mysql.com/snapshots.php#connector-j