[ Team LiB ] Previous Section Next Section

Global Auctions Use of JTA

In the Global Auctions application, the ProcessBidMDB is the component used to process customers' bids. The MDB processes JMS messages received asynchronously, and notifications are returned to the user via an email sent through the AuctionMailServlet. The ProcessBidMDB can be used to read current bid information on an item, set a current bid, and close the bidding when the auction deadline has been met. Many different actors concurrently use the bean to read and update bid information on their item. This makes the ProcessBidMDB a good candidate for transaction management.

In this section, we'll configure the ProcessBidMDB to participate in a container-managed transaction. All that is required to configure the component for container-managed transactions is to modify the deployment descriptor file: ejb-jar.xml.

Specifying the Transaction Type

In a text editor (or, better yet, an XML editor), open the ejb-jar.xml file. Transaction type is defined as being either container managed or bean managed. You designate which by adding the transaction type element within the <transaction-type></transaction-type> tags. This can be either Container for container-managed or Bean for bean managed. Our ProcessBidMDB is going to be container managed, so we use the Container attribute. The <transaction-type></transaction-type> element tags lie between the <message-driven></message-driven> tags, as shown in Listing 9.1.

Listing 9.1 ejb-jar.xml
<?xml version="1.0"?><!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.
//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtds/ejb-jar_2_0.dtd">
<ejb-jar>
  <enterprise-beans>
    <message-driven>
      <ejb-name>EJB_Name</ejb-name>
      <ejb-class>com.wlsunleashed.EJB.ejbExample</ejb-class>
      <transaction-type>Container</transaction-type>
      <message-driven-destination>
         <destination-type>javax.jms.Queue</destination-type>
      </message-driven-destination>
</message-driven>
</enterprise-beans>
</ejb-jar>

After you've modified the ejb-jar.xml file, save it and close it.

NOTE

In this chapter, we've shown snippets of code and talked about various transaction APIs. The aim here is not to show complete code, but just to give you enough of an idea of how transactions work within Weblogic Server 8.1. Concepts such as EJBs will be explained in later chapters, and full code samples will be provided.


Specifying the Default Transaction Attribute

Recall that the transaction attribute defines how transactions are handled by the container. The default transaction attribute is also specified in the ejb-jar.xml file between the <trans-attribute></trans-attribute> element tags. These tags are between the <message-driven></message-driven> element tags.

There are six different transaction attributes that you can define in the deployment descriptor: NotSupported, Required, Supports, RequiresNew, Mandatory, or Never. The attributes were defined earlier in the chapter.

TIP

A transaction attribute may also be defined on the method level. When a client invokes a method on an enterprise component, the container checks the trans-attribute setting for that method. If found, that method-level transaction attribute is used; if not, the transaction attribute assigned to the entire bean is used.


Configuring a component for container-managed transactions is not hard at all. Bean-managed transactions are not that more difficult. Your bean just needs to obtain the UserTransaction interface and you handle the management methods with appropriate method calls. You set the transaction-type attribute to Bean and let WebLogic's JTS handle all the rest.

    [ Team LiB ] Previous Section Next Section