Using a deployment descriptors for transactions

Transaction types can be specified using annotations or deployment descriptors. In this recipe, we illustrate how to use a deployment descriptor to specify the transaction type for an EJB and its methods.

Getting ready

The process for creating a deployment descriptor for transactions includes:

  • Creating an ejb-jar.xml file for the EJB module
  • Using the<container-transaction> element to define interceptors
  • Deploying the application

We will base this example on the PopulationManager class from the Chapter 6, Understanding how the TransactionAttributeType affects transactions recipe. The PopulationManager class as shown below, uses container-managed transactions and annotates the updateCityPopulation method with RequiresNew. The other methods of the class default to Required.

@Stateful
@TransactionManagement(TransactionManagementType.CONTAINER)
public class PopulationManager implements SessionSynchronization {
@EJB
CityFacade cityFacade;
...
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void updateCityPopulation(String cityName, long count) {
// Update city's population
try {
cityFacade.changePopulation(cityName, count);
} catch(IllegalPopulationException e) {
System.out.println("IllegalPopulationException caught");
}
}
}

How to do it...

Transaction attributes are specified in the ejb-jar.xml file. Create an ejb-jar.xml file. The root element is<ejb-jar> as shown here:

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns = "http://java.sun.com/xml/ns/javaee"
version = "3.1"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd">
...
</ejb-jar>

Within the<ejb-jar> element add an<assembly-descriptor> element. Within this element add two<container-transaction> elements which will be used to define the transactions.

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar>
<assembly-descriptor>
<container-transaction>
...
</container-transaction>
<container-transaction>
...
</container-transaction>
</assembly-descriptor>
</ejb-jar>

Within the<container-transaction> element the<method> element is used to specify those methods that transactions will be applied to. This is followed by a<trans-attribute> element which contains the transactions type.

The<method> element contains an<ejb-name> element to specify the name of the EJB and a<method-name> element to specify the method. The following example shows the complete ejb-jar.xml file which declares the same configuration as obtained from the annotations used earlier.

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>PopulationManager</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
<container-transaction>
<method>
<ejb-name>PopulationManager</ejb-name>
<method-name>updateCityPopulation</method-name>
</method>
<trans-attribute>RequiresNew</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>

When the PopulationServlet is executed, the application should execute successfully.

To verify that the ejb-jar.xml is controlling the transaction type, change the<trans-attribute> for the updateCityPopulation method to Mandatory. Re-executing the application results in an exception being thrown since the method requires a transaction to be present but there is none at this point.

How it works...

In the ejb-jar.xml file<container-transaction> elements were used to specify the transaction attribute to be used for methods of the application. The default transaction attribute for the PopulationManager was declared as Required except for the updateCityPopulation method which was specified as RequiresNew. These associations are applied by the server upon deployment of the application. This permits associations to differ by server.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.19.174