Java Reference
In-Depth Information
The following example illustrates how to use this annotation.
package com.ensode.sessionbeanintro.ejb;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
@Stateless
public class EchoBean implements EchoRemote {
@TransactionAttribute(
TransactionAttributeType.REQUIRES_NEW)
public String echo(String saying) {
return "echoing: " + saying;
}
}
As we can see, we simply need to decorate the method to be configured with the @
TransactionAttribute annotation with the appropriate TransactionAttributeType
enumeration constant as a parameter to configure transactions for a single method.
As we mentioned before, if we wish for all of our methods to use the same transaction
strategy, we can place the @TransactionAttribute annotation at the class level.
Implementing aspect oriented
programming with interceptors
Sometimes we wish to execute some logic just before and/or just after a method's
main logic executes. For example, we might want to measure the execution time of a
method to track down performance problems, or we might want to send a message
to a log every time we enter and leave a method, to make it easier to track down
bugs or exceptions.
The most common solution to these kind of problems is to add a little bit of code at
the beginning and end of every method, implementing the logic to profile or log in
each method. This approach has several problems: the logic needs to be implemented
several times, if we later wish to modify or remove the functionality; we need to
modify several methods.
Aspect Oriented Programming is a paradigm that solves the above problems by
providing a way to implement the logic to be executed just before and/or just after a
method's main logic in a separate class. EJB 3.0 introduced the ability to implement
aspect oriented programming via interceptors .
 
Search WWH ::




Custom Search