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 |
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 |
RequiresNew |
The container invokes the enterprise bean method with a new transaction |
Mandatory |
This allows bean methods to declare that they must be invoked |
Supports |
The beans participate in interactions without causing new transactions |
NotSupported |
Allows bean methods with implementations that can't or shouldn't |
Never |
This allows a bean method to declare that it must never be invoked |
Bean-managed |
This is also known as programmatic transactional control. In |
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:
-
Select a bean to define a transaction attribute.
-
Underneath the Employee entity you will see a child node
entitled Container Transaction. -
Double-click on the Container Transaction; this loads the Container
Transaction editor, shown in Figure 1. -
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. -
Finally, select the appropriate transactional attribute.
Figure 1
Define a transaction attribute using the Container Transaction editor.
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:
The Java Database Control in BEA Weblogic