Find example code below for an extension of the ExecuteAndWaitInterceptor.

The goal of this code is to allow a background process to execute while having access to the same open Hibernate session object.

The SessionFactory dependency is injected into the OpenSessionExecuteAndWaitInterceptor by Spring. You may use other methods of dependency injection if you are more comfortable with them. By overriding the getNewBackgroundProcess() method, this interceptor uses our custom OpenSessionBackgroundProcess instead of the WebWork default.

Overriding the beforeInvocation() and afterInvocation() methods in the OpenSessionBackgroundProcess ensure that the session will stay open throughout the life of the background process, and any Spring transaction management will also be used.

As this code is heavily dependent on Spring and Hibernate, you shouldn't expect to see it packaged with a WebWork distribution. It does, however, serve as a useful example of extending the Execute and Wait Interceptor

OpenSessionExecuteAndWaitInterceptor.java
import net.sf.hibernate.SessionFactory;

import com.opensymphony.webwork.interceptor.BackgroundProcess;
import com.opensymphony.webwork.interceptor.ExecuteAndWaitInterceptor;
import com.opensymphony.xwork.ActionInvocation;


/**
 * The OpenSessionExecuteAndWaitInterceptor will obtain a Hibernate
 * Session Factory from a Spring.
 * 
 * The session factory will then be passed to the BackgroundProcess,
 * to open a session, enable Spring's transaction management 
 * capabilities, and bind the Session to the background thread.
 * 
 */
public class OpenSessionExecuteAndWaitInterceptor extends ExecuteAndWaitInterceptor {
       
    SessionFactory sessionFactory;

    
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}


	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	protected BackgroundProcess getNewBackgroundProcess(String arg0, ActionInvocation arg1, int arg2) {
		return new OpenSessionBackgroundProcess(arg0, arg1, arg2, sessionFactory);
	}

}
OpenSessionBackgroundProcess.java
public class OpenSessionBackgroundProcess extends BackgroundProcess {

	SessionFactory sessionFactory;
	Session openSession;
        protected boolean initializationComplete = false;
        private Object lock = new Object(); // used for synchronization

	public OpenSessionBackgroundProcess(String name,
			ActionInvocation invocation, int threadPriority,
			SessionFactory factory) {
		super(name, invocation, threadPriority);
		this.sessionFactory = factory;
                initializationComplete = true;
                synchronized (lock) {
                        lock.notify();
                }
	}

	protected void beforeInvocation() throws Exception {
                while (!initializationComplete) {
                        try {
                                synchronized (lock) {
                                        lock.wait(100);
                                }
                       } catch (InterruptedException e) {
                               // behavior ignores cause of re-awakening.
                       }
                }
		openSession = SessionFactoryUtils.getSession(sessionFactory, true);
		openSession.setFlushMode(FlushMode.NEVER);
		TransactionSynchronizationManager.bindResource(sessionFactory,
				new SessionHolder(openSession));
		super.beforeInvocation();
	}

	protected void afterInvocation() throws Exception {
		super.afterInvocation();
		TransactionSynchronizationManager.unbindResource(sessionFactory);
		SessionFactoryUtils
				.closeSessionIfNecessary(openSession, sessionFactory);
	}
}
  • No labels

2 Comments

  1. I cannot seem to edit this page, so perhaps this comment will suffice. (Sorry for that first one--I forgot I was writing wiki markup code instead of just plaintext.)

    I believe OpenSessionBackgroundProcess has a race condition. The constructor of BackgroundProcess starts the process before completing. This means that beforeInvocation() may get called before the "this.sessionFactory = factory;" line in the constructor.

    OpenSessionBackgroundProcess.java
    public class OpenSessionBackgroundProcess extends BackgroundProcess {
    
    	SessionFactory sessionFactory;
    	Session openSession;
            protected boolean initializationComplete = false;
            private Object lock = new Object(); // used for synchronization
    
    	public OpenSessionBackgroundProcess(String name,
    			ActionInvocation invocation, int threadPriority,
    			SessionFactory factory) {
    		super(name, invocation, threadPriority);
    		this.sessionFactory = factory;
                    initializationComplete = true;
                    synchronized (lock) {
                            lock.notify();
                    }
    	}
    
    	protected void beforeInvocation() throws Exception {
                    while (!initializationComplete) {
                            try {
                                    synchronized (lock) {
                                            lock.wait(100);
                                    }
                           } catch (InterruptedException e) {
                                   // behavior ignores cause of re-awakening.
                           }
                    }
    		openSession = SessionFactoryUtils.getSession(sessionFactory, true);
    		openSession.setFlushMode(FlushMode.NEVER);
    		TransactionSynchronizationManager.bindResource(sessionFactory,
    				new SessionHolder(openSession));
    		super.beforeInvocation();
    	}
    
    	protected void afterInvocation() throws Exception {
    		super.afterInvocation();
    		TransactionSynchronizationManager.unbindResource(sessionFactory);
    		SessionFactoryUtils
    				.closeSessionIfNecessary(openSession, sessionFactory);
    	}
    }