- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我遇到了 IntegerCache 的问题:使用内部使用 iBatis PreparedStatement 类的 iBatis 数据访问框架。
像这样调用数据库过程
{ call UPDATE_PROC(?,?,?,?,?,?) }
with params : [123, 234, 345, TEST1, 567, TEST2]
当 iBatis API 设置第一个参数时使用:
typeHandler.setParameter(ps, i + 1, value, mapping.getJdbcTypeName());
i=0, value=123
这里ps是对PreparedStatement的引用,i是数据库过程中的参数索引。
内部调用
ps.setInt(i, ((Integer) parameter).intValue());
i=1, parameter=123 (Note : i is int not Integer)
在内部使用 Java 反射 API 调用此调用:
public Object invoke(Object proxy, Method method, Object[] params) throws Throwable
method : setInt, params : [1, 123]
在为方法调用获取 i 的整数值时,JVM 调用以下方法:
public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
IntegerCache.low = -128 IntegerCache.high = 127
IntegerCache.cache[i + (-IntegerCache.low)]
的值以 IntegerCache.cache[129]
结尾,应该是 1
在整数缓存索引 [129] 处,但是当我调试代码时,我在索引 [129] 处找到值 3
:
, -8, -7, -6, -5, -4, -3, -2, -1, 0, 3 **index[129]**, 2, 3 **index[131]**, 4, 5, 6, 7, 8, 9, 10, 11
因为 IntegerCache 是最终类型,所以不应该有重复值,例如3 在索引 [129] 和 [131]
所以,我最终得到了异常(exception):
java.sql.SQLException: Missing IN or OUT parameter at index:: 1
我的问题是这怎么可能?请建议
最佳答案
my question is how this can be possible?
某些代码可能会通过反射更改缓存实例的 Integer.value
字段。
关于带有 IntegerCache 的 Java valueOf(int) 为 valueOf(1) 返回值 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35593278/
这个问题在这里已经有了答案: Integers caching in Java [duplicate] (2 个答案) 关闭 8 年前。 在Integer class中,有private stati
我们都知道,Java 对 [-128, 127] 范围内的数字(被认为是“常用”。 缓存设计如下: private static class IntegerCache { static fin
我遇到了 IntegerCache 的问题:使用内部使用 iBatis PreparedStatement 类的 iBatis 数据访问框架。 像这样调用数据库过程 { call UPDATE_PRO
我是一名优秀的程序员,十分优秀!