记录一个Long转Integer带来的问题

Long转成Integer通常怎么转?

Long sourceNum = 10L;
Integer targetNum = sourceNum.intValue();
    /**
     * Returns the value of this {@code Long} as an {@code int} after
     * a narrowing primitive conversion.
     * @jls 5.1.3 Narrowing Primitive Conversions
     */
    public int intValue() {
        return (int)value;
    }

故障代码如下:

@Data
class OcsQueueDO {
    private Integer id;
    ......
}
Long id = queueSequence.next();
OcsQueueDO ocsQueueDO = new OcsQueueDO();
ocsQueueDO.setId(id.intValue());

故障的技术根因:

数据表的主键id使用了sequence,取出来的是Long,但是对应实体类id属性使用的是Integer,由于表数据的累积,sequence取出来的值超过了Integer的范围,导致数据插入db失败。

  • Integer范围:[-2^31 - 2^31) 也即[-2147483648,2147483647]
    /**
     * A constant holding the minimum value an {@code int} can
     * have, -231.
     */
    @Native public static final int   MIN_VALUE = 0x80000000;

    /**
     * A constant holding the maximum value an {@code int} can
     * have, 231-1.
     */
    @Native public static final int   MAX_VALUE = 0x7fffffff;
  • Long范围:[-2^63 , 2^63),也即[-9223372036854775808,9223372036854775807]
    /**
     * A constant holding the minimum value a {@code long} can
     * have, -263.
     */
    @Native public static final long MIN_VALUE = 0x8000000000000000L;

    /**
     * A constant holding the maximum value a {@code long} can
     * have, 263-1.
     */
    @Native public static final long MAX_VALUE = 0x7fffffffffffffffL;

如果sequence的值超出Integer的范围,就会出现截断,如下:

    public static void main(String[] args){
        Long src =  9223372036854775807L;
        System.out.println(src.intValue());
    }
运行结果

可以看到:db的主键被设置为负数,导致sql报错

这是故障的技术根因。

故障的管理根因

Long转Integer会出现截断,现在的开发同学基本都知道这个原理,但是为什么还会出现故障?
错误代码是历史存量代码(2010年左右的code)
sequence增长超出Integer范围是一个日积月累的过程
问题刚好出现在周末,响应慢

在这个故障后,对所有存量问题做了专项治理,不得不说,好的代码习惯是一切质量的源头。

你可能感兴趣的:(记录一个Long转Integer带来的问题)