EJB Transactional Attributes

This page discusses - EJB Transactional Attributes

EJB Transactional Attributes

Given a list of transaction behaviors, match them with the appropriate transaction attributes.

A transaction attribute is a value associated with a method of a session or entity bean's home or component interface or with the onMessage(...) method of a message-driven bean. The transaction attribute specifies how the Container must manage transactions for a method when a client invokes the method via the enterprise bean's home or component interface or when the method is invoked as the result of the arrival of a JMS message.

The transaction attribute must be specified for the following methods:

  • For a SESSION bean, the transaction attributes MUST be specified for the methods defined in the bean's COMPONENT interface and all the direct and indirect superinterfaces of the component interface, excluding the methods of the javax.ejb.EJBObject or javax.ejb.EJBLocalObject interface. Transaction attributes MUST NOT be specified for the methods of a session bean's HOME interface.

  • For an ENTITY bean, the transaction attributes MUST be specified for the methods defined in the bean's COMPONENT interface and all the direct and indirect superinterfaces of the component interface, excluding the getEJBHome, getEJBLocalHome, getHandle, getPrimaryKey, and isIdentical methods; and for the methods defined in the bean's HOME interface and all the direct and indirect superinterfaces of the home interface, excluding the getEJBMetaData and getHomeHandle methods specific to the remote home interface (remove, create, and find methods can all have transaction attributes).

  • For a MESSAGE-DRIVEN bean, the transaction attribute MUST be specified for the bean's onMessage(...) method.

Providing the transaction attributes for an enterprise bean is an optional requirement for the Application Assembler, because, for a given enterprise bean, the Application Assembler must either specify a value of the transaction attribute for all the methods for which a transaction attribute must be specified, or the Assembler must specify none. If the transaction attributes are not specified for the methods of an enterprise bean, the Deployer will have to specify them.

Enterprise JavaBeans defines the following (SIX) values for the transaction attribute:

  • NotSupported

  • Required

  • Supports

  • RequiresNew

  • Mandatory

  • Never

For MESSAGE-DRIVEN beans, ONLY the Required and NotSupported transaction attributes may be used.

For ENTITY beans that use EJB 2.0 container-managed persistence, only the Required, RequiresNew, or Mandatory transaction attributes should be used for the methods defined in the bean's component interface and all the direct and indirect superinterfaces of the component interface, excluding the getEJBHome, getEJBLocalHome, getHandle, getPrimaryKey, and isIdentical methods; and for the methods defined in the bean's home interface and all the direct and indirect superinterfaces of the home interface, excluding the getEJBMetaData and getHomeHandle methods specific to the remote home interface. Containers may optionally support the use of the NotSupported, Supports, and Never transaction attributes for the methods of entity beans with container-managed persistence. However, entity beans with container-managed persistence that use these transaction attributes WILL NOT BE PORTABLE.

If an enterprise bean implements the javax.ejb.SessionSynchronization interface, the Application Assembler can specify only the following values for the transaction attributes of the bean's methods: Required, RequiresNew, or Mandatory. This restriction is necessary to ensure that the enterprise bean is invoked only in a transaction. If the bean were invoked without a transaction, the Container would not be able to send the transaction synchronization calls.

Container-managed transaction demarcation for Session and Entity Beans.

  • NotSupported

    The Container invokes an enterprise Bean method whose transaction attribute is set to NotSupported with an unspecified transaction context.

    If a client calls with a transaction context, the container SUSPENDS the association of the transaction context with the current thread before invoking the enterprise bean's business method. The container resumes the suspended association when the business method has completed. The suspended transaction context of the client is not passed to the resource managers or other enterprise Bean objects that are invoked from the business method.

    If the business method invokes other enterprise beans, the Container passes NO transaction context with the invocation.

  • Required

    The Container must invoke an enterprise Bean method whose transaction attribute is set to Required with a valid transaction context.

    If a client invokes the enterprise Bean's method while the client is associated with a transaction context, the container invokes the enterprise Bean's method in the client's transaction context.

    If the client invokes the enterprise Bean's method while the client is not associated with a transaction context, the container AUTOMATICALLY STARTS a new transaction before delegating a method call to the enterprise Bean business method. The Container automatically enlists all the resource managers accessed by the business method with the transaction. If the business method invokes other enterprise beans, the Container PASSES the transaction context with the invocation. The Container attempts to COMMIT the transaction when the business method has completed. The container performs the commit protocol before the method result is sent to the client.

  • Supports

    The Container invokes an enterprise Bean method whose transaction attribute is set to Supports as follows:

    • If the client calls with a transaction context, the Container performs the same steps as described in the Required case (uses client's transaction).

    • If the client calls without a transaction context, the Container performs the same steps as described in the NotSupported case (with an unspecified transaction context).

  • RequiresNew

    The Container must invoke an enterprise Bean method whose transaction attribute is set to RequiresNew with a NEW transaction context.

    If the client invokes the enterprise Bean's method while the client is NOT associated with a transaction context, the container AUTOMATICALLY STARTS a new transaction before delegating a method call to the enterprise Bean business method. The Container automatically enlists all the resource managers accessed by the business method with the transaction. If the business method invokes other enterprise beans, the Container passes the transaction context with the invocation. The Container attempts to commit the transaction when the business method has completed. The container performs the commit protocol before the method result is sent to the client.

    If a client calls with a transaction context, the container SUSPENDS the association of the transaction context with the current thread before starting the new transaction and invoking the business method. The container resumes the suspended transaction association after the business method and the new transaction have been completed. 0

  • Mandatory

    The Container MUST invoke an enterprise Bean method whose transaction attribute is set to Mandatory in a client's transaction context. The client is required to call with a transaction context.

    • If the client calls with a transaction context, the Container performs the same steps as described in the Required case (uses client's transaction).

    • If the client calls without a transaction context, the Container throws the javax.transaction.TransactionRequiredException exception if the client is a remote client, or the javax.ejb.TransactionRequiredLocalException if the client is a local client. 1

  • Never

    The Container invokes an enterprise Bean method whose transaction attribute is set to Never WITHOUT a transaction context defined by the EJB specification. The client is required to call without a transaction context.

    • If the client calls with a transaction context, the Container throws the java.rmi.RemoteException exception if the client is a remote client, or the javax.ejb.EJBException if the client is a local client.

    • If the client calls without a transaction context, the Container performs the same steps as described in the NotSupported case (with an unspecified transaction context). 2

Container-managed transaction demarcation for Message-driven Beans.

Only the NotSupported and Required transaction attributes may be used for message-driven beans. The use of the other transaction attributes is not meaningful for message-driven beans because there can be no pre-existing transaction context (RequiresNew, Supports) and no client to handle exceptions (Mandatory, Never).

  • NotSupported

    The Container invokes a message-driven Bean method whose transaction attribute is set to NotSupported with an unspecified transaction context. 3

    If the onMessage(...) method invokes other enterprise beans, the Container passes NO transaction context with the invocation.

  • Required

    The Container must invoke a message-driven Bean method whose transaction attribute is set to Required with a valid transaction context. Because there is never a client transaction context available for a message-driven bean, the container automatically starts a new transaction before the dequeuing of the JMS message and, hence, before the invocation of the message-driven bean's onMessage(...) method. The Container automatically enlists the resource manager associated with the arriving message and all the resource managers accessed by the onMessage(...) method with the transaction. If the onMessage(...) method invokes other enterprise beans, the Container passes the transaction context with the invocation. The Container ATTEMPTS TO COMMIT the transaction when the onMessage(...) method has completed. If the onMessage(...) method does not successfully complete or the transaction is rolled back by the Container, JMS message redelivery semantics apply. 4

Table 11.1. Transaction attribute summary

Transaction attributeClient's transactionTransaction associated with business methodTransaction associated with resource managers
NotSupportednonenonenone
T1nonenone
RequirednoneT2T2
T1T1T1
Supportsnonenonenone
T1T1T1
RequiresNewnoneT2T2
T1T2T2
MandatorynoneERRORn/a
T1T1T1
Nevernonenonenone
T1ERRORn/a

Tutorials

  1. Appendix A. First Appendix
  2. Second Section
  3. Third Section
  4. Part II. Appendixes
  5. From a list, identify the responsibility of the bean provider and the responsibility of the container provider for a message-driven bean.
  6. Chapter 6. Component Contract for Container-Managed Persistence (CMP)
  7. Identify correct and incorrect statements or examples about persistent relationships, remove protocols, and about the abstract schema type of a CMP entity bean.
  8. Identify the interfaces and methods a CMP entity bean must and must not implement.
  9. Match the name with a description of purpose or functionality, for each of the following deployment descriptor elements: ejb-name, abstract-schema-name, ejb-relation, ejb-relat
  10. Identify correctly-implemented deployment descriptor elements for a CMP bean (including container-managed relationships).
  11. From a list, identify the purpose, behavior, and responsibilities of the bean provider for a CMP entity bean, including but not limited to: setEntityContext, unsetEntityContext, ejbC
  12. Chapter 7. CMP Entity Bean Life Cycle
  13. Identify correct and incorrect statements or examples about the rules and semantics for relationship assignment and relationship updating in a CMP bean.
  14. From a list, identify the responsibility of the container for a CMP entity bean, including but not limited to: setEntityContext, unsetEntityContext, ejbCreate, ejbPostCreate, ejbActi
  15. Given a code listing, determine whether it is a legal and appropriate way to programmatically access a caller's security context.
  16. Chapter 10. Message-Driven Bean Component Contract
  17. Identify correct and incorrect statements about the purpose and use of the deployment descriptor elements for environment entries, EJB references, and resource manager connection factory r
  18. Identify the use and the behavior of the ejbPassivate method in a session bean, including the responsibilities of both the container and the bean provider.
  19. Chapter 12. Exceptions
  20. Identify correct and incorrect statements or examples about the client view of an entity bean's local component interface (EJBLocalObject).
  21. Identify EJB 2.0 container requirements.
  22. Chapter 1. EJB Overview
  23. Identify correct and incorrect statements or examples about EJB programming restrictions.
  24. Chapter 9. EJB-QL
  25. Identify correct and incorrect statements or examples about the purpose and use of EJB QL.
  26. Identify correct and incorrect conditional expressions, BETWEEN expressions, IN expressions, LIKE expressions, and comparison expressions.
  27. Identify correct and incorrect statements or examples about the client view of a entity bean's remote component interface (EJBObject).
  28. Given a list, identify which are requirements for an EJB-jar file.
  29. Match EJB roles with the corresponding description of the role's responsibilities, where the description may include deployment descriptor information.
  30. Chapter 2. Client View of a Session Bean
  31. Chapter 13. Enterprise Bean Environment
  32. Chapter 8. Entity Beans
  33. Identify the use, syntax, and behavior of, the following entity bean home method types, for Container-Managed Persistence (CMP); finder methods, create methods, remove methods, and home me
  34. Identify correct and incorrect statements or examples about an entity bean's primary key and object identity.
  35. Identify correct and incorrect statements or examples about the client's view of exceptions received from an enterprise bean invocation.
  36. Identify correct and incorrect statements or examples about application exceptions and system exceptions in entity beans, session beans, and message-driven beans.
  37. Given a particular method condition, identify the following: whether an exception will be thrown, the type of exception thrown, the container's action, and the client's view.
  38. Given a list of responsibilities related to exceptions, identify those which are the bean provider's, and those which are the responsibility of the container provider. Be prepared to recog
  39. SCBCD Study Guide
  40. Identify the use and behavior of the MessageDrivenContext interface methods.