---
title: "提升基于JDBC的应用程序访问数据库的效率"
author: "huqi"
date: "2007-11-18T22:51:00Z"
tags: ["JDBC", "SQL", "log4j", "企业应用", "网络应用"]
slug: "jdbc"
draft: false
lastmod: "2007-11-18T22:51:00Z"
id: 169471
url: "https://huqi.iteye.com/blog/169471"
---

一般来说，我们的JavaEE - JDBC企业应用程序的主要瓶颈之一就是在访问数据库，而数据库的瓶颈又主要在海量数据的查询上。如何解决这个问题？最简单的方法就是为数据库表建立精确的索引。

如何做到有效的建立索引？不妨利用P6Spy（[http://www.p6spy.com/](http://www.p6spy.com/)）及SQL Profiler（[http://www.jahia.net/jahia/page597.html](http://www.jahia.net/jahia/page597.html)）软件包针对应用程序对数据的访问进行动态监测，通过利用P6Spy代理数据库驱动程序记录数据的访问、收集分析应用程序使用的SQL语句，找出性能瓶颈，生成优化方案报告及建立索引的SQL脚本。

1. 获取 p6spy-install.jar （[http://www.p6spy.com](http://www.p6spy.com)）
2. 从 p6spy-install.jar 中得到p6spy.jar，这就是监测程序的软件包，将其加入您的用程序classpath中。
3. 将应用程序的数据库驱动改换成由P6Spy提供的代理驱动，在Spring中可以这样配置：
   
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"&gt;  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;!—这是你原来的数据库驱动：com.microsoft.jdbc.sqlserver.SQLServerDriver --&gt;  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;property name="driverClassName"&gt;  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;!--这是P6Spy的代理驱动--&gt;  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;value&gt;com.p6spy.engine.spy.P6SpyDriver&lt;/value&gt;  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;/property&gt;  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;property name="url"&gt;  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;value&gt;jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=sample&lt;/value&gt;  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;/property&gt;  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;property name="username"&gt;&lt;value&gt;sa&lt;/value&gt;&lt;/property&gt;  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;property name="password"&gt;&lt;value&gt;sa&lt;/value&gt;&lt;/property&gt;  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/bean&gt;
4. 修改 **spy.properties** 文件，在其中配置你真正的数据库驱动程序（详细下面说）。

通过以上的步骤，你就完成的P6Spy的配置，启动应用后，P6Spy就会对您的应用进行监测并产生相应的日志文件。

以下，我们将通过配置 SQL Profiler 对P6Spy的监测数据进行收集和分析： 

1. 获取 sqlprofiler-0.3-bin.zip（[http://www.jahia.net/jahia/page597.html](http://www.jahia.net/jahia/page597.html)）
2. 替代上面的步骤 4，从sqlprofiler-0.3-bin.zip中获取 **spy.properties** 配置文件，这就是P6Spy监测程序的配置文件，我们需要在这个文件中为代理配置真正的数据库驱动程序：
   
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)# oracle driver  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)# realdriver=oracle.jdbc.driver.OracleDriver  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)# mysql Connector/J driver  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)# realdriver=com.mysql.jdbc.Driver  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)# informix driver  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)# realdriver=com.informix.jdbc.IfxDriver  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)# ibm db2 driver  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)# realdriver=COM.ibm.db2.jdbc.net.DB2Driver  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)# the mssqlserver driver  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)realdriver=com.microsoft.jdbc.sqlserver.SQLServerDriver  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#specifies another driver to use  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)realdriver2=  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#specifies a third driver to use  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)realdriver3=
   
   最后，将这个文件放置在你应用程序classpath的根下即可。
3. 在spy.properties中，我们还需要配置更多的信息以收集数据，可以利用Log4j向网络上输出性能监测数据，这样Sql Profiler软件包就能收到数据并进行分析，当然，您要在配置文件中指定接收数据的网络监听地址和端口。
   
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#specifies the appender to use for logging  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)appender=com.p6spy.engine.logging.appender.Log4jLogger  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#appender=com.p6spy.engine.logging.appender.StdoutLogger  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#appender=com.p6spy.engine.logging.appender.FileLogger  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)# name of logfile to use, note Windows users should make sure to use forward slashes in their pathname (e:/test/spy.log) (used for file logger only)  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#logfile     = spy.log  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)# append to  the p6spy log file.  if this is set to false the  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)# log file is truncated every time.  (file logger only)  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)append=true  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#The following are for log4j logging only  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#log4j.appender.STDOUT.layout.ConversionPattern=p6spy - %m%n  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)log4j.appender.SQLPROFILER\_CLIENT=org.apache.log4j.net.SocketAppender  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)log4j.appender.SQLPROFILER\_CLIENT.RemoteHost=localhost  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)log4j.appender.SQLPROFILER\_CLIENT.Port=4445  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)log4j.appender.SQLPROFILER\_CLIENT.LocationInfo=true  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#log4j.logger.p6spy=INFO,STDOUT  
   ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)log4j.logger.p6spy=DEBUG, SQLPROFILER\_CLIENT
4. 从sqlprofiler-0.3-bin.zip中获取 **sqlprofiler.jar**，这是一个图形界面的应用程序，它被用来接收监测数据（来自上面配置的P6Spy）和生成分析结果。

至此，准备工作已经全部完成。执行分析的应用：

首先执行 **sqlprofiler.jar**（利用javaw），其在指定端口监听应用程序通过Log4j发来的监测数据。接下来就可以运行经过P6Spy代理的应用程序了。

在 SQL Profiler 的界面可以看到数据的采集。应用程序运行一段时间后（充分反映您应用程序的运行周期），点击“||”暂停按钮结束数据收集，接下来就是分析数据、生成解决方案、优化文件（索引脚本）。