Member Menu
 
 Monthly JBoss newsletter:
 
Java Persistence with Hibernate
CaveatEmptor

AspectJ Hibernate aspect

In order to not need to close explicitly hibernate sessions, one can use AOP to handle this by adding a "persistent" aspect to business objects.

This could be done using the following aspect (which uses the Sessions and transactions pattern discussed in this area), developed with AspectJ:

public aspect HibernateAspect {

    pointcut transactionalInvocation(): execution(* test.server..*.*(..));
    
    Object around(): transactionalInvocation() && !cflowbelow(transactionalInvocation()) {
        try {
            return proceed();
        } finally {
            HibernateSession.closeSession();
        }
    }
}

  NEW COMMENT

AspectJ Hibernate aspect 16 Jan 2004, 03:53 difranr
Here are some examples of transaction control via aspectj is some 
Hibernate based projects I am working on:

/**
 * This class provides Hibernate transaction control any time any 
method  
 * on the <code>HibernateTransaction</code> is execute a transaction 
is started
 * and stopped properly.  This use the helper class 
<code>HibernateHelper</code>
 * which uses the Thread Local Session pattern.
 *
 * @author Ronald R. DiFrango 
 */
public aspect TransactionControl
{
	pointcut transaction(HibernateTransaction tc) :
			target(tc) && execution(public * 
HibernateTransaction.*(..));
		
	before(HibernateTransaction tc) : transaction(tc)
	{
		HibernateHelper.start();
	}

	after(HibernateTransaction tc) returning : transaction(tc)
	{
		HibernateHelper.commit();
	}
	
	after(HibernateTransaction tc) throwing : transaction(tc)
	{
		HibernateHelper.rollback();
	}
}

/**
 * This class provides opens and closes a Hibernate session any 
 * time any method on in the hibernate package is executed.
 * This use the helper class <code>HibernateHelper</code>
 * which uses the Thread Local Session pattern.
 *
 * @author Ronald R. DiFrango 
 */
public aspect HibernateAspect
{
	pointcut hibernateClassList() : within
(com.tascon.tim.provider.hibernate.*);
	
	pointcut hibernateFetch(HibernateProviderBase hpb) : execution
(public * *.*(..)) && target(hpb);
	
	before(HibernateProviderBase hpb) : hibernateClassList() && 
hibernateFetch(hpb)
	{
		try
		{
			hpb.sess = HibernateHelper.openSession();
		}
		catch(Exception e)
		{
		}
	}
	
	after(HibernateProviderBase hpb) returning : hibernateClassList
() && hibernateFetch(hpb)
	{
		HibernateHelper.closeSession();
	}
	
	after(HibernateProviderBase hpb) throwing : hibernateClassList
() && hibernateFetch(hpb)
	{
		HibernateHelper.closeSession();
	}
}
 
AspectJ solution for session management 19 Oct 2004, 12:00 zubairov
This solution replaces ThreadLocal Session pattern and greatly 
simplifies session management.

/**
 * An interface which has to be implemented
 * by every class which is needed a session for the
 * operation.
 * Actually the method of it is implemented in the aspect
 * therefore there no need to add any method to the parent
 * class
 * 
 * @author Renat Zubairov
 */
public interface SessionHolder
{
    public abstract Session getSession();

}

/**
 * Aspect for Session management.
 * Manages session and open it as soon as it was requested
 * from any of the Controllers (objects realizes SessionHolder 
interface)
 * Closes session after controllers are worked out.
 * 
 * @author Renat Zubairov
 */
public aspect SessionManagementAspect percflow (execution(* 
SessionHolder+.*(..)) && !cflowbelow(execution(* SessionHolder+.*
(..)))) {
    
    private Session s = null;
    
    public Session getSession() {
        if (s == null) s = SessionFactory.getInstance().getSession();
        return s;
    }
    
    public void closeSession() {
        if (s != null) s.close();
    }
    
    private pointcut pc(): execution(* SessionHolder+.*(..));
    
    public Session SessionHolder.getSession() {   
        return SessionManagementAspect.aspectOf().getSession();
    }
    
    after() : pc()  && !cflowbelow(pc()) {
        SessionManagementAspect.aspectOf().closeSession();
    }

}

Explanations: All classes which needs an access to the 
HibernateSession implements interface SessionHolder (it can be done 
manyally or via connector aspect using declare parrents: * implements 
SessionHolder). This type of introduction called Container 
Introduction (copyright by Stefan Hannenberg from Uni-Essen) or 
Abstract Schema (copyright by Rickard Oberg). Althroug interface has 
one method this method is implemented by aspect therefore classes 
which needs a session needn't to implement method getSession().
To get a session in the worker classes all you need to do is:
((SessionHolder)this).getSession();

Session will be automatically opened as soon as requested and 
propogated in the current thread (controlflow). It will be also closed 
as soon as controllflow will ends (i.e. execution of the first called 
SessionHolder completed).
 
© Copyright 2006, Red Hat Middleware, LLC. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc. [Privacy Policy]