You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Gary was so kind to provide us a non-IoC Hibernate 'Open Session in View'-interceptor. Rather than having XWork or Spring doing the dependency injection, he sets up the Hibernate Session himself.

/*
  * HibernateOpenSessionInViewInterceptor.java
  *
  * Created on March 18, 2006, 3:51 PM
  *
  * To change this template, choose Tools | Template Manager
  * and open the template in the editor.
  */
 
package edu.washington.javawebdevelopment.webwork.interceptor;
 
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.AroundInterceptor;
import edu.washington.javawebdevelopment.dao.DaoFactoryHibernate;
import javax.servlet.ServletException;
import org.hibernate.SessionFactory;
import org.hibernate.StaleObjectStateException;
 
/**
  *
  * @author gary
  */
public class HibernateOpenSessionInViewInterceptor extends AroundInterceptor {
     private SessionFactory hibernateSessionFactory;
 
     public void init() {
         System.out.println("Initializing HibernateOpenSessionInViewInterceptor interceptor, obtaining Hibernate SessionFactory from DaoFactoryHibernate");
         hibernateSessionFactory = DaoFactoryHibernate.getSessionFactory();
     }
 
     public void destroy() {
     }
 
     public void before(ActionInvocation invocation) throws Exception {
         System.out.println("Starting a database transaction in the HibernateOpenSessionInViewInterceptor");
         hibernateSessionFactory.getCurrentSession().beginTransaction();
     }
 
     public void after(ActionInvocation invocation, String result) throws Exception {
         // Commit and cleanup
         try {
             System.out.println("Committing the database transaction in the HibernateOpenSessionInViewInterceptor");
             hibernateSessionFactory.getCurrentSession().getTransaction().commit();
         } catch (StaleObjectStateException staleEx) {
             System.err.println("This interceptor does not implement optimistic concurrency control!");
             System.err.println("Your application will not work until you add compensation actions!");
             // Rollback, close everything, possibly compensate for any permanent changes
             // during the conversation, and finally restart business conversation. Maybe
             // give the user of the application a chance to merge some if his work with
             // fresh data... what you do here depends on your applications design.
             throw staleEx;
         } catch (Throwable ex) {
             // Rollback only
             ex.printStackTrace();
             try {
                 if (hibernateSessionFactory.getCurrentSession().getTransaction().isActive()) {
                     System.out.println("Trying to rollback database transaction after exception");
                     hibernateSessionFactory.getCurrentSession().getTransaction().rollback();
                 }
             } catch (Throwable rbEx) {
                 System.err.println("Could not rollback transaction after exception! - " + rbEx);
             }
 
             // Let others handle it... maybe another interceptor for exceptions?
             throw new ServletException(ex);
         }
     }
}
  • No labels