November 1, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Transactions in EJB

  • August 15, 2003
  • By Saleem Siddiqui, Michael Landy, & Jeff Swisher
  • Send Email »
  • More Articles »

This material is from the book JBuilder Developer's Guide , written by Saleem Siddiqui, Michael Landy, and Jeff Swisher, published by Sams Publishing.

© Copyright Sams Publishing. All rights reserved.


Transactions play an important role in most application development projects. This is not different when developing an EJB-based solution. Transactions can be complex features to implement, but EJB-based systems require that the vendors implement transaction processing within the container. This does not mean, however, that you do not have to be concerned about transactions.

Simply put, a transaction is a grouping of work that represents a single unit or task. This single unit may contain something as simple as one step or many complex steps to be treated as one. Many times, people ask "Why do I need transactions?" Transactions help to alleviate the complexity of coordinating data or resource access across many clients.

Whenever you use transactions, you need to understand the four main characters: Atomicity, Consistency, Isolation, and Durability, commonly referred to as ACID. These four properties and the basic requirements fulfilled through the use of transactions are shown in Table 1.

Table 1 Properties of a Successful Transaction

Property

Description

Atomicity

All or none of a transaction's effects are kept.

Consistency

Transactions preserve data integrity and consistency.

Isolation

Intermediate results of a transaction are not visible outside that transaction.

Durability

Committed effects survive system failures.


Transaction Attributes

With declarative demarcation you specify to the container which methods are going to be included within transactional processing. Each method is given a transactional context that it uses to participate within the given transaction. Enterprise JavaBeans define seven different types of transaction attributes, each having a slightly different implementation, as shown in Table 2.

Table 2 Transaction Attributes Available for EJB Transactional Implementation

Transactional Attribute

Description

Required

Guarantees that the work performed by the method is within a global transaction context. If the caller already has a transaction context, the container uses the same context. If the caller has not been assigned a transaction context, the container begins a new transaction automatically. The attribute makes it easy to implement multiple beans and coordinate the work of all the beans using the same global transaction for all the beans within the same transaction.

RequiresNew

The container invokes the enterprise bean method with a new transaction it has started with. The container ends this transaction when the invocation completes. The transaction is ended either by a rollback or commit request.

Mandatory

This allows bean methods to declare that they must be invoked by clients within transactions. Typically, such bean methods are not intended to be used on their own but are expected to be one part of a larger transaction.

Supports

The beans participate in interactions without causing new transactions to be started, yet using them if they are present. Only application logic that can function correctly both within a transaction and without one should be allowed to use this mode.

NotSupported

Allows bean methods with implementations that can't or shouldn't be invoked with a transaction. This might happen, for instance, if a bean does not want the client's transaction to propagate to resources that it may contact during its processing.

Never

This allows a bean method to declare that it must never be invoked by clients running transactions. Typically, this is because the bean's method performs some activity that can provide ACID properties. This attribute has been deprecated in EJB 2.0 specifications.

Bean-managed

This is also known as programmatic transactional control. In other words, the transactions are implemented within the source of the bean.


Transaction Usage

Transaction usage within the container is specified within the deployment descriptor. JBuilder's EJB Designer provides an easy-to-use interface that gives access to the transactional attributes. This interface makes it simple to define a transactional context that will ultimately be contained within the deployment descriptor. For example, let's look at our Employee entity bean again. The following process is used to define the transactional context for the entity bean:

  1. Select a bean to define a transaction attribute.

  2. Underneath the Employee entity you will see a child node entitled Container Transaction.

  3. Double-click on the Container Transaction; this loads the Container Transaction editor, shown in Figure 1.


  4. Figure 1
    Define a transaction attribute using the Container Transaction editor.

  5. Using the drop-down combo boxes, select the interface—either Home, Remote, LocalHome, or Local. After you select an interface, you are then allowed to either select all the methods (*) or specify a method.

  6. Finally, select the appropriate transactional attribute.

The results of this process are placed in the deployment descriptor, as shown in Listing 1.

Listing 1 Portion of the Deployment Descriptor Containing the Transaction Attributes

<container-transaction>
  <method>
    <description />
    <ejb-name>Employee</ejb-name>
    <method-intf>LocalHome</method-intf>
    <method-name>create</method-name>
    <method-params>
      <method-param>java.lang.Short</method-param>
    </method-params>
  </method>
  <trans-attribute>Required</trans-attribute>
</container-transaction>
<container-transaction>
  <method>
    <description />
    <ejb-name>Employee</ejb-name>
    <method-intf>LocalHome</method-intf>
    <method-name>findAll</method-name>
    <method-params />
  </method>
  <trans-attribute>Supports</trans-attribute>
</container-transaction>

About the Authors

Saleem Siddiqui is a technical architect and trainer with Dunn Solutions Group. He is also a Sun Certified Developer and a Borland Certified JBuilder Instructor. At Dunn, he provides consulting services and business application development.

Michael Landy is Director of Business Applications for Dunn Solutions Group directing the strategic solutions for clients.

Jeff Swisher is a technical manager for the Dunn Solutions Group business applications. He works as a programmer, trainer, architect, and instructional designer. He is a Sun Certified Java Developer and a Borland Certified Instructor in JBuilder.

Source of this material

This material is from Chapter 23: Developing Entity Beans from the book JBuilder Developer's Guide (ISBN: 0-672-32427-X) written by Saleem Siddiqui, Michael Landy, and Jeff Swisher, published by Sams Publishing.

To access the full Table of Contents for the book.


Other Chapters from Sams Publishing:

Web Services and Flows (WSFL)
Overview of JXTA
Introduction to EJBs
Processing Speech with Java
The Java Database Control in BEA Weblogic
Databases and Tomcat
Working with JAX-RPC
Understanding Servlets







Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel