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。

目录
相关文章
|
4天前
|
Arthas 监控 Java
Arthas (阿尔萨斯)arthas-boot 方式安装及使用教程
Arthas (阿尔萨斯)arthas-boot 方式安装及使用教程
446 0
|
4天前
|
Arthas Java 测试技术
Arthas基础使用篇
Arthas基础使用篇
|
5月前
|
Arthas Java 测试技术
arthas入门
arthas入门
91 1
|
6月前
|
Arthas Java 测试技术
神器Arthas使用小结
神器Arthas使用小结
46 0
|
7月前
|
Arthas Java 测试技术
Arthas 使用
Arthas 使用
40 0
|
8月前
|
Arthas Java 测试技术
Arthas 排查JVM问题总结
Arthas 排查JVM问题总结
236 0
Arthas 排查JVM问题总结
|
Arthas 监控 Java
JVM学习(四):Arthas的安装与使用
我们本身已经有Jvisualvm和Jprofiler,这两款工具在业界知名度也比较高,他们的优点是可以图形界面上看到各维度的性能数据,使用者根据这些数据进行综合分析,然后判断哪里出现了性能问题。
406 0
|
Arthas Java 测试技术
Arthas之ClassLoaderLoadClass
在使用Arthas排查线上问题的时候,有些时候我们需要查看某些类有没有被加载,或者这个类的静态成员变量到底有没有被打包,又或者需要load测试一下是否正常,那么这个时候可以通过本文进行相关的操作了。
305 0
Arthas之ClassLoaderLoadClass
|
Arthas 运维 Java
Arthas使用指南(上)
Arthas 是Alibaba开源的Java诊断工具。安装在系统所在服务器。可以帮助开发人员或者运维人员查找问题,分析性能,bug追踪。
548 0
|
Arthas 监控 Java
Arthas入门使用
Arthas常用方法入门介绍
443 0
Arthas入门使用