SQL 耗时跟踪
来源:dev.to
2024-10-26 11:06:34
0浏览
收藏
对于一个文章开发者来说,牢固扎实的基础是十分重要的,golang学习网就来带大家一点点的掌握基础知识点。今天本篇文章带大家了解《SQL 耗时跟踪》,主要介绍了,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!
@Slf4j public class InspectSqlStackFilter extends FilterEventAdapter { private final Set<String> firstStacks = new ConcurrentHashSet<>(); @Override public boolean statement_execute(FilterChain chain, StatementProxy statement, String sql) throws SQLException { try { long startTime = System.currentTimeMillis(); boolean result = super.statement_execute(chain, statement, sql); doLogAsExecute(chain, statement, sql, System.currentTimeMillis() - startTime); return result; } catch (SQLException ex) { throw unwrapException(ex, chain, statement); } } @Override public boolean statement_execute(FilterChain chain, StatementProxy statement, String sql, int autoGeneratedKeys) throws SQLException { try { long startTime = System.currentTimeMillis(); boolean result = super.statement_execute(chain, statement, sql, autoGeneratedKeys); doLogAsExecute(chain, statement, sql, System.currentTimeMillis() - startTime); return result; } catch (SQLException ex) { throw unwrapException(ex, chain, statement); } } @Override public boolean statement_execute(FilterChain chain, StatementProxy statement, String sql, int[] columnIndexes) throws SQLException { try { long startTime = System.currentTimeMillis(); boolean result = super.statement_execute(chain, statement, sql, columnIndexes); doLogAsExecute(chain, statement, System.currentTimeMillis() - startTime); return result; } catch (SQLException ex) { throw unwrapException(ex, chain, statement); } } @Override public boolean statement_execute(FilterChain chain, StatementProxy statement, String sql, String[] columnNames) throws SQLException { try { long startTime = System.currentTimeMillis(); boolean result = super.statement_execute(chain, statement, sql, columnNames); doLogAsExecute(chain, statement, sql, System.currentTimeMillis() - startTime); return result; } catch (SQLException ex) { throw unwrapException(ex, chain, statement); } } @Override public int[] statement_executeBatch(FilterChain chain, StatementProxy statement) throws SQLException { try { long startTime = System.currentTimeMillis(); int[] result = super.statement_executeBatch(chain, statement); doLogAsExecute(chain, statement, System.currentTimeMillis() - startTime); return result; } catch (SQLException ex) { throw unwrapException(ex, chain, statement); } } @Override public ResultSetProxy statement_executeQuery(FilterChain chain, StatementProxy statement, String sql) throws SQLException { try { long startTime = System.currentTimeMillis(); ResultSetProxy result = super.statement_executeQuery(chain, statement, sql); doLogAsExecute(chain, statement, sql, System.currentTimeMillis() - startTime); return result; } catch (SQLException ex) { throw unwrapException(ex, chain, statement); } } @Override public int statement_executeUpdate(FilterChain chain, StatementProxy statement, String sql) throws SQLException { try { long startTime = System.currentTimeMillis(); int result = super.statement_executeUpdate(chain, statement, sql); doLogAsExecute(chain, statement, sql, System.currentTimeMillis() - startTime); return result; } catch (SQLException ex) { throw unwrapException(ex, chain, statement); } } @Override public int statement_executeUpdate(FilterChain chain, StatementProxy statement, String sql, int autoGeneratedKeys) throws SQLException { try { long startTime = System.currentTimeMillis(); int result = super.statement_executeUpdate(chain, statement, sql, autoGeneratedKeys); doLogAsExecute(chain, statement, sql, System.currentTimeMillis() - startTime); return result; } catch (SQLException ex) { throw unwrapException(ex, chain, statement); } } @Override public int statement_executeUpdate(FilterChain chain, StatementProxy statement, String sql, int[] columnIndexes) throws SQLException { try { long startTime = System.currentTimeMillis(); int result = super.statement_executeUpdate(chain, statement, sql, columnIndexes); doLogAsExecute(chain, statement, sql, System.currentTimeMillis() - startTime); return result; } catch (SQLException ex) { throw unwrapException(ex, chain, statement); } } @Override public int statement_executeUpdate(FilterChain chain, StatementProxy statement, String sql, String[] columnNames) throws SQLException { try { long startTime = System.currentTimeMillis(); int result = super.statement_executeUpdate(chain, statement, sql, columnNames); doLogAsExecute(chain, statement, sql, System.currentTimeMillis() - startTime); return result; } catch (SQLException ex) { throw unwrapException(ex, chain, statement); } } @Override public boolean preparedStatement_execute(FilterChain chain, PreparedStatementProxy statement) throws SQLException { try { long startTime = System.currentTimeMillis(); boolean result = super.preparedStatement_execute(chain, statement); doLogAsExecute(chain, statement, System.currentTimeMillis() - startTime); return result; } catch (SQLException ex) { throw unwrapException(ex, chain, statement); } } @Override public ResultSetProxy preparedStatement_executeQuery(FilterChain chain, PreparedStatementProxy statement) throws SQLException { try { long startTime = System.currentTimeMillis(); ResultSetProxy result = super.preparedStatement_executeQuery(chain, statement); doLogAsExecute(chain, statement, System.currentTimeMillis() - startTime); return result; } catch (SQLException ex) { throw unwrapException(ex, chain, statement); } } @Override public int preparedStatement_executeUpdate(FilterChain chain, PreparedStatementProxy statement) throws SQLException { try { long startTime = System.currentTimeMillis(); int result = super.preparedStatement_executeUpdate(chain, statement); doLogAsExecute(chain, statement, System.currentTimeMillis() - startTime); return result; } catch (SQLException ex) { throw unwrapException(ex, chain, statement); } } private SQLException unwrapException(SQLException ex, FilterChain chain, StatementProxy statement){ String message = String.format("\nurl -> %s\nsql -> %s\nerros->%s", chain.getDataSource().getUrl(), statement.getLastExecuteSql(), ex.getMessage()); if (ex instanceof CommunicationsException) { Throwable cause = ex.getCause(); if (cause instanceof SocketTimeoutException) { return new SQLException(message, cause); } else if (cause instanceof CJCommunicationsException) { if (cause.getCause() instanceof SocketTimeoutException) { return new SQLException(message, cause.getCause()); } } } return new SQLException(message, ex.getCause()); } private void doLogAsExecute(FilterChain chain, StatementProxy statement, long duration) { this.doLogAsExecute(chain, statement, null, duration); } public static void main(String[] args) { } private void doLogAsExecute(FilterChain chain, StatementProxy statement, String sql, long duration) { String applicationName = SpringContextHolder.getApplicationName(); if (duration < 1000) { return; } if ( applicationName.startsWith("example-live") || applicationName.startsWith("example-score") || applicationName.startsWith("example-news") || applicationName.startsWith("example-uc") || applicationName.startsWith("example-support") || applicationName.startsWith("example-sms") || applicationName.startsWith("example-integral") ) { LinkedList<String> lines = new LinkedList<>(); LinkedList<String> stacks = new LinkedList<>(); String firstStack = null; StackTraceElement[] stackTraces = Thread.currentThread().getStackTrace(); for (int i = 15; i < stackTraces.length - 1; i++) { StackTraceElement stackTrace = stackTraces[i]; String className = stackTrace.getClassName(); if (className.startsWith("com.example.base")) { continue; } if (className.startsWith("com.example")) { if (stackTrace.getLineNumber() == -1) { continue; } int lastPoint = className.lastIndexOf("."); String javaFileName = className.substring(lastPoint + 1); String line = String.format("%s.%s(%s.java:%s)", className, stackTrace.getMethodName(), javaFileName, stackTrace.getLineNumber()); if (firstStack == null) { firstStack = line; if (firstStacks.contains(firstStack)) { return; } firstStacks.add(firstStack); } lines.add("\t" + line); stacks.add(line); } if (className.startsWith("org.springframework.web")) { break; } } try { if (lines.isEmpty()) { return; } lines.addFirst("\nstackTraces: \t"); if (sql == null) { sql = statement.getLastExecuteSql(); } Map<Integer, JdbcParameter> parameters = statement.getParameters(); if (!parameters.isEmpty()) { Map<Integer, Object> hashMap = new HashMap<>(); for (Map.Entry<Integer, JdbcParameter> entry : parameters.entrySet()) { hashMap.put(entry.getKey(), entry.getValue().getValue()); } lines.addFirst("parameters: " + hashMap); } if (sql != null) { sql = SQLUtils.format(sql, DbType.mysql); lines.addFirst("sql: " + sql); SpringContextHolder.publishEvent(new InspectSqlEvent(sql.replaceAll("\\s+", " "), stacks)); } lines.addFirst("url: " + chain.getDataSource().getUrl()); lines.addFirst("duration: " + duration + "ms"); lines.addLast("\n\n-----------------------dividing---------------------\n\n"); File file = new File("sql_trace_info.txt"); FileUtils.writeLines(file, "UTF-8", lines, true); } catch (IOException ignore) { } } } }
以上就是《SQL 耗时跟踪》的详细内容,更多关于的资料请关注golang学习网公众号!
版本声明
本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除

- 上一篇
- Golang 可变参数列表与数组或切片有何不同?

- 下一篇
- PHP 中函数闭包:揭秘其力量
查看更多
最新文章
-
- 文章 · java教程 | 3小时前 |
- JUnit5单元测试入门与实战指南
- 131浏览 收藏
-
- 文章 · java教程 | 3小时前 | 开发效率 模式匹配 instanceof Java17 switch表达式
- Java17模式匹配优化,提升开发效率
- 400浏览 收藏
-
- 文章 · java教程 | 3小时前 |
- AndroidImageView锚点缩放实现技巧
- 398浏览 收藏
-
- 文章 · java教程 | 3小时前 |
- Java连接TDengine数据库方法详解
- 340浏览 收藏
-
- 文章 · java教程 | 3小时前 |
- TeenTalk程序无限循环修复方法
- 421浏览 收藏
-
- 文章 · java教程 | 3小时前 |
- Java序列化漏洞深度解析
- 406浏览 收藏
-
- 文章 · java教程 | 4小时前 |
- Java处理天文图像与FITS数据技巧
- 170浏览 收藏
-
- 文章 · java教程 | 4小时前 |
- Java中URL与URLConnection的使用详解
- 214浏览 收藏
-
- 文章 · java教程 | 4小时前 |
- SpringMVCRESTfulAPI设计全攻略
- 283浏览 收藏
-
- 文章 · java教程 | 4小时前 |
- JavaSPI机制详解:服务发现与应用解析
- 344浏览 收藏
-
- 文章 · java教程 | 5小时前 |
- SpringBoot整合GraphQL查询教程
- 300浏览 收藏
-
- 文章 · java教程 | 5小时前 |
- GluonMobile控制安卓音量教程
- 183浏览 收藏
查看更多
课程推荐
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 511次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 498次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
查看更多
AI推荐
-
- 千音漫语
- 千音漫语,北京熠声科技倾力打造的智能声音创作助手,提供AI配音、音视频翻译、语音识别、声音克隆等强大功能,助力有声书制作、视频创作、教育培训等领域,官网:https://qianyin123.com
- 192次使用
-
- MiniWork
- MiniWork是一款智能高效的AI工具平台,专为提升工作与学习效率而设计。整合文本处理、图像生成、营销策划及运营管理等多元AI工具,提供精准智能解决方案,让复杂工作简单高效。
- 193次使用
-
- NoCode
- NoCode (nocode.cn)是领先的无代码开发平台,通过拖放、AI对话等简单操作,助您快速创建各类应用、网站与管理系统。无需编程知识,轻松实现个人生活、商业经营、企业管理多场景需求,大幅降低开发门槛,高效低成本。
- 191次使用
-
- 达医智影
- 达医智影,阿里巴巴达摩院医疗AI创新力作。全球率先利用平扫CT实现“一扫多筛”,仅一次CT扫描即可高效识别多种癌症、急症及慢病,为疾病早期发现提供智能、精准的AI影像早筛解决方案。
- 198次使用
-
- 智慧芽Eureka
- 智慧芽Eureka,专为技术创新打造的AI Agent平台。深度理解专利、研发、生物医药、材料、科创等复杂场景,通过专家级AI Agent精准执行任务,智能化工作流解放70%生产力,让您专注核心创新。
- 213次使用
查看更多
相关文章
-
- 提升Java功能开发效率的有力工具:微服务架构
- 2023-10-06 501浏览
-
- 掌握Java海康SDK二次开发的必备技巧
- 2023-10-01 501浏览
-
- 如何使用java实现桶排序算法
- 2023-10-03 501浏览
-
- Java开发实战经验:如何优化开发逻辑
- 2023-10-31 501浏览
-
- 如何使用Java中的Math.max()方法比较两个数的大小?
- 2023-11-18 501浏览