When a component is transactional and uses the EJB Server connection management feature, commands sent to a data source are automatically performed as part of a transaction. Component methods can call EJB Server’s transaction state primitives to influence whether EJB Server commits or aborts the current transaction.
The EJB Server coordinates the database activity of all transactional components participating in the transaction. The application can roll back everything that took place in the transaction if any component could not complete its part of the work.
To define how a component participates in transactions:
Choose a transaction coordinator for the EJB Server. The transaction coordinator manages the flow of transactions that involve more than one connection and sometimes more than one data source.
Set the component's transaction attribute to determine how the component participates in transactions.
Code methods to call EJB Server’s transaction state primitives to influence the transaction outcome.
Each task is described in detail below.
The transaction coordinator manages the flow of transactions that involve more than one connection and sometimes more than one data source. To enable transactions involving multiple data sources, you must configure your EJB Server to use a transaction coordinator that supports two-phase commit, such as OTS/XA.
Each EJB Server component has a transaction attribute that determines how instances of the component participate in transactions. Values are:
Attribute |
Description |
---|---|
Requires Transaction |
The component always executes in a transaction. Use this option when your component’s database activity needs to be coordinated with other components, so that all components participate in the same transaction. |
Requires New Transaction |
Whenever the component is instantiated, a new transaction begins. |
Supports Transaction |
The component can execute in the context of an EJB Server transaction, but a transaction is not required to execute the component's methods. If a method is called by a base client that has a pending transaction, the method’s database work occurs in the scope of the client’s transaction. Otherwise, the component’s database work is done outside of any transaction. |
Not Supported |
The component's methods never execute as part of a transaction. If the component is activated by a client that has a pending transaction, the component’s work is performed outside the existing transaction. |
Mandatory |
Component methods must be called in the context of a pending transaction. If a client calls a method without an open transaction, the EJB Server ORB throws an exception. |
Bean Managed |
For EJB session Beans only. The component can explicitly begin, commit, and rollback new, independent transactions by using the javax.transaction.UserTransaction interface. Transactions begun by the component execute independently of the client’s transaction. If the component has not begun a transaction, the component’s database work is performed independently of any EJB Server transaction. |
If your component participates in EJB Server transactions, you can call transaction state primitives to explicitly commit or roll back database updates performed in a method.
Components that use the Bean Managed or OTS Style transaction attribute must explicitly begin and end transactions using the APIs described below. For components that use any other attribute, EJB Server implicitly commits each method’s work when the method returns unless the method has requested rollback.
Different component types use different transaction APIs:
EJB components using attribute Bean Managed Only EJB session Beans can use the Bean Managed transaction attribute. Components using this attribute must call the methods in the interface javax.transaction.UserTransaction to begin, commit, and roll back transactions. If the component is not a stateful session Bean, then transactions begun in a method call must be committed or rolled back before the method returns. Otherwise, EJB Server logs a runtime error and returns an exception to the client.
EJB components using any other attribute When an EJB component does not use the Bean Managed transaction attribute, EJB Server implicitly commits the component’s work after each method returns. To override the default outcome, call the EJBContext.setRollBackOnly method.