Identify correct and incorrect statements or examples about specifying transaction information via annotations and/or deployment descriptors.

[Note]

Specification of a Bean's Transaction Management Type

By DEFAULT, a session bean or message-driven bean has container managed transaction (CMT) demarcation if the transaction management type IS NOT SPECIFIED. The Bean Provider of a session bean or a message-driven bean can use the TransactionManagement annotation to declare whether the session bean or message-driven bean uses bean-managed or container-managed transaction demarcation:

@Target(TYPE) @Retention(RUNTIME)
public @interface TransactionManagement {
	TransactionManagementType value()
		default TransactionManagementType.CONTAINER;
}
					

The value of the TransactionManagement annotation is either CONTAINER or BEAN.:

public enum TransactionManagementType {
	CONTAINER,
	BEAN
}
					

The TransactionManagement annotation is applied to the ENTERPRISE BEAN CLASS.

Example (BMT via annotation):

@Stateful 
@TransactionManagement(TransactionManagementType.BEAN)
public class CalculatorBean implements Calculator {
	...
}
					

Alternatively, the Bean Provider can use the transaction-type deployment descriptor element to specify the bean's transaction management type. If the deployment descriptor is used, it is only necessary to explicitly specify the bean's transaction management type if bean-managed transaction is used.

Example (CMT via Deployment Descriptor):


<enterprise-beans>
	<session>
		<ejb-name>CalculatorBean</ejb-name>
		<business-remote>by.iba.ejb.Calculator</business-remote>
		<ejb-class>by.iba.ejb.CalculatorBean</ejb-class>
		<session-type>Stateless</session-type>
		<transaction-type>Container</transaction-type>
	</session>
</enterprise-beans>
					
					

The transaction management type of a bean is determined by the Bean Provider. The application assembler is NOT permitted to use the deployment descriptor to override a bean's transaction management type regardless of whether it has been explicitly specified or defaulted by the Bean Provider.

Specification of the Transaction Attributes for a Bean's Methods

The Bean Provider of an enterprise bean with container-managed transaction (CMT) demarcation MAY specify the transaction attributes for the enterprise bean's methods. By DEFAULT, the value of the transaction attribute for a method of a bean with container-managed transaction demarcation is the REQUIRED transaction attribute, and the transaction attribute does not need to be explicitly specified in this case.

A transaction attribute is a value associated with each of the following methods:

The transaction attribute specifies how the container must manage transactions for a method when a client invokes the method.

Transaction attributes are specified for the following methods:

By DEFAULT, if a TransactionAttribute annotation is not specified for a method of an enterprise bean with container-managed transaction (CMT) demarcation, the value of the transaction attribute for the method is defined to be REQUIRED.

The Bean Provider may use the deployment descriptor as an alternative to metadata annotations to specify the transaction attributes (or as a means to supplement or override metadata annotations for transaction attributes). Transaction attributes specified in the deployment descriptor are assumed to OVERRIDE or supplement transaction attributes specified in annotations. If a transaction attribute value is not specified in the deployment descriptor, it is assumed that the transaction attribute specified in annotations applies, or - in the case that no annotation has been specified - that the value is Required.

The application assembler is permitted to override the transaction attribute values using the bean's deployment descriptor. The deployer is also permitted to override the transaction attribute values at deployment time. Caution should be exercised when overriding the transaction attributes of an application, as the transactional structure of an application is typically intrinsic to the semantics of the application.

Enterprise JavaBeans defines the following values for the TransactionAttribute metadata annotation:

The deployment descriptor values that correspond to these annotation values are the following:

NOTE: For a message-driven bean's message listener methods (or interface), only the REQUIRED and NOT_SUPPORTED TransactionAttribute values may be used.

NOTE: For an enterprise bean's timeout callback method only the REQUIRES, REQUIRES_NEW and NOT_SUPPORTED transaction attributes may be used.

NOTE: If an enterprise bean implements the javax.ejb.SessionSynchronization interface, only the following values may be used for the transaction attributes of the bean's methods: REQUIRED, REQUIRES_NEW, MANDATORY.

The above 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.

Specification of Transaction Attributes with Metadata Annotations

The TransactionAttribute annotation is used to specify a transaction attribute.

@Target({METHOD, TYPE}) @Retention(RUNTIME)
public @interface TransactionAttribute {
	TransactionAttributeType value()
		default TransactionAttributeType.REQUIRED;
}
					

The value of the transaction attribute annotation is given by the enum TransactionAttributeType:

public enum TransactionAttributeType {
	MANDATORY,
	REQUIRED,
	REQUIRES_NEW,
	SUPPORTS,
	NOT_SUPPORTED,
	NEVER
}
					

The transaction attributes for the methods of a bean class may be specified on the class, the business methods of the class, or both.

Specifying the TransactionAttribute annotation on the bean class means that it applies to all applicable business interface methods of the class. If the transaction attribute type is not specified, it is assumed to be REQUIRED. The absence of a transaction attribute specification on the bean class is equivalent to the specification of TransactionAttribute(REQUIRED) on the bean class:

@Stateless
@TransactionAttribute(NOT_SUPPORTED)
public class PersistentCalculatorBean implements PersistentCalculator {

	public double add(double a, double b) {
		...
	}
	...
}
					

A transaction attribute may be specified on a method of the bean class to override the transaction attribute value explicitly or implicitly specified on the bean class:

					
@Stateless
@TransactionAttribute(NOT_SUPPORTED)
public class PersistentCalculatorBean  implements PersistentCalculator {

	public double add(double a, double b) {
		...
	}
	
	@TransactionAttribute(REQUIRED) 
	public void clearHistory() {
		...
	}
	...
}

					

If the bean class has superclasses, the following additional rules apply:

Example:

@TransactionAttribute(SUPPORTS)
public class SomeClass {
	public void aMethod () {...}
	public void bMethod () {...}
	...
}

@Stateless public class ABean extends SomeClass implements A {
	public void aMethod () {...}

	@TransactionAttribute(REQUIRES_NEW)
	public void cMethod () {...}
	...
}
					

Assuming aMethod, bMethod, cMethod are methods of interface A, their transaction attributes are REQUIRED, SUPPORTS, and REQUIRES_NEW respectively.

Specification of Transaction Attributes in the Deployment Descriptor

Note that even in the absence of the use of annotations, it is not necessary to explicitly specify transaction attributes. If a transaction attribute is not specified for a method in an EJB 3.0 deployment descriptor, the transaction attribute DEFAULTS to Required.

If the deployment descriptor is used to override annotations, and transaction attributes are not specified for some methods, the values specified in annotations (whether explicit or defaulted) will apply for those methods.

The container-transaction element may be used to define the transaction attributes for business, home, component, and message-listener interface methods; web service endpoint methods; and timeout callback methods. Each container-transaction element consists of a list of ONE OR MORE method elements, and the trans-attribute element. The container-transaction element specifies that all the listed methods are assigned the specified transaction attribute value. It is required that all the methods specified in a single container-transaction element be methods of the SAME enterprise bean.

The method element uses the ejb-name, method-name, and method-params elements to denote one or more methods. There are three legal styles of composing the method element:

  1. 
    <!-- Style 1 -->
    
    <method>
    	<ejb-name>EJBNAME</ejb-name>
    	<method-name>*</method-name>
    </method>
    
    								

    This style is used to specify a default value of the transaction attribute for the methods for which there is no Style 2 or Style 3 element specified. There must be at most one container-transaction element that uses the Style 1 method element for a given enterprise bean.

  2. 
    <!-- Style 2 -->
    
    <method>
    	<ejb-name>EJBNAME</ejb-name>
    	<method-name>METHOD</method-name>
    </method>
    
    								

    This style is used for referring to a specified method of a business, home, component or message listener interface method; web service endpoint method; or timeout callback method of the specified enterprise bean. If there are multiple methods with the same overloaded name, this style refers to ALL the methods with the same name. There must be at most one container-transaction element that uses the Style 2 method element for a given method name. If there is also a container-transaction element that uses Style 1 element for the same bean, the value specified by the Style 2 element TAKES PRECEDENCE.

  3. 
    <!-- Style 3 -->
    
    <method>
    	<ejb-name>EJBNAME</ejb-name>
    	<method-name>METHOD</method-name>
    	<method-params>
    		<method-param>PARAMETER_1</method-param>
    		...
    		<method-param>PARAMETER_N</method-param>
    	</method-params>
    </method>
    
    								

    This style is used to refer to a single method within a set of methods with an overloaded name. If there is also a container-transaction element that uses the Style 2 element for the method name, or the Style 1 element for the bean, the value specified by the Style 3 element TAKES PRECEDENCE.

The optional method-intf element can be used to differentiate between methods with the same name and signature that are multiply defined across the business, component, and home interfaces, and/or web service endpoint. However, if the same method is a method of both a local business interface and the local component interface, the same transaction attribute applies to the method for BOTH interfaces. Likewise, if the same method is a method of both a remote business interface and the remote component interface, the same transaction attribute applies to the method for BOTH interfaces.

The following is an example of the specification of the transaction attributes in the deployment descriptor. The updatePhoneNumber method of the EmployeeRecord enterprise bean is assigned the transaction attribute Mandatory; all other methods of the EmployeeRecord bean are assigned the attribute Required. All the methods of the enterprise bean AardvarkPayroll are assigned the attribute RequiresNew:


<ejb-jar>
	...
	<assembly-descriptor>
		...
		<container-transaction>
			<method>
				<ejb-name>EmployeeRecord</ejb-name>
				<method-name>*</method-name>
			</method>
			<trans-attribute>Required</trans-attribute>
		</container-transaction>
		
		<container-transaction>
			<method>
				<ejb-name>EmployeeRecord</ejb-name>
				<method-name>updatePhoneNumber</method-name>
			</method>
			<trans-attribute>Mandatory</trans-attribute>
		</container-transaction>
		
		<container-transaction>
			<method>
				<ejb-name>AardvarkPayroll</ejb-name>
				<method-name>*</method-name>
			</method>
			<trans-attribute>RequiresNew</trans-attribute>
		</container-transaction>
	</assembly-descriptor>
</ejb-jar>

					

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     Free Mock Exam Engine