Hibernate and Struts

You can find a simple example application in the project HibernateStruts. The application follows best practices and the approach can be reused in real projects. The application makes of of Ajax technology, displays a list of hedgehogs, provides paging and allows to create and edit hedgehog information. I used PostgreSQL but you may replace the database if you like.

I used the following frameworks:

The application demonstrates

Optimistic locking

Optimistic locking means that when you update an object, Hibernate checks if this object was not already changed by another user. This approach is described in chapter TODO: Referenz nicht gefunden. If the data was already changed, Hibernate throws a StaleObjectException. In this case we have to apply the normal exception handling – rollback transaction, close the session – and display the user an appropriate error message.

I propose to do this directly in the service class HedgehogServiceImp. Have a look in the method saveOrUpdateHedgehog. After the exception handling the exception is thrown again to allow handling in our Struts action method.

The Struts action class HedgehogAction adds an error message to the request and sends the user back to the input form. You can test the behaviour by opening two browser windows, editing the same hedgehog in both windows and than saving them.

In contrast to other RuntimeException which are normally fatal, we can offer the user a little bit more comfort, if we handle the StaleObjectException this way. The next chapter explains how to handle other Hibernate RuntimeExceptions.

Exception handling

If an exception happens it is important to rollback a open transaction and to close the Hibernate session. If we use the CurrentSessionContext (see chapter Session Handling and Transactions Chapter 13, Session and Transaction Handling ) the closing of the session is handled by Hibernate, if we call commit or rollback.

<property name="current_session_context_class">thread</property>

I created a Struts ExceptionHandler, which is called if a RuntimeException happens in Struts. This exception handler will rollback the transaction and display a general exception page.

First we have to configure the exception handler in the Struts configuration file struts-config.xml.

   <exception type="java.lang.RuntimeException"
      key="errors.severe" path="/error.jsp">

The class SevereExceptionHandler is my ExceptionHandler rolling back the transaction.

try {

   Transaction transaction = InitSessionFactory

   if (transaction.isActive())
   log.debug("Rolled back transaction after exception.");

} catch (RuntimeException e) {
   log.error("Error rolling back transaction");

If your database is not available or another exception happens, you will see an error page now. My example uses the C3P0 connection pool. This pools tries to reach the database a number of times before throwing an exception. If you test the exception handling by shutting down the database, you will have to wait a moment before getting the exception page.