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();
}
} |