Copyright © 2001 O'Reilly & Associates, Inc. All rights reserved.
Printed in the United States of America.
Published by O'Reilly & Associates, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O'Reilly & Associates books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safari.oreilly.com). For more information contact our corporate/institutional sales department: 800-998-9938 or email@example.com.
Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks of O'Reilly & Associates, Inc. The association between the image of a wallaby and joey and the topic of Enterprise JavaBeans is a trademark of O'Reilly & Associates, Inc. Java and all Java-based trademarks and logos are trademarks or registered trademarks of Sun Microsystems, Inc., in the United States and other countries. O'Reilly & Associates, Inc. is independent of Sun Microsystems.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O'Reilly & Associates, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher assumes no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
For my wife and best friend, Hollie.
In the winter of 1997, I was consulting on an e-commerce project that was using Java RMI. Not surprisingly, the project failed because Java RMI didn't address performance, scalability, failover, security, or transactions, all of which are vital in a production environment. Although the outcome of that project is not unique to Java RMI—I have seen the same thing happen with CORBA—the timing of the project was especially interesting. Enterprise JavaBeans™ was first introduced by Sun Microsystems at around that time, and had Enterprise JavaBeans (EJB) been available earlier, that same project probably would have succeeded.
At the time I was working on that ill-fated Java RMI project, I was also writing a column for JavaReport Online called "The Cutting Edge." The column covered what were then new Java technologies such as the Java Naming and Directory Interface™ ( JNDI) and the JavaMail™ API. I was actually looking for a new topic for the third installment of "The Cutting Edge" when I discovered the first public draft of Enterprise JavaBeans, Version 0.8. I had originally heard about this technology in 1996, but this was the first time that public documentation had been made available. Having worked on CORBA, Java RMI, and other distributed object technologies, I knew a good thing when I saw it and immediately began writing an article about this new technology.
That seems like eons ago. Since I published that article in March 1998, literally hundreds of articles on EJB have been written, and several books on the subject have come and gone. This book, now in its third edition, has kept pace with three versions of the EJB specification in as many years. As the newest version of the specification takes flight, and a slew of new books on the subject debut, I can't help but remember the days when the words "Enterprise JavaBeans" drew blank looks from just about everyone. I'm glad those days are over.
When Java™ was first introduced in the summer of 1995, most of the IT industry focused on its graphical user interface characteristics and the competitive advantage it offered in terms of distribution and platform independence. Those were interesting times. The applet was king, and only a few of us were attempting to use it on the server side. In reality, we spent about half of our time coding and the other half trying to convince management that Java was not a fad.
Today, the focus has broadened considerably: Java has been recognized as an excellent platform for creating enterprise solutions, specifically for developing distributed server-side applications. This shift has much to do with Java's emerging role as a universal language for producing implementation-independent abstractions for common enterprise technologies. The JDBC™ API is the first and most familiar example. JDBC ( Java Database Connectivity) provides a vendor-independent Java interface for accessing SQL relational databases. This abstraction has been so successful that it's difficult to find a relational database vendor that doesn't support JDBC. Java abstractions for enterprise technologies have expanded considerably to include JNDI for abstracting directory services, JTA ( Java Transaction API) for abstracting access to transaction managers, JMS ( Java Message Service) for abstracting access to different message-oriented middleware products, and more.
Enterprise JavaBeans, first introduced as a draft specification in late 1997, has since established itself as one of the most important Java enterprise technologies provided by Sun Microsystems. EJB provides an abstraction for component transaction monitors (CTMs), which represent the convergence of two technologies: traditional transaction-processing (TP) monitors (such as CICS, TUXEDO, and Encina), and distributed object services (such as CORBA, DCOM, and native Java RMI). Combining the best of both technologies, component transaction monitors provide a robust, component-based environment that simplifies distributed development while automatically managing the most complex aspects of enterprise computing, such as object brokering, transaction management, security, persistence, and concurrency.
Enterprise JavaBeans defines a server-side component model that allows business objects to be developed and moved from one brand of EJB container to another. A component (i.e., an enterprise bean) presents a simple programming model that allows the developer to focus on its business purpose. An EJB server is responsible for making the component a distributed object and for managing services such as transactions, persistence, concurrency, and security. In addition to defining the bean's business logic, the developer defines the bean's runtime attributes in a way that is similar to choosing the display properties of visual widgets. The transactional, persistence, and security behaviors of a component can be defined by choosing from a list of properties. The end result is that EJB makes developing distributed-component systems that are managed in a robust transactional environment much easier. For developers and corporate IT shops that have struggled with the complexities of delivering mission-critical, high-performance distributed systems using CORBA, DCOM, or Java RMI, EJB provides a far simpler and more productive platform on which to base development efforts.
When Enterprise JavaBeans 1.0 was finalized in 1998, it quickly become a de facto industry standard. Many vendors announced their support even before the specification was finalized. Since that time, EJB has been enhanced twice. The specification was first updated in 1999, to Version 1.1, which was covered in the second edition of this book. The most recent revision to the specification, Version 2.0, is covered by this, the third edition of Enterprise JavaBeans. This edition also covers EJB 1.1, which is for the most part a subset of the functionality offered by EJB 2.0.
Products that conform to the EJB standard have come from every sector of the IT industry, including the TP monitor, CORBA ORB, application server, relational database, object database, and web server industries. Some of these products are based on proprietary models that have been adapted to EJB; many more wouldn't even exist without EJB.
In short, Enterprise JavaBeans 2.0 and 1.1 together provide a standard distributed-component model that greatly simplifies the development process and allows beans developed and deployed on one vendor's EJB server to be easily deployed on a different vendor's EJB server. This book will provide you with the foundation you need to develop vendor-independent EJB solutions.
This book explains and demonstrates the fundamentals of the Enterprise JavaBeans 2.0 and 1.1 architectures. Although EJB makes distributed computing much simpler, it is still a complex technology that requires a great deal of time and study to master. This book provides a straightforward, no-nonsense explanation of the underlying technology, Java classes and interfaces, component model, and runtime behavior of Enterprise JavaBeans. It includes material that is backward-compatible with EJB 1.1 and provides special notes and chapters when there are significant differences between 1.1 and 2.0.
Although this book focuses on the fundamentals, it's not a "dummies" book. Enterprise JavaBeans is an extremely complex and ambitious enterprise technology. While using EJB may be fairly simple, the amount of work required to truly understand and master EJB is significant. Before reading this book, you should be fluent in the Java language and have some practical experience developing business solutions. Experience with distributed object systems is not a must, but you will need some experience with JDBC (or at least an understanding of the basics) to follow the examples in this book. If you are unfamiliar with the Java language, I recommend Learning Java™ by Patrick Niemeyer and Jonathan Knudsen; this book was formerly Exploring Java™ (O'Reilly). If you are unfamiliar with JDBC, I recommend Database Programming with JDBC™ and Java™ by George Reese (O'Reilly). If you need a stronger background in distributed computing, I recommend Java™ Distributed Computing by Jim Farley (O'Reilly).
Here's how the book is structured. The first three chapters are largely background material, placing Enterprise JavaBeans 2.0 and 1.1 in the context of related technologies and explaining at the most abstract level how the EJB technology works and what makes up an enterprise bean. Chapter 4 through Chapter 13 go into detail about developing enterprise beans of various types. Chapter 14 and Chapter 15 could be considered advanced topics, except that transactions (Chapter 14) are essential to everything that happens in enterprise computing, and design strategies (Chapter 15) help you deal with a number of real-world issues that influence bean design. Chapter 16 describes in detail the XML deployment descriptors used in EJB 2.0 and 1.1, and Chapter 17 gives an overview of Java™ 2, Enterprise Edition ( J2EE), which includes servlets, JavaServer Pages™ ( JSP), and EJB. Finally, the three appendices provide some reference information that should be useful to you.
This book covers Enterprise JavaBeans Versions 2.0 and 1.1, including all optional features. It uses Java language features from the Java 1.2 platform and JDBC. Because the focus of this book is on developing vendor-independent Enterprise JavaBeans components and solutions, I have stayed away from proprietary extensions and vendor-dependent idioms. Any EJB-compliant server can be used with this book, but you should be familiar with your server's specific installation, deployment, and runtime-management procedures to work with the examples.
EJB 2.0 and 1.1 have a lot in common, but when they differ, chapters or sections within a chapter that are specific to each version are clearly marked. Feel free to skip version-specific sections that do not concern you. Unless indicated, the source code in this book has been written for both EJB 2.0 and 1.1.
Examples developed in this book are available from ftp://ftp.oreilly.com/pub/examples/java/ejb/. The examples are organized by chapter.
Although EJB applications themselves are portable, the manner in which you install and run EJB products varies widely from one vendor to the next. For this reason it is nearly impossible to cover all the EJB products available, so I have chosen a radical but effective way to address these differences: workbooks.
To help you deploy the book examples in different EJB products, I will publish several free workbooks that you can use along with this book to run the examples on specific commercial and noncommercial EJB servers. The workbook for a specific product will address that product's most advanced server. So, for example, if the vendor supports EJB 2.0, the examples in the workbook will address EJB 2.0 features. If, on the other hand, the vendor supports only EJB 1.1, the examples in the workbook will be specific to EJB 1.1.
Although I plan to publish workbooks for several different EJB servers, at least two workbooks will be made available immediately. These workbooks will be available free online (in PDF format) at http://www.oreilly.com/catalog/entjbeans3/ or http://www.titan-books.com.
The following typographical conventions are used in this book:
Constant width bold
An Enterprise JavaBean consists of many parts; it's not a single object, but a collection of objects and interfaces. To refer to an enterprise bean as a whole, we use its business name in Roman type, followed by the acronym EJB. For example, we will refer to the Customer EJB when we want to talk about the enterprise bean in general. If we put the name in a constant-width font, we are referring explicitly to the bean's remote interface; thus, CustomerRemote is the remote interface that defines the business methods of the Customer EJB.
Please address comments and questions concerning this book to the publisher:
There is a web page for this book, which lists errata, examples, and any additional information. You can access this page at:
To comment on or ask technical questions about this book, send email to:
For more information about books, conferences, software, Resource Centers, and the O'Reilly Network, see the O'Reilly web site at:
The author maintains a web site for the discussion of EJB and related distributed computing technologies at http://www.jmiddleware.com. jMiddleware.com provides news about this book as well as code tips, articles, and an extensive list of links to EJB resources.
While there is only one name on the cover of this book, the credit for its development and delivery is shared by many individuals. Michael Loukides, my editor, was pivotal to the success of every edition of this book. Without his experience, craft, and guidance, this book would not have been possible. Other people at O'Reilly & Associates, including Rachel Wheeler, Rob Romano, Kyle Hart, and many others, were also very important to the success of this book.
Many expert technical reviewers helped ensure that the material was technically accurate and true to the spirit of Enterprise JavaBeans. Of special note are Greg Nyberg of ObjectPartners, Hemant Khandelwal of Pramati, Kyle Brown of IBM, Robert Castaneda of CustomWare, Joe Fialli of Sun Microsystems, Anil Sharma and Priyank Rastogi of Pramati, Seth White of BEA, Evan Ireland and Meyer Tanuan of Sybase, David Chappell of David Chappell & Associates, Jim Farley, author of Java™ Distributed Computing (O'Reilly), and Prasad Muppirala of BORN Information Services. They contributed greatly to the technical accuracy of this book and brought a combination of industry and real-world experience to bear, helping to make this one of the best books on Enterprise JavaBeans published today.
I would also like to thank everyone in the community who provided valuable feedback by reviewing this book while it was being written at http://oreilly.techrev.org, including (in alphabetical order): Dion Almaer, Jim Archer, Stephen Davies, John De la Cruz, Tom Gullo, Marty Harvey, Hai Hoang, Meeraj Kunnumpurath, Tom Larson, Bjarne Rasmussen, rgparker (name unknown), Larry Seltzer, and Curt Smith.
Special thanks also go to Sriram Srinivasan of BEA, Anne Thomas of Sun Microsystems, Ian McCallion of IBM Hursley, Tim Rohaly of jGuru.com, James D. Frentress of ITM Corp., Andrzej Jan Taramina of Accredo Systems, Marc Loy, coauthor of Java™ Swing (O'Reilly), Don Weiss of Step 1, Mike Slinn of The Dialog Corporation, and Kevin Dick of Kevin Dick & Associates. The contributions of these technical experts were critical to the technical and conceptual accuracy of earlier editions of this book. Others I would like to thank include Maggie Mezquita, Greg Hartzel, John Klug, and Jon Jamsa of BORN Information who all suffered though the first draft of the first edition so long ago to provide valuable feedback.
Thanks also to Vlad Matena and Mark Hapner of Sun Microsystems, the primary architects of Enterprise JavaBeans; Linda DeMichiel, EJB 2.0 specification lead; and Bonnie Kellett, J2EE Program Manager—they were all willing to answer several of my most complex questions. Thanks to all the participants in the EJB-INTEREST mailing list hosted by Sun Microsystems for their interesting and sometimes controversial, but always informative, postings over the past four years.
Finally, I extend the most sincere gratitude to my wife, Hollie, for supporting and assisting me through the three years of painstaking research and writing that were required to produce three editions of this book. Without her unfailing support and love, this book would not have been completed.