Detailed logging output simplifies the debugging of a typical Java Platform, Enterprise Edition (Java EE) application, for example, a three-tier application that is deployed in an application server and stores data in a relational database management system (RDBMS). By integrating Hibernate 4, Apache Log4j, and GlassFish Server with the Eclipse integrated development environment (IDE) for Java EE developers, you can obtain detailed logging output from an application that you are developing. Hibernate obtains basic SQL logging output. Apache Log4j obtains detailed logging output. The Eclipse IDE displays a concise form of the individual log entries, omitting extraneous information in the raw server.log file.

A sample application is provided to demonstrate the logging output that you can obtain by integrating these technologies.


Note - Because of class loading limitations with Hibernate, you must use Hibernate 4.


This tutorial addresses the following topics:

Suggested Audience

This tutorial is for software developers who are developing Java EE applications for deployment in GlassFish Server. This tutorial assumes that you are familiar with all the technologies that are used in the tutorial.

Downloading, Installing, and Configuring Required Software

Before you begin, download, install, and configure the required software.

  1. Ensure that Java Development Kit (JDK) 6 is installed and that paths and environment variables for the JDK are correct. If the JDK and the Java Runtime Environment (JRE) are installed, ensure that the path to the JDK precedes the path to the JRE in your path statement. Otherwise, GlassFish Server might use a JRE for its Java virtual machine (VM) and steps for compiling JavaServer Pages (JSP) technology pages will fail.
  2. Download and install Eclipse IDE for Java EE developers.
  3. Download and install GlassFish Server Open Source Edition 3.1.2.
  4. Download Apache Log4j and extract the log4j-1.2.x.jar file to the glassfish3/glassfish/lib folder.
  5. Download Hibernate 4 and extract all JAR files in the following folders to the glassfish3/glassfish/lib folder:
    • hibernate-release-4.x.x.Final/lib/jpa
    • hibernate-release-4.x.x.Final/lib/required

      Notes -

    • The Hibernate 4 libraries are distributed in a set of folders. However, you must copy the JAR files directly into the glassfish3/glassfish/lib folder without preserving this folder structure.
    • To use use other features of Hibernate, such as 2nd-level caching, extract the JAR files in all folders under hibernate-release-4.x.x.Final/lib.
    • If you are running multiple domains, extract the Hibernate libraries and Log4j library to the glassfish3/glassfish/domains/domain1/lib folder.
  6. Download and install the GlassFish Server plugin for Eclipse IDE. When you add an update site, specify the location as http://download.java.net/glassfish/eclipse/indigo and not the location that is stated in the instructions.
  7. Register your installation of GlassFish Server with Eclipse IDE. When you select the server type, select GlassFish 3.1.2 and not the type that is stated in the instructions.
  8. Change the global preferences for GlassFish Server to meet the requirements of your application. If your application uses the Java DB database that is bundled with GlassFish Server, select all options on the Preferences page as shown in the following figure.

Creating a Project in Eclipse IDE

Before you start to develop your application, create a suitable Eclipse IDE project for developing Java EE applications.

Setting Up the Database and Data Source

Exactly how to set up the database and data source depends on the database that your application uses. For detailed instructions, see Administering Database Connectivity in Oracle GlassFish Server Administration Guide.

Specifying Hibernate as the Persistence Provider

By default, GlassFish Server Open Source Edition uses EclipseLink as the persistence provider. To specify Hibernate as the persistence provider, you must explicitly declare the persistence provider in the persistence.xml file. The persistence.xml file is a descriptor file that defines one or more persistence units in an enterprise application that uses entities.

Declare the persistence provider as follows:

  1. Add a persistence-unit element.
  2. Set the name attribute of the persistence-unit element to a unique name in the current scoped class loader.
  3. Add a provider element inside the persistence-unit element.
  4. In the provider element, specify the fully qualified Java class name of the underlying Java Persistence API (JPA) EntityManager implementation, namely org.hibernate.ejb.HibernatePersistence.

The element <property name="hibernate.show-sql"  value="true"/> does not affect the SQL logging output. Therefore, you can omit this element.

The following example persistence.xml file declares a persistence unit that is named BankServiceCtx for which the persistence provider is org.hibernate.ejb.HibernatePersistence.

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
    <persistence-unit name="BankServiceCtx" transaction-type="JTA">
     <provider>org.hibernate.ejb.HibernatePersistence</provider>
     <jta-data-source>jdbc/__default</jta-data-source>
         <properties>
          <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
          <property name="hibernate.hbm2ddl.auto" value="create"/>
          <!-- <property name="hibernate.show-sql"  value="true"/> -->
        </properties>
    </persistence-unit>
</persistence>

Configuring Hibernate to Obtain Basic SQL Logging Output

  1. Create the hibernate.properties file in the source folder of your project. In the sample application, this folder is the ejbModule folder.
  2. Add the following lines to the hibernate.properties file:
    hibernate.show_sql=true
    hibernate.format_sql=true

Configuring Log4j To Obtain Detailed Logging Output

Before you begin, ensure that the glassfish3/glassfish/lib folder in your GlassFish Server installation contains the Log4j library file log4j-1.2.x.jar.

To obtain detailed SQL logging output for the org.hibernate.ejb.HibernatePersistence persistence provider, configure Log4j to trace all packages whose name starts with org.hibernate:

  1. From the Hibernate 4 distribution file, extract the log4j.properties file to the source folder of your project. In the sample application, this folder is the ejbModule folder.

    Note - The log4j.properties file is in the Hibernate 4 distribution file, not the Apache Log4j distribution file.


  2. Verify in the extracted log4j.properties file that log4j.logger.org.hibernate is set to debug:
    log4j.logger.org.hibernate=debug

The complete log4j.properties file is as follows:

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootLogger=warn, stdout

#log4j.logger.org.hibernate=info
log4j.logger.org.hibernate=debug

### log HQL query parser activity
#log4j.logger.org.hibernate.hql.ast.AST=debug

### log just the SQL
#log4j.logger.org.hibernate.SQL=debug

### log JDBC bind parameters ###
log4j.logger.org.hibernate.type=info
#log4j.logger.org.hibernate.type=debug

### log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl=debug

### log HQL parse trees
#log4j.logger.org.hibernate.hql=debug

### log cache activity ###
#log4j.logger.org.hibernate.cache=debug

### log transaction activity
#log4j.logger.org.hibernate.transaction=debug

### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=debug

### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace

Using the Sample Application

The sample application is a three-tier application that stores data that a user enters in persistent storage, namely a database.

The web tier consists of a very simple form that invokes a basic servlet to process the data. The web tier accesses only the session beans.

Within the enterprise bean tier, the session beans use Java persistence entities. The entities access the database tables that store the entity states. A plain old Java object (POJO) with some annotations specifies how JPA is to store the data persistently. Hibernate 4 complies fully with the JPA 2.0 specification. The EntityManager implementation creates the tables in the database.

The application uses the JavaDB database that is bundled with Glassfish Server. You can set up this database by using the the GlassFish Server Administration Console without using the command line.

The container controls transactions by using the using the jdbc/__default DataSource object. This object is specified in the jta-data-source element of the persistence.xml file as follows:

<jta-data-source>jdbc/__default</jta-data-source>

Using the sample application involves the following tasks:

To Convert the Sample Application to an Eclipse IDE Project

The sample application is distributed as an enterprise application archive (EAR) file. To use this application, you must convert it to an Eclipse IDE project.

  1. Download the application EAR file .
  2. Import the application EAR file into Eclipse IDE.
  3. Set the properties of the SqlLoggingWeb project to add the SqlLoggingEjb project to the Java build path.

To Modify the DerbyPool Connection Pool

The sample application uses the preconfigured DerbyPool connection pool for connections to the database. To enable the sample application to use this pool, you must modify the preset values of some properties of the pool.

Before you begin, ensure that the option to start the Java DB process with GlassFish Server is set in the global preferences for the GlassFish Server.

  1. Start GlassFish Server in Eclipse IDE. The JavaDB database that is bundled with GlassFish Server is also started.
  2. In the Servers view, select and right-click GlassFish Server.
  3. From the pop-up menu that opens, choose GlassFish→View Admin Console. The GlassFish Server Administration Console is opened in the system browser.
  4. In the navigation tree, under the Resources node, expand the JDBC node.
  5. Under the JDBC node, select the JDBC Connection Pools node. The Connection Pools page opens.
  6. In the table of connection pools, click DerbyPool. The Edit Connection Pool page opens.
  7. On the Edit Connection Pool page, click the Additional Properties tab. The Edit Connection Pool Properties page opens.
  8. Edit the properties as follows:
    Property Value
    User user
    Password user
    DatabaseName newdb

    The following figure shows the Edit Connection Pool Properties page after these properties have been edited.

  9. Click Save.
  10. Confirm that the connection to the pool is usable.
    1. On the Edit Connection Pool Properties page, click the General tab. The Edit Connection Pool page opens.
    2. On the Edit Connection Pool page, click Ping.
  11. Confirm that the JNDI name of the connection pool is correct.
    1. In the navigation tree, under the Resources node, expand the JDBC node.
    2. Under the JDBC node, select the JDBC Resources node. The JDBC Resources page opens.
    3. On the JDBC Resources page, select jdbc/__default. DerbyPool is associated with this JNDI name as shown in the following figure.

To Run the Sample Application

  1. In the Project Explorer view, select and right-click the SqlLogging project.
  2. From the pop-up menu that opens, select Run As→Run on Server. The Run on Server wizard opens.
  3. Ensure that the option to choose an existing server is selected.
  4. From the list of servers, select GlassFish Server and click Next. The Add and Remove page opens.
  5. Confirm that SqlLogging is listed under the configured projects.
  6. Click Finish.

To View the SQL Logging Output

Viewing the SQL logging output involves viewing the server.log file in Eclipse IDE.

  1. In the Servers view, select and right-click GlassFish Server.
  2. From the pop-up menu that opens, choose GlassFish→View Log File.
    The SQL logging output is displayed in Eclipse IDE as shown in the following figure.

Further Reading

For more information about the technologies in this tutorial, see the following documentation: