Bug #8424 Arithmetic Exception
Submitted: 10 Feb 2005 23:22 Modified: 15 Feb 2005 19:39
Reporter: [ name withheld ]
Status: Closed
Category:Connector/J Severity:S1 (Critical)
Version:3.1.6 OS:Linux (Linux Fedora 3)
Assigned to: Target Version:

[10 Feb 2005 23:22] [ name withheld ]
Description:
java.lang.ArithmeticException: Rounding necessary
        at java.math.BigDecimal.divide(BigDecimal.java:465)
        at java.math.BigDecimal.setScale(BigDecimal.java:699)
        at java.math.BigDecimal.setScale(BigDecimal.java:735)
        at com.mysql.jdbc.ResultSet.getBigDecimalFromString(ResultSet.java:5507)
        at com.mysql.jdbc.ResultSet.getNativeBigDecimal(ResultSet.java:4016)
        at com.mysql.jdbc.ResultSet.getBigDecimal(ResultSet.java:547)

How to repeat:
It is very difficult to code a test case. Please see suggested fix.

Suggested fix:
Change this:
            try {
            	return new BigDecimal(stringVal).setScale(scale);
            } catch (NumberFormatException ex) {
                throw new SQLException(Messages.getString(
                        "ResultSet.Bad_format_for_BigDecimal____166") //$NON-NLS-1$
                     +stringVal +
                    Messages.getString("ResultSet.___in_column__167") +
                    columnIndex + "(" //$NON-NLS-1$
                     + this.fields[columnIndex - 1] + ").",
                    SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
            }

into this:
            try {
                //FIXED: no more ArithmeticExceptions
            	return new BigDecimal(stringVal).setScale(scale,BigDecimal.ROUND_HALF_UP);
            } catch (NumberFormatException ex) {
                throw new SQLException(Messages.getString(
                        "ResultSet.Bad_format_for_BigDecimal____166") //$NON-NLS-1$
                     +stringVal +
                    Messages.getString("ResultSet.___in_column__167") +
                    columnIndex + "(" //$NON-NLS-1$
                     + this.fields[columnIndex - 1] + ").",
                    SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
            }
[15 Feb 2005 19:20] Mark Matthews
This is fixed for Connector/J 3.1.7. The driver now only chooses a rounding mode of 'half
up' if non-rounding setScale() failed. 

Thanks for the bug report.