Thread:
 AspectJ Hibernate aspect 
 difranr   16 Jan 2004, 03:53 

Comment
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
From: difranr (16 Jan 2004, 03:53) Replies: 0, Views: 36578
Subject: AspectJ Hibernate aspect
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();
	}
}
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
© Copyright 2006, Red Hat Middleware, LLC. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc. [Privacy Policy]