org.apache.ibatis.exceptions.TooManyResultsException的异常排查过程

简介: org.apache.ibatis.exceptions.TooManyResultsException的异常排查过程

在查阅测试环境业务日志中的ERROR级别的日志时,发现了有一个Mybatis相关的异常错误org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 13

一、分析:详细堆栈信息打印如下

2018-09-30 17:30:17.548 ERROR 14146 --- [DubboServerHandler-172.16.44.184:20882-thread-136] [c.a.dubbo.rpc.filter.ExceptionFilter    :85] :  [DUBBO] Got unchecked and undeclared exception which called by 172.16.44.184. service: net.lingyejun.mall.product.facade.ProductDetailService, method: getSkuLiveStatusInfo, exception: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 13, dubbo version: 2.6.2, current host: ip
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 13
        at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77)
        at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
        at com.sun.proxy.$Proxy78.selectOne(Unknown Source)
        at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:166)
        at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:82)
        at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
        at com.sun.proxy.$Proxy84.selectOneByExample(Unknown Source)
        at net.lingyejun.mall.product.impl.ProductSkuServiceImpl.getSkuInfoById(ProductSkuServiceImpl.java:95)
        at sun.reflect.GeneratedMethodAccessor138.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:197)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
        at com.sun.proxy.$Proxy95.getSkuInfoById(Unknown Source)
        at net.lingyejun.mall.product.impl.ProductDetailServiceImpl.getSkuLiveStatusInfo(ProductDetailServiceImpl.java:122)
        at sun.reflect.GeneratedMethodAccessor178.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:197)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
        at com.sun.proxy.$Proxy98.getSkuLiveStatusInfo(Unknown Source)
        at com.alibaba.dubbo.common.bytecode.Wrapper2.invokeMethod(Wrapper2.java)
        at com.alibaba.dubbo.rpc.proxy.javassist.JavassistProxyFactory$1.doInvoke(JavassistProxyFactory.java:47)
        at com.alibaba.dubbo.rpc.proxy.AbstractProxyInvoker.invoke(AbstractProxyInvoker.java:76)
        at com.alibaba.dubbo.config.invoker.DelegateProviderMetaDataInvoker.invoke(DelegateProviderMetaDataInvoker.java:52)
        at com.alibaba.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:56)
        at com.alibaba.dubbo.validation.filter.ValidationFilter.invoke(ValidationFilter.java:58)
        at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
        at com.alibaba.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:62)
        at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
        at com.alibaba.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:75)
        at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
        at com.alibaba.dubbo.rpc.filter.TimeoutFilter.invoke(TimeoutFilter.java:42)
        at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
        at com.alibaba.dubbo.rpc.filter.AccessLogFilter.invoke(AccessLogFilter.java:155)
        at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
    at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:103)
        at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:96)
        at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:172)
        at com.alibaba.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:51)
        at com.alibaba.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:80)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
    Caused by: org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 13
        at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:81)
        at sun.reflect.GeneratedMethodAccessor183.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:433)
        ... 62 common frames omitted  

二、根源:条件为Null则会忽略

检查数据库查询那里的代码后发现并无异样,但是这个堆栈信息又将错误非常明晰地指出,一定是Mybatis的SelectOne查询sql,查出了多条记录,但是在数据库表中skuId和status对应的是唯一一条记录。难不成?有些条件没有生效,执行sql时将skuId这一查询条件去掉后发现查询出来的的确是13条记录。

Example example = new Example(ProductSku.class);
// 设置查询条件
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("skuId", skuId);
criteria.andEqualTo("status", ProductConst.DataStatus.NORMAL);
ProductSku productSku = productSkuMapper.selectOneByExample(example);

没错,当Mybatis的Criteria中的查询条件为Null时,则会忽略掉这个查询条件。即,skuId为空Null的时候就没有带skuId这个条件,直接是where status = 0;所以查出了多条记录,像这样的查询条件需要在最外层做判空处理,接口层若缺少判空处理,则会出现此问题。


目录
相关文章
|
3月前
|
Prometheus Cloud Native 关系型数据库
实时计算 Flink版操作报错合集之实时计算 Flink版操作报错合集之当从保存点恢复并添加新的表时,出现了org.apache.flink.util.FlinkRuntimeException异常,该怎么办
在使用实时计算Flink版过程中,可能会遇到各种错误,了解这些错误的原因及解决方法对于高效排错至关重要。针对具体问题,查看Flink的日志是关键,它们通常会提供更详细的错误信息和堆栈跟踪,有助于定位问题。此外,Flink社区文档和官方论坛也是寻求帮助的好去处。以下是一些常见的操作报错及其可能的原因与解决策略。
|
4月前
|
IDE Java 应用服务中间件
解决org.apache.jasper.JasperException异常
解决org.apache.jasper.JasperException异常
|
12月前
|
Java Apache Maven
【异常解决】Handler dispatch failed;nested exception is java.lang.NoClassDefFoundError: org/apache/common
【异常解决】Handler dispatch failed;nested exception is java.lang.NoClassDefFoundError: org/apache/common
3169 0
|
12月前
|
Java
【Java异常】Error:(30, 62) java: 程序包com.sun.org.apache.xerces.internal.impl.dv.util不存在
【Java异常】Error:(30, 62) java: 程序包com.sun.org.apache.xerces.internal.impl.dv.util不存在
1155 0
|
12月前
|
存储 Cloud Native 应用服务中间件
解决Apache Tomcat “Request header is too large“ 异常 ‍
解决Apache Tomcat “Request header is too large“ 异常 ‍
260 1
|
XML SQL Java
mybatis-plus异常记录:org.apache.ibatis.binding.BindingException Invalid bound statement
mybatis-plus异常记录:org.apache.ibatis.binding.BindingException Invalid bound statement
1540 0
mybatis-plus异常记录:org.apache.ibatis.binding.BindingException Invalid bound statement
|
12月前
|
缓存 Java 编译器
【Java异常】Error:(19, 21) java: 无法访问org.apache.poi.xwpf.usermodel.ParagraphAlignment 找不到org.apache.po
【Java异常】Error:(19, 21) java: 无法访问org.apache.poi.xwpf.usermodel.ParagraphAlignment 找不到org.apache.po
311 0
|
12月前
|
Java 数据库连接 mybatis
【Mybatis异常】nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter
【Mybatis异常】nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter
746 0
|
12月前
|
XML Java 数据库连接
【异常解决】解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题
【异常解决】解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题
166 0
|
XML 前端开发 Java
解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)异常
解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)异常
1326 0
解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)异常

推荐镜像

更多
下一篇
无影云桌面