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=?

Configure JDK Logging in Tomcat

When developing from Eclipse, it is useful to increase the log level to say FINE. In this article, we will see how to do this. I tested this out using TomEE 1.5, but, this should work for Tomcat 7 also.


In the Servers view, double click tomcat server.


Click Open launch configuration.


Click Arguments tab.


Add these two arguments:


Save changes.

To set the log level of your own code, open TOMCAT_INSTALL/conf/logging.properties and add this line to the end:

com.mobiarch.test.level = FINE

This will set the log level for the com.mobiarch.test package to FINE.

Restart the server.

Now, your application code using JDK logging will start to show its messages in Eclipse console.

Logger logger = Logger.getLogger(getClass().getName());
logger.fine("Hello world");

Configuring a MySQL Data Source in TomEE

Today, I hit a major brick wall trying to define a data source in TomEE. The official documentation is a little outdated. I know that the developers at TomEE do a great job and do not always get the time to updating the documentation. I thought, I will post this tutorial to help them out.

Define the Data Source

Open <TOMEE>/conf/tomee.xml. Right below the root element <tomee> add:

<Resource id="TestDS" type="DataSource">
    JdbcDriver  com.mysql.jdbc.Driver
    JdbcUrl jdbc:mysql://localhost/test
    UserName    root
    Password    some_pass

Of course, settings for your database will be different.

The id attribute defines the resource name. So, to use the data source from code, you need to inject it like this:

DataSource ds;

The JNDI name will be java:openejb/Resource/TestDS. For example, this will setup a persistence.xml.


Copy the Driver JAR File

Copy the JDBC driver JAR file (mysql-connector-java-5.1.22-bin.jar) to <TOMEE>/lib folder.

Fix Eclipse Server

At this point, our data source is fully defined. But, if you start TomEE from Eclipse, it will not use the settings in tomee.xml. That is because, by default, it uses a different configuration folder. To fix the problem, copy the <TOMEE>/conf/tomee.xml file and paste it inside the Servers/<TOMEE> folder of your workspace.


Restart the server.

Serializing Date With Jackson

When serializing java.util.Date, Jackson gets the long time value by calling getTime() and outputs the time. For example a class:

public class Customer {
    private String id;
    private Date birthDay;
    //Getter and setter...

May be serialized as:

{"id":"1", "birthDay":1320987600000}

Working with date like this may be difficult for certain clients. How will you output formatted date, like “11-11-2011”? Most tutorials on the web, such as this one, recommend writing a custom serializer. A simpler approach is to provider a getter method that does the formatting. For example:

public String getBirthdayFormatted() {
   SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");

   return sdf.format(birthDay);

Now, the JSON will look like this:

{"id":"1", "birthDay":1320987600000, "birthdayFormatted":"11-11-2011"}

You can apply the same principle for formatting of currency, time or anything else. This approach, aside from its simplicity, has two added benefits:

  • The code is not using any Jackson specific solution.
  • We are outputting both the raw and the formatted values. Advanced clients can do their own formatting using the raw value.