Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Try Declarative Programming with Annotations and Aspects : Page 4

Learn how to combine the power of annotations with aspects to provision enterprise services declaratively, in an EJB 3.0-compatible manner, while still providing container independence.


advertisement
Transactions
Transactions are an important part of enterprise development since they help ensure integrity of data in a concurrent environment. At a high level, transactions ensure that with multiple operations either all complete or that none complete at all. For more information on transactions see my previous article, "Using Annotations with Aspects in Pre-Java 5 Versions," or the related resources sidebar.

Unlike the annotations for resource injection and security, the annotations for transactions are specific to EJB 3.0 and not defined in JSR 250 common annotations. EJB 3.0 defined two annotations related to transactions: TransactionManagement and TransactionAttribute. The TransactionManager annotation specifies whether transactions are to be container-managed or bean-managed. In EJB 3, if this annotation is not specified then container-managed transactions are assumed. The TransactionAttribute annotation specifies transaction propagation level of the method. Valid values—including mandatory, required, requires new, supports, not supported, and never—define whether an existing transaction is required, or a new transaction should be started, etc.

Because the bookFlight operation involves two steps, booking an outbound and a returning flight, you can ensure consistency of this operation by wrapping it in a transaction. Using the EJB 3.0 transaction annotations this would look as follows:



public class TravelAgencyServiceImpl implements ITravelAgencyService { @Resource(name = "flightDAO") public IFlightDAO flightDAO; @RolesAllowed("user") @TransactionAttribute(TransactionAttributeType.REQUIRED) public void bookTrip(long outboundFlightID, long returnFlightID, int seats) throws InsufficientSeatsException { reserveSeats(outboundFlightID, seats); reserveSeats(returnFlightID, seats); } }

And you could apply a simple aspect that demarcates the transactions boundaries automatically:

@Aspect public class TransactionAspect { @Pointcut("execution(@javax.ejb.TransactionAttribute * *.*(..))") public void transactionalMethods() {} @Before("transactionalMethods()") public void beforeTransactionalMethods() { HibernateUtil.beginTransaction(); } @AfterReturning("transactionalMethods()") public void afterReturningTransactionalMethods() { HibernateUtil.commitTransaction(); } @AfterThrowing("transactionalMethods()") public void afterThrowingTransactionalMethods() { HibernateUtil.rollbackTransaction(); } }

This implementation is based on the assumption that Hibernate and the ubiquitous thread local pattern are being used to manage the Hibernate Session and Transaction objects, but any appropriate implementation, such as one based on JTA, could be used instead.

This article has shown how cross-cutting concerns such as resource management, security, and transactions can be implemented as aspects using the EJB 3.0 and JSR 250 annotations sets. There are several lessons to be learned from this. This first is of course the blueprint that these sample aspects provides for modularizing cross-cutting concerns using AspectJ's implementation. Secondly, we have looked at some of the news ideas and concepts behind the emerging EJB 3.0 specification. And lastly, we have seen in a dramatic fashion the freedom that decoupling our business objects from the EJB API has to offer. At this point all you have to do to make the TravelAgencyServiceImpl a stateless session bean is to add one last annotation:

@Stateful public class TravelAgencyServiceImpl implements ITravelAgencyService { ... }

I hope this very liberating approach to the provisioning of enterprise services will spark much competition and innovation in the framework/container industry.



Rod Coffin is an agile technologist at Semantra, helping to develop an innovative natural language ad hoc reporting platform. He has many years of experience mentoring teams on enterprise Java development and agile practices and has written several articles on a range of topics from Aspect-Oriented Programming to EJB 3.0. Rod is a frequent speaker at user groups and technology conferences and can be contacted via his home page.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap