Thread: logging statements from hibernate to valid SQL

logging statements from hibernate to valid SQL

From
Willy-Bas Loos
Date:
Hi,
I'm trying to log statements that are being fired from hibernate, so that i can benchmark them without hibernate itself.
I want to fire the SQL at the database directly and see what difference i can make by tweaking things.

But when i set log_statement = 'all', the logging looks like this:

2010-02-09 00:12:32 CET LOG:  execute <unnamed>: select nextval ('schema.sequence')
2010-02-09 00:12:32 CET LOG:  execute <unnamed>: insert into schema.tabe (field1, field2, field3, ...) values ($1, $2, $3, ...)
2010-02-09 00:12:32 CET DETAIL:  parameters: $1 = '1', $2 = NULL, $3 = '654767', ...)

Now, how can i get these $1 etc values to be replaced with their values, so that i can produce valid SQL?
Is there a postgresql.conf setting, or maybe some perl script that goes around?
(of course, it's more than just 3 lines.... I can't do it by hand.)

thx!

WBL

--
"Patriotism is the conviction that your country is superior to all others because you were born in it." -- George Bernard Shaw

Re: logging statements from hibernate to valid SQL

From
Andy Dale
Date:
Hi,

I would consider telling Hibernate to log the SQL it is generating to a file. This can be done by setting the logging category org.hibernate.SQL to debug, and for the parameters used in the prepared statements I think you must also enable org.hibernate.type on debug (I have not managed to get this working under JBoss though).

The produced output should look something like so:

2010-02-10 08:04:18,726 DEBUG [org.hibernate.SQL]
    /* named HQL query MessagingSession.findMessages */ select
        message0_.ID_ as col_0_0_
    from
        JBPM_MESSAGE message0_
    where
        message0_.DESTINATION_=?
        and message0_.ISSUSPENDED_<>true
        and (
            message0_.EXCEPTION_ is null
        )

Cheers,

Andy


On 9 February 2010 11:00, Willy-Bas Loos <willybas@gmail.com> wrote:
Hi,
I'm trying to log statements that are being fired from hibernate, so that i can benchmark them without hibernate itself.
I want to fire the SQL at the database directly and see what difference i can make by tweaking things.

But when i set log_statement = 'all', the logging looks like this:

2010-02-09 00:12:32 CET LOG:  execute <unnamed>: select nextval ('schema.sequence')
2010-02-09 00:12:32 CET LOG:  execute <unnamed>: insert into schema.tabe (field1, field2, field3, ...) values ($1, $2, $3, ...)
2010-02-09 00:12:32 CET DETAIL:  parameters: $1 = '1', $2 = NULL, $3 = '654767', ...)

Now, how can i get these $1 etc values to be replaced with their values, so that i can produce valid SQL?
Is there a postgresql.conf setting, or maybe some perl script that goes around?
(of course, it's more than just 3 lines.... I can't do it by hand.)

thx!

WBL

--
"Patriotism is the conviction that your country is superior to all others because you were born in it." -- George Bernard Shaw

Re: logging statements from hibernate to valid SQL

From
Willy-Bas Loos
Date:
On Wed, Feb 10, 2010 at 8:52 AM, Andy Dale <andy.dale@gmail.com> wrote:
Hi,

I would consider telling Hibernate to log the SQL it is generating to a file. This can be done by setting the logging category org.hibernate.SQL to debug, and for the parameters used in the prepared statements I think you must also enable org.hibernate.type on debug (I have not managed to get this working under JBoss though).

The produced output should look something like so:

2010-02-10 08:04:18,726 DEBUG [org.hibernate.SQL]
    /* named HQL query MessagingSession.findMessages */ select
        message0_.ID_ as col_0_0_
    from
        JBPM_MESSAGE message0_
    where
        message0_.DESTINATION_=?
        and message0_.ISSUSPENDED_<>true
        and (
            message0_.EXCEPTION_ is null
        )

Cheers,

Andy

Hi Andy, thanks for replying.
I tried logging from hibernate before i tried in postgres logging, but there the values are replaced with question marks.
I tried what you suggested (added   <logger name="org.hibernate.SQL" level="debug"/> and   <logger name="org.hibernate.type" level="debug"/> to logback.xml)
but there are no messages from org.hibernate.type in the log, and there are still question marks instead of values.

2010-02-10 09:44:04,228 DEBUG org.hibernate.SQL:401 - select nextval ('schema.sequence')
2010-02-10 09:44:04,231 DEBUG org.hibernate.SQL:401 - insert into schema.tabe (field1, field2, field3, ...) values (?, ?, ?, ...)
The actual values are not even logged at all. I'm looking to improve that somehow, but to no success so far.

So i was hoping that i could use postgresql logging to catch the SQL that i need..

Cheers,

WBL

--
"Patriotism is the conviction that your country is superior to all others because you were born in it." -- George Bernard Shaw