Java Reference
In-Depth Information
The @TransactionAttribute annotation can be used to decorate the class
declaration of our Enterprise JavaBean, or it can be used to decorate a single method.
If used to decorate the class declaration, then the declared transaction behavior
will apply to all methods in the bean. When used to decorate a single method,
the declared behavior will affect only the decorated method. If a bean has a
@TransactionAttribute annotation both at the class level and at the method level,
the method level annotation takes precedence. If no transaction attribute is specified
for a method, then TransactionAttributeType.REQUIRED attribute is used
by default.
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 mentioned before, if we wish 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.
 
Search WWH ::




Custom Search