利用P6SPY +SQL Profiler记录、统计web app对数据库的操作。

弄hibernate时,想显示sql语句,可以设置show_sql为true来达到这个目的,但是参数值全是像PreparedStatement一样,用?来代替的。
用p6spy可以达到显示的那些参数原值的目的,但可读性差。可以利用SQL Profiler来处理这个事情。
p6spy还可以用来记录web app访问DB的sql总数,从而更好的帮我们分析项目中的隐藏问题.
p6spy: http://www.p6spy.com
SQL Profile: http://www.jahia.net/jahia/page597.html

p6spy安装:
* 将p6spy.jar放到WEB-INF/lib目录下,将spy.properties放到WEB-INF/classes目录下。
* 修改你 原有 JDBC Driver为:com.p6spy.engine.spy.P6SpyDriver
* 修改 spy.properties 中的 realdriver 值为 原有 的JDBC Driver,比如我的是:com.mysql.jdbc.Driver
* 完成,运行web server。

我的日志记录产生在 %TOMCAT_HOME%\bin下,此log位置可以能过修改 logfile = x:\x_dir\spy.log 来控制
打开看看,看里面的日志是不是看起来比较不爽?下面我们安装SQL Profiler来让自已的视线爽一点。

SQL Profiler安装:(须p6spy成功安装)
* 将SQL Profiler自带的spy.properties覆盖原来的classes目录下文件
* 修改现在spy.properties中realdriver 值为 原有 的JDBC Driver
看后看看readme注意这几句 ^__^ :
1. Start the GUI
2. Start the webapp, in starts doing some JDBC requests we will ignore
3. Press the "reset" button on the GUI
4. Make a request to the webapp
5. Press the "pause" button after the request has finished executing
6. Press the "report" button to save profiling results as a CSV file

* 我们先用java -jar sqlprofiler.jar 运行 sql profiler
* 然后启动web server :-)

一切尽在眼前了吧?
当然,p6spy 和 sql profiler 能做的不止这些,sql profiler还能根据你的query来帮你生成建立合适的index功能等等。

快速Guide
Quick Start Notes:

to enable p6spy, we need to let p6spy intercept JDBC connection. In my spring's configuration xml file, I used below setting to simplify configuration:


<!-- change the original dataSource bean id to dataSourceTarget to be prepaired for p6spy proxying -->
<bean id="dataSourceTarget" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="PSTDS" />
</bean>

<!-- add below fragment to let p6spy to provide dataSource proxied -->
<bean id="dataSource" class="com.p6spy.engine.spy.P6DataSource">
<constructor-arg ref="dataSourceTarget"/>
</bean>


1) add p6spy.jar under p6spy folder and sqlprofiler.jar under sql-profiler folder to your application's classpath;

2) add spy.properties provided in current folder to classpath also;

3) run sqlprofiler.jar with command "java -jar sqlprofiler.jar"

4) start your web application to check whether it works(ensure sqlProfiler starts up before your web application to enable GUI-based analysis function);
你可以使用 p6spy 来保存 SQL数据库p6spy 是一个用于监控和记录 JDBC 驱动程序生成的 SQL 语句的工具。它提供了一个代理驱动程序,可以将 SQL 输出到日志文件或数据库中。 以下是一些步骤来配置 p6spy 来保存 SQL数据库: 1. 导入 p6spy 的依赖:在你的项目中添加 p6spy 的依赖,例如 Maven 中的以下代码片段: ``` <dependency> <groupId>p6spy</groupId> <artifactId>p6spy</artifactId> <version>3.9.1</version> </dependency> ``` 2. 配置数据库连接:在你的应用程序中,将数据库连接的驱动程序类名设置为 p6spy 的驱动程序类名。例如,对于 MySQL 数据库: ``` jdbc.driver=com.p6spy.engine.spy.P6SpyDriver ``` 3. 配置 p6spy:在你的应用程序的配置文件中,添加 p6spy 的配置项。例如,在 Spring Boot 应用程序的 `application.properties` 文件中添加以下配置: ``` spring.datasource.url=jdbc:p6spy:mysql://localhost:3306/mydatabase spring.datasource.driver-class-name=com.p6spy.engine.spy.P6SpyDriver spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect ``` 4. 配置日志输出:在 p6spy 的配置文件中,指定你想要将 SQL 输出日志保存到的目标位置,可以是日志文件或数据库。例如,在 Spring Boot 应用程序的 `application.properties` 文件中添加以下配置: ``` # 将 SQL 输出到文件 spy.logFile=/path/to/log/file.log # 将 SQL 输出到数据库 spy.appender=com.p6spy.engine.spy.appender.Slf4JLogger ``` 这样,p6spy 就会拦截应用程序生成的 SQL 并将其保存到指定的日志文件或数据库中。你可以根据需要调整 p6spy 的配置和日志输出方式。希望这对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值