J2EE应用监控后台执行SQL

时间:2023-03-08 16:36:14

我们可能已经很熟悉在未使用数据库连接池的hibernate的环境下,配置p6spy和sql profiler.这在单独使用hibernate,以及项目初期是有效的.但是,在真实的开发环境下,往往是项目进行到中后期时,才考虑到优化系统的性能,才考虑优化sql和数据库设计,而且在很多的项目中都会采用数据库连接池.为了减少配置文件在jdbc和datasource之间的配置切换,能不能考虑在使用数据库连接池的hibernate环境下配置p6spy和sqlfiler呢?

配置p6spy  sqlprofiler

下面就以spring,hibernate环境为例来说明在使用数据库连接池情况下配置p6spy和sql profiler:

1.下载p6spy和sqlprofiler
        http://www.p6spy.com
         http://www.jahia.org

   2.把解压后得到的p6spy.jar加入项目文件的CLASSPATH.
   
    3.把解压sql profiler后得到的spy.properties放入运行环境的根目录(Eclipse中,将其置于src目录节点之下),修改spy.properties如下:
                              # the MySQL open source driver
                              realdriver=net.sourceforge.jtds.jdbc.Driver        //这里填写你用的实际数据库驱动
   
     4.
配置spring文件
         <bean id="dataSource"
              class="org.apache.commons.dbcp.BasicDataSource"
              destroy-method="close">
                    <property name="driverClassName" value="${jdbc.driverClassName}" />
                    <property name="url" value="${jdbc.url}" />
                    <property name="username" value="${jdbc.username}" />
                      <property name="password" value="${jdbc.password}" />
         </bean>
          <bean id="myDataSource" class="com.p6spy.engine.spy.P6DataSource" 
                                                  destroy-method="close">
                  <constructor-arg>
                         <ref local="dataSource"/>
                    </constructor-arg>
           </bean>

<!--Hibernate SessionFatory-->
          <bean id="sessionFactory"
                 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
                 <property name="dataSource" ref="myDataSource" />
                 <property name="mappingDirectoryLocations">
                  <list>
                      <value>
                          classpath:/com/got/caip/domain/
                      </value>
                  </list>
                 </property>
                 <property name="hibernateProperties">
                     <props>
                         <prop key="hibernate.dialect">
                             org.hibernate.dialect.SQLServerDialect
                         </prop>
                         <prop key="hibernate.show_sql">
                                ${hibernate.show_sql}
                         </prop>
                         <prop key="hibernate.cache.use_query_cache">
                                ${hibernate.cache.use_query_cache}
                          </prop>
                   <prop key="hibernate.cache.provider_class">
                          ${hibernate.cache.provider_class}
                   </prop>
                   <prop key="hibernate.jdbc.batch_size">
                       20
                  </prop>
                   <prop key="net.sf.ehcache.configurationResourceName">
                          /ehcache.xml
                   </prop>
                  </props>
                   </property>
             </bean>

      5.通过Java -jar sqlprofiler启动sql profiler