SessionFactoryImpl.get错误:java.lang.ArrayIndexOutOfBoundsException: 68问题

最近项目在生产环境抛错:

at org.hibernate.impl.SessionFactoryImpl.get(SessionFactoryImpl.java:339)
at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:411)
at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:884)
at org.hibernate.impl.SessionImpl.iterate(SessionImpl.java:920)
at org.hibernate.impl.QueryImpl.iterate(QueryImpl.java:41)

....

上述是调用Hibernate查询数据query.iterate();后,就抛错了,根据日志分析,错误日志记录的一般是java.lang.ArrayIndexOutOfBoundsException: -68,后面是一个-128范围内的负数,并且根据时间逐渐增大,比如到java.lang.ArrayIndexOutOfBoundsException: -2,然后java.lang.ArrayIndexOutOfBoundsException: -1,然后又轮回到java.lang.ArrayIndexOutOfBoundsException: -127。

开始看了半天一直在找自己代码的问题,最后通过查看hibernate源码SessionFactoryImpl.java的339附近代码,方法如下:

private synchronized Object get(Object key)
{
Object result = this.softQueryCache.get(key);
if (result != null) {
this.strongRefs[(++this.strongRefIndex % 128)] = result;
}
return result;
}

this.strongRefs[(++this.strongRefIndex % 128)] = result;为第339行,strongRefs数组是初始化长度为128的数组,strongRefIndex被定义为:private transient int strongRefIndex = 0;

strongRefIndex是根据每次获取数据都递增1的,根据报错内容,应该是strongRefIndex为负数了,而且每次也确实在递增。

通过上述分析,strongRefIndex字段要不内存中被无故修改了,或者就是递增到int的最大长度2147483647了,导致变为了负数。好像没有其他办法,只能重启网站,重启后问题果然解决了!

原文地址:https://www.cnblogs.com/Lawson/p/2811371.html