A transaction ensures ACID. A transaction can end in two ways: with a commit or a rollback.
Traditionally, J2EE developers have had two choices for transaction management: to use global or local transactions. Global transactions are managed by the application server, using JTA. Local transactions are resource-specific: for example, a transaction associated with a JDBC connection. This choice had profound implications. Global transactions provide the ability to work with multiple transactional resources. (It's worth noting that most applications use a single transaction resource). With local transactions, the application server is not involved in transaction management, and cannot help ensure correctness across multiple resources. Global transactions have a significant downside. Code needs to use JTA: a cumbersome API to use (partly due to its exception model). Furthermore, a JTA UserTransaction normally needs to be obtained from JNDI: meaning that we need to use both JNDI and JTA to use JTA. Obviously all use of global transactions limits the reusability of application code, as JTA is normally only available in an application server environment. The preferred way to use global transactions was via EJB CMT (Container Managed Transaction): a form of declarative transaction management (as distinguished from programmatic transaction management). EJB CMT removes the need for transaction-related JNDI lookups--although of course the use of EJB itself necessitates the use of JNDI. It removes most--not all--need to write Java code to control transactions. The significant downside is that CMT is (obviously) tied to JTA and an application server environment; and that it's only available if we choose to implement business logic in EJBs, or at least behind a transactional EJB facade. The negatives around EJB in general are so great that this is not an attractive proposition, when there are alternatives for declarative transaction management. Local transactions may be easier to use, but also have significant disadvantages: They cannot work across multiple transactional resources, and tend to invade the programming model. For example, code that manages transactions using a JDBC connection cannot run within a global JTA transaction.
According to J2EE 1.4, A Container-Managed transaction attribute may have one of the following values:
- Required
- RequiresNew
- Mandatory
- NotSupported
- Supports
- Never
Table 25-1 Transaction Attributes and Scope
| ||
Transaction Attribute
|
Client's Transaction
|
Business Method's Transaction
|
Required
|
None
|
T2
|
T1
|
T1
| |
RequiresNew
|
None
|
T2
|
T1
|
T2
| |
Mandatory
|
None
|
error
|
T1
|
T1
| |
NotSupported
|
None
|
None
|
T1
|
None
| |
Supports
|
None
|
None
|
T1
|
T1
| |
Never
|
None
|
None
|
T1
|
Error
|
See "Chapter 25: Transactions" of "j2ee-tutorials14" for more information.