[ Team LiB ] Previous Section Next Section

Best Practice for Choosing Transaction Attributes

In this section, we discuss the best practices for choosing the appropriate transaction attributes. The recommended choice for a transaction attribute is Required because it ensures that the methods of an enterprise bean are invoked under a JTA transaction. These EJBs can be made to perform work under the scope of a single transaction.

  • If an enterprise bean method needs to commit its results unconditionally, the RequiresNew transaction attribute is the right choice. That attribute always creates a new transaction, regardless of whether another transaction is in progress—an example is logging. The appropriate method in the EJB that performs logging should be invoked with the RequiresNew attribute. This ensures that logging is performed regardless of whether other transactions complete or are rolled back.

  • The NotSupported transaction attribute can be used when the resource being accessed for a transaction cannot be part of a JTA transaction. This is generally used when accessing external systems that do not support J2EE transactions. The J2EE server does not have any control over that system's transactions.

  • The Supports transaction attribute is not recommended at all. Supports can lead to violations of the ACID properties because the EJB behaves differently based on the transaction context of the calling code.

  • The transaction attributes Mandatory and Never can be used when it's necessary to verify the transaction association of the calling client. They put constraints on the transactionality of the calling components.

    [ Team LiB ] Previous Section Next Section