zoukankan      html  css  js  c++  java
  • JVM加载类冲突,导致Mybatis查数据库返回NULL的一个小问题

    今天碰到个bug,虽然小,但是有点意思

    背景是SpringMVC + Mybatis的一个项目,mapper文件里写了一条sql 大概相当于 select a from tableA where b = "123" 这样的级别

    然后不管传进去的是什么 数据库里有没有 都会返回null


    第一反应是sql语句写错了,比如把1和小写L弄混了之类的,传给sql的参数里有奇怪的空格等等

    于是打开debug log 拿到传给sql的preparedStatement 和对应的参数

    复制到console里自己查了一下,可以执行,返回结果也正确,说明不是sql的问题


    既然不是sql的问题,那只好调试一下代码了

    既然preparedStatement sql能够被打印出来,所以就不从业务逻辑加断点了,直接定位到PreparedStatement类 找到execute方法,上个断点开始单步

    单步的时候发现ide提示说源代码和class文件对应行不一致,并且对我正在监视的变量报了一个类中不存在该变量的错

    所以怀疑是引用冲突了


    接下来确认一下是不是引用了奇怪的类 ,用下面这个方法去定位一下PreparedStatement的位置

        public static String where(final Class cls) {
            if (cls == null)throw new IllegalArgumentException("null input: cls");
            URL result = null;
            final String clsAsResource = cls.getName().replace('.', '/').concat(".class");
            final ProtectionDomain pd = cls.getProtectionDomain();
            if (pd != null) {
                final CodeSource cs = pd.getCodeSource();
                if (cs != null) result = cs.getLocation();
                if (result != null) {
                    if ("file".equals(result.getProtocol())) {
                        try {
                            if (result.toExternalForm().endsWith(".jar") ||
                                    result.toExternalForm().endsWith(".zip"))
                                result = new URL("jar:".concat(result.toExternalForm())
                                        .concat("!/").concat(clsAsResource));
                            else if (new File(result.getFile()).isDirectory())
                                result = new URL(result, clsAsResource);
                        }
                        catch (MalformedURLException ignore) {}
                    }
                }
            }
            if (result == null) {
                final ClassLoader clsLoader = cls.getClassLoader();
                result = clsLoader != null ?
                        clsLoader.getResource(clsAsResource) :
                        ClassLoader.getSystemResource(clsAsResource);
            }
            return result.toString();
        }

    用where方法去查一下类,发现jvm没有加载我认为的msql-java-connector-5.1.63 而是加载了一个内部类库里的java-connector,定位进去看了一下 确实是写的有问题,原因找到了。


     最后到maven依赖里,打开依赖树,找到了加载这个自定义connector的pom条目,配置了<exclusions> ,然后重启项目,解决。

  • 相关阅读:
    你所选择的栏目与当前模型不相符请选择白色的选
    DEDECMS首页调用图片集里的多张图片
    dedecms内容页调用图片集文档的图集图片
    Sublime Text 使用介绍、全套快捷键及插件推荐
    基于Nutch+Hadoop+Hbase+ElasticSearch的网络爬虫及搜索引擎
    nutch
    ant安装
    selenium经过WebDriverWait实现ajax测试
    查看当前android设备已安装的第三方包
    批处理学习总结之常用符号
  • 原文地址:https://www.cnblogs.com/bethunebtj/p/7479930.html
Copyright © 2011-2022 走看看