posts - 28,  comments - 15,  trackbacks - 0

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

    下面就以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

posted on 2007-03-26 15:19 zhangxl 阅读(660) 评论(0)  编辑  收藏 所属分类: ORM(包括hibernate等)

只有注册用户登录后才能发表评论。


网站导航:
 
<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(1)

随笔分类(17)

随笔档案(28)

文章分类(30)

文章档案(30)

相册

收藏夹(2)

hibernate

java基础

mysql

xml

关注

压力测试

算法

最新随笔

搜索

  •  

积分与排名

  • 积分 - 95498
  • 排名 - 602

最新评论

阅读排行榜

评论排行榜