zoukankan      html  css  js  c++  java
  • 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;所以查出了多条记录,像这样的查询条件需要在最外层做判空处理,接口层若缺少判空处理,则会出现此问题。

  • 相关阅读:
    快速入门系列--MVC--07与HTML5移动开发的结合
    快速入门系列--WebAPI--01基础
    快速入门系列--CLR--02多线程
    MongoDB快速入门
    ubuntu 12.04 server编译安装nginx
    apache 正反向代理
    c# 继承,多态,new /overrid 区别, 引用父类的方法
    python 异步线程简单实现
    ubuntu上完全卸载package
    apache2:Invalid option to WSGI daemon process definition
  • 原文地址:https://www.cnblogs.com/lingyejun/p/9743670.html
Copyright © 2011-2022 走看看