ShardingSphere引发的IndexOutOfBoundsException
- 一、异常
- 二、 原因
- 三、解决方法
- 四、总结
一、异常
### Error querying database. Cause: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
### The error may exist in file [D:\JetBrains\Idea\workspace\zohe\bjxz\ruoyi-system\target\classes\mapper\business\device\AlertInfoMapper.xml]
### The error may involve com.zhuohe.business.mapper.AlertInfoMapper.selectAlertInfoList_COUNT
### The error occurred while handling results
### SQL: SELECT count(0) FROM alert_info a LEFT JOIN scene b ON a.scene_id = b.scene_id WHERE a.alert_type = ?
### Cause: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:149)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
at sun.reflect.GeneratedMethodAccessor89.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:426)
... 124 common frames omitted
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at org.apache.shardingsphere.core.merge.dql.DQLMergeEngine.<init>(DQLMergeEngine.java:71)
at org.apache.shardingsphere.core.merge.MergeEngineFactory.newInstance(MergeEngineFactory.java:58)
at org.apache.shardingsphere.shardingjdbc.jdbc.core.statement.ShardingPreparedStatement.getResultSet(ShardingPreparedStatement.java:139)
at sun.reflect.GeneratedMethodAccessor96.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:69)
at com.sun.proxy.$Proxy147.getResultSet(Unknown Source)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getFirstResultSet(DefaultResultSetHandler.java:237)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:187)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:65)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)
at sun.reflect.GeneratedMethodAccessor94.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63)
at com.sun.proxy.$Proxy145.query(Unknown Source)
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63)
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:325)
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
at com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor.intercept(MybatisPlusInterceptor.java:65)
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61)
at com.sun.proxy.$Proxy144.query(Unknown Source)
at sun.reflect.GeneratedMethodAccessor193.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63)
at com.sun.proxy.$Proxy144.query(Unknown Source)
at com.github.pagehelper.util.ExecutorUtil.executeAutoCount(ExecutorUtil.java:166)
at com.github.pagehelper.PageInterceptor.count(PageInterceptor.java:157)
at com.github.pagehelper.PageInterceptor.intercept(PageInterceptor.java:100)
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61)
at com.sun.proxy.$Proxy144.query(Unknown Source)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)
... 129 common frames omitted
二、 原因
一开始我还以为Mybatis的问题也是糊涂啊,没仔细看报错代码 ,找了半天,各种debug 最后看到 走到了shardingsphere 的源码中报错了。最后还发现是分表查询的时候会有一个组合的过程,然后比如我发生的问题 alert_info 表 与 alert_info2022(无数据) 结果集组合的过程 发生了异常。
使用shardingsphere 4.0.0-RC1 版本
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-namespace</artifactId>
<version>4.0.0-RC1</version>
</dependency>
三、解决方法
换个 shardingsphere 版本
这里我将 4.0.0-RC1 换成了 4.1.1
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.1.1</version>
</dependency>
4.1.1 版本 与 4.0.0-RC1 有些差异需要注意
druid 需要单独引入 不能使用 druid-spring-boot-starter 会报错 DataSource注入失败的问题
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.6</version>
</dependency>
配置文件不需要在配置 Mysql 数据源这些配置 因为在shardingsphere 配置中 也会配置数据源这些操作
四、总结
发生异常的时候一定要看仔细报错原因例如 我截图的 就能大概知道是 shardingsphere框架的问题。
确认框架问题后,发生查不到的异常原因或者各种无法解决的异常 尝试将框架版本升级一下试试。 毕竟框架也是在一步步完善中
如果对你有帮助,加个关注把~