测量Java应用程序中的sql执行时间 [英] Measure sql execution time in a Java Application

查看:639
本文介绍了测量Java应用程序中的sql执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法可以测量JDBC执行的所有sql语句的执行时间并将结果打印到输出中?

Is there a easy way to measure execution time of all sql statements that are executed by JDBC and print the result to the output?

有些人可能会建议我使用AOP要做到这一点,但我试图尽可能避免这种情况。还有另一种方法吗?

Some may advise me to use AOP to do this but I'm trying to avoid this if possible. Is there another way?

推荐答案

如果您没有在为您提供数据源的应用程序服务器中运行该应用程序,您会发现 log4jdbc 项目非常有用。项目提供的 jdbc.sqltiming 记录器将允许您记录执行的SQL语句的执行。

If you are not running the application in an application server that provides you a DataSource, you would find the log4jdbc project to be useful. The jdbc.sqltiming logger provided by the project will allow you to record the execution of the SQL statements executed.

您可以在依赖于DataSources的应用程序中使用此功能,通过包装从DataSource返回的连接在ConnectionSpy对象中。这需要更改您的代码库。

You could use this in an application that relies on DataSources, by wrapping the connection returned from the DataSource in a ConnectionSpy object. This would require changes in your codebase.

当然,在撰写本文时还有其他选项:

There are of course, other options available the time of writing this:


  • 仍可在大多数应用程序服务器中使用的 P6Spy项目。虽然确实过时了(并且被一些人认为已经放弃),但它绝不会过时。

  • JAMon API允许监视执行的SQL命令的执行时间。这需要使用JAMon API来监控连接。

  • the P6Spy project that can still be used in most application servers. Although certainly dated (and considered abandoned by some), it is by no means obsolete.
  • the JAMon API allows for monitoring of execution time for SQL commands executed. This would require using the JAMon API to monitor the connection.

这篇关于测量Java应用程序中的sql执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆