HIBERNATE JBoss.org
 |  Register  | 
     
News 
About 
   Feature List 
   Road Map 
Documentation 
   Related Projects 
   External Documentation 
Download 
Forum & Mailinglists 
Support & Training 
JIRA Issue Tracking
Wiki Community Area


Hibernate Public Training Courses


Get Hibernate in Action eBook!


JavaWorld 2003 Finalist


Jolt Award 2004 Winner
      
Documentation > Community Area > AspectJ Hibernate aspect

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

coWiki