Logging SQL Statements in JBoss AS 7

If you are using JPA, it is highly recommended that you keep an eye out for the generated SQL statements so that you can avoid any poorly generated queries. In JBoss AS 7, take these steps to view the SQL statements.

First, in your persistence.xml, add the showSql parameter.

<persistence ...>
 <persistence-unit name="...">
 <property name="showSql" value="true"/>
 <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />

Next, open the server’s configuration file. For example, for standalone server, it will be JBOSS/standalone/configuration/standalone.xml. Locate the start of the logging subsystem by looking for this line:

<subsystem xmlns="urn:jboss:domain:logging:1.1">

Somewhere below that add:

<logger category="org.hibernate.SQL">
    <level name="TRACE"/>

Save the file and restart the server. Now, in Eclipse Console or in server’s server.log file, you will see all SQL statements executed by JPA. It will look something like this.

09:49:24,227 DEBUG [org.hibernate.SQL] (http-localhost- select product0_.id as id3_0_, product0_.description as descript2_3_0_, product0_.imageLink as imageLink3_0_, product0_.name as name3_0_, product0_.price as price3_0_, product0_.sku as sku3_0_ from Product product0_ where product0_.id=?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.