Arthas之WatchSql

简介: 在使用Arthas排查线上问题的时候,有些时候我们需要查看某些Sql的生成,如果线上没有完备的APM的话,那么如何临时查看呢,前几篇文章我们分析了Mybatis的插件机制,如果你还记得的话,我们可以通过watch这个插件进行查看。

前言

image (6).png
在使用Arthas排查线上问题的时候,有些时候我们需要查看某些Sql的生成,如果线上没有完备的APM的话,那么如何临时查看呢,前几篇文章我们分析了Mybatis的插件机制,如果你还记得的话,我们可以通过watch这个插件进行查看。

Mybatis核心组件

因为现在大部分公司都是使用的Mybatis,我们就以它举例说明,你可以推导任何的方式,先回顾一下Mybatis的核心组件的执行流程,具体的请参阅我之前的文章,就不挂链接了。
image.png
因为一个SqlSession对应一个Executor对象,而Executor是MyBatis的内部SQL执行器,它负责调用StatementHandler操作数据库进行增删改查的具体操作,并把结果集通过ResultSetHandler进行自动映射。我们在自定义拦截器的时候不知道你还记得吗,如下:

/**
 * @author Duansg
 * @date 2022-10-08 11:45 下午
 */
@Intercepts(value = {
    @Signature(type = Executor.class, method = "query",
            args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
    @Signature(type = Executor.class, method = "query",
            args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})}
)
public class ExampleInterceptor implements Interceptor {
    //.....
}

Executor

Executor的方法如下:

public interface Executor {

  <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException;

  <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException;

  // ....
}
其底层都会走到有BoundSql的方法:
 @Override
  public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
    BoundSql boundSql = ms.getBoundSql(parameter);
    CacheKey key = createCacheKey(ms, parameter, rowBounds, boundSql);
    return query(ms, parameter, rowBounds, resultHandler, key, boundSql);
  }

BoundSql

/**
 *    Copyright 2009-2019 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.mapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.property.PropertyTokenizer;
import org.apache.ibatis.session.Configuration;

/**
 * An actual SQL String got from an {@link SqlSource} after having processed any dynamic content.
 * The SQL may have SQL placeholders "?" and an list (ordered) of an parameter mappings
 * with the additional information for each parameter (at least the property name of the input object to read
 * the value from).
 * <p>
 * Can also have additional parameters that are created by the dynamic language (for loops, bind...).
 *
 * @author Clinton Begin
 */
public class BoundSql {

  private final String sql;
  private final List<ParameterMapping> parameterMappings;
  private final Object parameterObject;
  private final Map<String, Object> additionalParameters;
  private final MetaObject metaParameters;

  public BoundSql(Configuration configuration, String sql, List<ParameterMapping> parameterMappings, Object parameterObject) {
    this.sql = sql;
    this.parameterMappings = parameterMappings;
    this.parameterObject = parameterObject;
    this.additionalParameters = new HashMap<>();
    this.metaParameters = configuration.newMetaObject(additionalParameters);
  }

  public String getSql() {
    return sql;
  }

  public List<ParameterMapping> getParameterMappings() {
    return parameterMappings;
  }

  public Object getParameterObject() {
    return parameterObject;
  }

  public boolean hasAdditionalParameter(String name) {
    String paramName = new PropertyTokenizer(name).getName();
    return additionalParameters.containsKey(paramName);
  }

  public void setAdditionalParameter(String name, Object value) {
    metaParameters.setValue(name, value);
  }

  public Object getAdditionalParameter(String name) {
    return metaParameters.getValue(name);
  }
}

Arthas Idea Plugin

doQuery的底层在最终查询的时候,都会从boundSql中执行getSql()方法
  @Override
  public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
    String sql = boundSql.getSql();
    statement.execute(sql);
    return resultSetHandler.handleResultSets(statement);
  }
所以,我们执行watch这个getSql()方法

image.png

生成的命令如下
watch org.apache.ibatis.mapping.BoundSql getSql '{params,returnObj,throwExp}'  -n 5  -x 3 

小结

这种方式可以临时用一下,如果是核心业务的话,线上的调用量可能会很频繁,不建议在线上直接使用此命令,在疯狂输出的情况下,你也未必能找到你想要的这条Sql。

目录
相关文章
|
Arthas Java 测试技术
Arthas可以对线上jar中的代码进行断点查看
Arthas可以对线上jar中的代码进行断点查看
4241 1
|
Apache
apisix~14在自定义插件中调用proxy_rewrite
在 Apache APISIX 中,通过 proxy-rewrite 插件来修改上游配置时,需要确保插件的执行顺序和上下文环境正确。你提到在自己的插件中调用 proxy_rewrite.rewrite({host="new_upstream"}, ctx),但新上游没有生效,这可能是由于以下几个原因: 1. 插件执行顺序:确保你的自定义插件在 proxy-rewrite 插件之后执行,proxy-rewrite.priority是1008。
261 0
|
缓存 JSON Dubbo
Dubbo 版 Swagger 来啦!Dubbo-Api-Docs 发布
Swagger 是一个规范和完整的前端框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。Swagger 规范也逐渐发展成为了 OpenAPI 规范。
Dubbo 版 Swagger 来啦!Dubbo-Api-Docs 发布
|
存储 Java 调度
Quartz 的启动流程分析
Quartz 的启动流程分析
455 0
|
Arthas Java 测试技术
Arthas中cat 、cls、echo、grep基础命令应用
通过本教程的操作,您可以体验如何Alibaba Cloud Linux  2.1903 LTS 64位操作系统的云服务器上学习cat 、cls、echo、grep基础命令教程用法。
|
Java 应用服务中间件 Apache
Spring Boot几种启动问题的解决方案
  使用Spring Boot以来,遇到和解决过好几次不同的项目启动问题,大多数事故起于错误的配置和依赖。因此,本文用于汇总这些问题,以及提供相应的解决方案,帮助大家更快的定位和排除故障。
2980 0
|
缓存 网络协议 Oracle
Spring集成H2内存数据库
H2内存数据库使用,满足缓存关系型数据库的使用,快速上手,无缝衔接oracle、mysql
Spring集成H2内存数据库
|
Arthas 测试技术
Arthas之Watch静态对象的值
如果在线上排查问题的时候,想获取某个类中的ThreadLocal静态对象的值、静态字段的值,如何获取呢?
1366 0
Arthas之Watch静态对象的值
|
Arthas 监控 IDE
谈谈阿里arthas背后的原理
谈谈阿里arthas背后的原理
谈谈阿里arthas背后的原理
|
Arthas SQL Java
arthas 高级玩法
redefine的class不能修改、添加、删除类的field和method,包括方法参数、方法名称及返回值; redefine命令和jad/watch/trace/monitor/tt等命令会冲突。执行完redefine之后,如果再执行上面提到的命令,则会把redefine的字节码重置。