Bug #29837 JDBC driver fails with short reads in BasicSocketComm
Submitted: 17 Jul 2007 4:37 Modified: 12 Dec 2009 16:11
Reporter: Ivan Posva Email Updates:
Status: Won't fix Impact on me:
None 
Category:MaxDB Severity:S1 (Critical)
Version:7.6.0 OS:Any
Assigned to: CPU Architecture:Any

[17 Jul 2007 4:37] Ivan Posva
Description:
Depending on fragmentation of network packets on the connection between the MaxDB and the JDBC client, the JDBC client can fail to connect to the MaxDB server due to the BasicSocketComm class assuming no fragmentation of packets in certain situations in methods receiveConnect() and receiveData().

A RTEException will be thrown even if the connection between the MaxDB and the JDBC client is working properly, but the read call returns less than the expected number of bytes from the InputStream.

This problem was identified with version:
Implementation-Title: MaxDB JDBC Driver
Implementation-Version: 7.6.0   Build 030-000-005-567
Implementation-Vendor: MySQL MaxDB

How to repeat:
Modify the InputStream to only return one byte at a time to a read call to debug this problem on networks that do not reliably fragment the network packets.

Suggested fix:
Add the following helper method to com.sap.dbtech.rte.comm.BasicSocketComm which reads at least min bytes from the input before returning unless an error is detected first:

  private static int readMin(InputStream input, byte[] buf, int off, int len, int min) throws IOException {
    int result = 0;
    while (result < min) {
      int r = input.read(buf, off, len);
      if (r < 0) {
        return ((result == 0) ? r : result);
      }
      result += r;
      off += r;
      len -= r;
    }
    return result;
  }

This helper method should be called in places where the code expects a certain number of bytes to be received before proceeding such as receiveConnect() and receiveData() to avoid throwing a RTEException if the packet has been fragmented.

I have applied the above patch with great succes, where I had a 100% failure rate before.
[11 Sep 2007 11:30] Burkhard Diesing
Hi Ivan,
thanks for your patch. Although our socket connections are seldom so bad that the bytes only drip out. 
But the idea is not bad and we will integrate it into one of our next versions. 

You can follow the state with http://www.sapdb.org/webpts?wptsdetail=yes&ErrorType=0&ErrorID=1150498

Regards,
Burkhard
[4 Oct 2007 17:12] Valeriy Kravchuk
Thank you for a problem report. According to http://www.mysql.com/news-and-events/press-release/release_2007_40.html:

"support of the MaxDB database will revert back to SAP"