Bug #71666 Int type conversion does't work if useCursorFetch and defaultFetchSize>0
Submitted: 11 Feb 2014 9:02 Modified: 11 Feb 2014 16:44
Reporter: Andriy Sukhyy Email Updates:
Status: Verified Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:5.1.29 OS:Microsoft Windows (64bit)
Assigned to: Filipe Silva CPU Architecture:Any
Tags: defaultFetchSize, useCursorFetch

[11 Feb 2014 9:02] Andriy Sukhyy
Description:
Precondition:
0. Connector/J - 5.1.29, MySQL Community Server - 5.6.15-log, WIN64(x86_64)
1. Stored procedure with out parameter(INTEGER type).
2. properties.put("useUnicode", "true");
   properties.put("characterEncoding", "UTF-8");
   properties.put("characterSetResults", "UTF-8");
   properties.put("useCursorFetch", "true"); // 1
   properties.put("defaultFetchSize", "100"); // 2
   properties.put("useServerPrepStmts", "true");
3. If (1) and (2) are omitted then it works as expected, in they present exception will be thrown.
4. Code to execute stored procedure:
CallableStatement cstmt = own.prepareCall("{call numberOfUsers(?)}");
cstmt.registerOutParameter(1, Types.INTEGER);
cstmt.execute();
int result = cstmt.getInt(1);

How to repeat:
1. Create stored procedure with integer out parameter (no other parameters are required);
1. Create connection to DB with useCursorFetch=true&defaultFetchSize=100
3. Create java.sql.CallableStatement for this stored procedure (see description).
4. Register type of out parameter (see description).
5. Execute it.
6. Exception will be thrown.

Suggested fix:
I tried to get bytes instead of integer (when (1), (2) are presented)and realized that these bytes represent long value:
byte[] bytes = cstmt.getBytes(1);
byte[] reversed = new byte[bytes.length];
for (int i = 0; i < bytes.length; i++) {
 reversed[i] = bytes[bytes.length - 1 - i];
}
long numberOfUsers = ByteBuffer.wrap(reversed).getLong();
[11 Feb 2014 16:42] Filipe Silva
Hi Andriy,

Thank you for this bug report. Verified as described.