`
MafiaDada
  • 浏览: 25772 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Grails, p6spy and Sql Profiler

阅读更多
There are several ways to have Grails log SQL statements that Hibernate is sending to the database under the covers of GORM.  The first is the loggingSql flag in DataSource.groovy:

loggingSql=true


This will show you the prepared statement that Hibernate is generating, like this:

SELECT
    this_.id AS id6_0_,
    this_.version AS version6_0_,
    this_.authority AS authority6_0_,
    this_.description AS descript4_6_0_
FROM
    role this_
WHERE
    this_.authority=?


This is a good first step – but what if I want to see the parameter that is being sent as well?  You could turn logging for the org.hibernate.type package to TRACE level:

//in Config.groovy
log4j = {
    trace  'org.hibernate.type'
}


YIKES! That gets REALLY noisy, really fast:

Hibernate:
    select
        this_.id as id6_0_,
        this_.version as version6_0_,
        this_.authority as authority6_0_,
        this_.description as descript4_6_0_
    from
        role this_
    where
        this_.authority=?
TRACE type.StringType  - binding 'ROLE_USER' to parameter: 1
TRACE type.LongType  - returning '1' as column: id6_0_
TRACE type.LongType  - returning '1' as column: version6_0_
TRACE type.StringType  - returning 'ROLE_USER' as column: authority6_0_
TRACE type.StringType  - returning 'Default user role' as column: descript4_6_0_


Besides, what I really want is the ability to copy and paste a query, with the parameters bound, directly into a SQL editor to execute the query and see the results myself. Enter the p6spy plugin for Grails:

grails install-plugin p6spy


Swap the driver in your DataSource.groovy file from your driver to the p6spy driver (the plugin will automatically add this, you just need to comment it in):

environments {
  development {
    dataSource {
      //driverClassName = "org.hsqldb.jdbcDriver"
      driverClassName = "com.p6spy.engine.spy.P6SpyDriver"
    }
  }
}


Fire up your Grails application – p6spy will create a spy.log file in your application directory and log a pipe delimited line – the last column has the actual SQL being executed, with the parameters!

select this_.id as id2_0_, this_.version as version2_0_, this_.authority as authority2_0_, this_.description as descript4_2_0_ from role this_ where this_.authority='ROLE_USER'


This is a great start, and most people stop here. But sometimes there are so many queries being executed that it’s difficult to wade through spy.log to find the one you’re looking for. Thanks to a tip from the excellent Grails In Action book that was recently released – SQL Profiler can help!

Download SQL Profiler (http://sourceforge.net/projects/sqlprofiler) and add these lines to grails-app/conf/spy.properties:

log4j.appender.SQLPROFILER_CLIENT=org.apache.log4j.net.SocketAppender
log4j.appender.SQLPROFILER_CLIENT.RemoteHost=localhost
log4j.appender.SQLPROFILER_CLIENT.Port=4445
log4j.appender.SQLPROFILER_CLIENT.LocationInfo=true


Add the appender to the logger definition:

log4j.logger.p6spy=INFO,STDOUT,SQLPROFILER_CLIENT


Finally, make sure the Log4j Appender is enabled:

#specifies the appender to use for logging
appender=com.p6spy.engine.logging.appender.Log4jLogger
#appender=com.p6spy.engine.logging.appender.StdoutLogger
#appender=com.p6spy.engine.logging.appender.FileLogger


Then, in the directory in which you downloaded the SQL Profiler jar, launch the GUI with this line:

java -jar sqlprofiler.jar


Now, p6spy will log SQL to the SQL Profiler GUI! You can use it to profile SQL statements, but you can also use the ‘Logger’ tab to filter the log. I click the little trash can icon in the upper left to clear the log, run a test in the application, then filter the results using the filter fields available. For instance, if I only want to see queries related to the ‘ROLE’ table, just put the word ‘role’ in the Filter message text box, and you’ll only see queries related to that table. It’s a great way to get straight at a particular SQL query when you’re drowning in tons and tons of SQL messages.

---------------------------------------------------------------------------------------------

如果遇到这个问题:
Exception in thread "Thread-2" java.lang.NumberFormatException: For input string
: "21:26:47"
        at java.lang.NumberFormatException.forInputString(Unknown Source)
        at java.lang.Long.parseLong(Unknown Source)
        at java.lang.Long.parseLong(Unknown Source)
        at org.jahia.sqlprofiler.QueryEntry.parseP6Encoding(QueryEntry.java:134)


那么屏蔽掉spy.properties文件里面这一行内容:
# sets the date format using Java's SimpleDateFormat routine
#dateformat=HH:mm:ss


参考文章:
http://www.piragua.com/2009/06/17/grails-p6spy-and-sql-profiler/
http://grails.1312388.n4.nabble.com/p6spy-slqprofiler-error-td1385711.html

分享到:
评论

相关推荐

    grails-profiler:Grails Profiler插件

    grails install-plugin profiler 如何使用它 插件安装完成后,您只需要将“ profiler = on”或“ profiler = 1”参数添加到请求中,即可对请求进行概要分析。 例如,要分析一个简单的GET请求: 您可能还看不到任何...

    Grails Persistence with GORM and GSQL

    Grails Persistence with GORM and GSQL

    Grails权威指南 Grails权威指南

    3. **GORM(Grails Object-Relational Mapping)**:Grails的内置ORM工具,允许开发者以声明式的方式操作数据库,支持SQL的CRUD操作,简化了数据持久化的过程。GORM支持多种数据库,如MySQL、PostgreSQL等。 4. **...

    Grails Grails Grails

    **Grails 框架详解** Grails 是一个基于 Groovy 语言的开源Web应用程序框架,它构建在Java平台之上,旨在简化开发过程并提高生产力。Grails 的设计深受Ruby on Rails的影响,提供了MVC(模型-视图-控制器)架构模式...

    Eclipse下搭建Grails项目

    【Grails项目搭建详解】 Grails是一个基于Groovy语言的开源Web应用框架,它简化了开发过程,尤其适合快速构建动态网站。在Eclipse中搭建Grails项目可能相对复杂,但通过以下步骤,即使是初学者也能顺利进行。 1. *...

    the definitive guide to grails 2

    通过GORM(Grails Object Relational Mapping),Grails提供了自动的ORM支持,使得开发者无需编写复杂的SQL语句即可实现数据的持久化操作。 #### Controllers(控制器) 控制器(Controllers)负责处理来自用户的...

    Grails企业web应用开发与部署

    使用Grails的profiler工具找出性能瓶颈。 4. 容器优化:根据应用负载调整Web容器配置,如增大最大连接数,优化线程池设置。 总之,Grails作为一款强大的企业Web开发框架,提供了高效、简洁的开发体验。通过熟练掌握...

    grails-用户手册

    《Grails用户手册》 Grails,作为一个基于Groovy语言的开源Web应用框架,深受开发者喜爱,它简化了Java开发的复杂性,提供了强大的MVC(Model-View-Controller)架构,以及丰富的插件系统。这份用户手册将帮助你...

    grails-2.4.4.zip

    GORM 提供了对数据库的操作接口,使得开发者可以通过面向对象的方式操作数据,减少了与SQL的直接交互,支持关系型数据库如MySQL、PostgreSQL等。 5. **Grails插件系统** Grails 的强大之处在于其丰富的插件库,如...

    Grails权威指南.pdf

    《Grails权威指南》这本书是针对Grails框架的深度解析之作,旨在帮助开发者全面掌握Grails的使用和开发技巧。Grails是一个基于Groovy语言的开源Web应用框架,它简化了Java开发流程,提供了丰富的功能和高效能。下面...

    eclipse开发grails插件

    对于Grails开发,我们需要的是Eclipse中的Grails插件,它能够提供对Grails项目的创建、运行、调试等一系列功能。 **Grails**是基于Groovy语言的全栈式Web开发框架,它借鉴了Ruby on Rails的设计理念,提供了快速...

    学习grails框架时候自己写的例子

    在这个例子中,你可能学会了如何在Grails中使用`groovy.sql.Sql`类或者HQL(Hibernate Query Language)进行定制化查询。 3. **分页功能**:在Web应用中,数据分页是很常见的需求。Grails提供了方便的分页API,可以...

    grails学习

    【Grails 学习知识点详解】 Grails 是一个基于 Groovy 语言的开源 Web 应用框架,它简化了开发过程并提供了丰富的功能。本文将深入探讨 Grails 中的一些常见问题及其解决方案,帮助你更好地理解和掌握 Grails 开发...

    grails ajax

    描述中的"javascript and ajax using in grails"强调了JavaScript在Grails应用中的重要性。JavaScript是实现Ajax交互的主要语言,通常用于处理用户交互和动态更新页面。Grails提供了与jQuery等流行JavaScript库集成...

    grails中文入门简介

    Grails是一个基于Groovy语言的全栈框架,它遵循约定优于配置的原则,并且紧密集成Spring和Hibernate等流行的Java库,简化了开发流程。Grails在IT行业中尤其受到重视,因为它能够帮助开发者快速搭建并部署基于MVC模式...

    Grails 中文参考手册

    《Grails 中文参考手册》是一本全面介绍Grails框架的指南,旨在帮助开发者快速上手并深入理解Grails的各个核心概念和技术。Grails是一个基于Groovy语言的开源Web应用框架,它提供了高效的开发环境和强大的功能,使得...

Global site tag (gtag.js) - Google Analytics