Skip Headers
Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide
10g (10.1.3.5.0)

Part Number E13981-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

Configuring an EJB 3.0 Transaction Attribute

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:

Using Annotations

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 ClientEControlled Transaction Exists ClientEControlled Transaction Does Not Exist

NOT_SUPPORTED

Container suspends client transaction

Use no transaction

SUPPORTS

Use client-controlled transaction

Use no transaction

REQUIREDFoot 1 

Use client-controlled transaction

Container starts a new transaction

REQUIRES_NEW

Use client-controlled transaction

Container starts a new transaction

MANDATORY

Use client-controlled transaction

Exception raised

NEVER

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);
    }
}

Using Deployment XML

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").