Java Reference
In-Depth Information
The @TransactionAttribute annotation can be used to decorate the class
declaration of our EJB, or it can be used to decorate a single method. If used to
decorate the class declaration, the declared transaction behavior will apply to all
methods in the bean. If used to decorate a single method, the declared behavior
will affect only the decorated method. If a bean has an @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,
the TransactionAttributeType.REQUIRED attribute is used by default.
The following example shows how to use the @TransactionAttribute annotation:
package com.ensode.sessionbeanintro.ejb;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
@Stateless
public class Echo
@override
@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 want 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