| Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide 10g (10.1.3.1.0) Part Number B28221-02 |
|
|
View PDF |
To configure how the container manages transactions when a client invokes a method of an EJB 3.0 enterprise bean configured for container-managed transactions, you can use annotations (see Using Annotations) or deployment XML (see "Using Deployment XML").
For more information, see the following:
You can configure transaction management using the @TransactionAttribute annotation attribute value, as Example 21-2 shows. Table 21-1 lists the TransactionAttributeType values you can specify and shows how the container will respond depending on whether or not a client-controlled transaction exists at the time the method is invoked.
Table 21-1 TransactionAttributeType Values for @TransactionAttribute
| Transaction Attribute | Client-Controlled Transaction Exists | Client-Controlled Transaction Does Not Exist |
|---|---|---|
|
|
Container suspends client transaction |
Use no transaction |
|
|
Use client-controlled transaction |
Use no transaction |
|
|
Use client-controlled transaction |
Container starts a new transaction |
|
|
Use client-controlled transaction |
Container starts a new transaction |
|
|
Use client-controlled transaction |
Exception raised |
|
|
Exception raised |
Use no transaction |
Footnote 1 Default.
You can apply the @TransactionAttribute annotation at the class-level to specify the default transaction attribute for all business methods of the enterprise bean. You can apply this annotation at the method-level to specify the transaction attribute for that method. Applying the annotation at the method-level overrides the class-level annotation (if any) for that method.
Example 21-2 Configuring Transaction Attribute for an EJB 3.0 Session Bean
import javax.ejb.Stateful;
import javax.annotation.PostConstruct;
import javax.ejb.Remove;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.ejb.TransactionAttribute;
import static javax.ejb.TransactionAttributeType.REQUIRED;
import static javax.ejb.TransactionAttributeType.REQUIRES_NEW;
import com.acme.Cart;
@Stateful
@TransactionManagement(value=TransactionManagementType.CONTAINER)
@TransactionAttribute(value=REQUIRED)
public class CartBean implements Cart {
private ArrayList items;
@PostConstruct
public void initialize() {
items = new ArrayList();
}
@Remove
@TransactionAttribute(value=REQUIRES_NEW)
public void finishedShipping() {
// Release any resources.
}
public void addItem(String item) {
items.add(item);
}
public void removeItem(String item) {
items.remove(item);
}
}
For an EJB 3.0 enterprise bean, you configure transaction attributes in the orion-ejb-jar.xml file as you would for an EJB 2.1 enterprise bean (see "Using Deployment XML").