Cover image for Enterprise JavaBeans, Third Edition

Book description

Enterprise JavaBeans was recently voted "Best Java Book" by the editors and readers of Java Developer's Journal. Readers of JavaPro named it the "Best Java Book for Experts." And Amazon.com included it in the Top Computer Books for 2000. Now the best only gets better! In the new 3rd edition, Enterprise JavaBeans has been completely revised and updated with a thorough introduction to the new 2.0 version of the EJB specification. Significantly different from the earlier version, the 2.0 specification introduces three dramatic improvements: A completely new version of container-managed persistence; local interfaces; and a totally new kind of bean called the "message driven bean." The new version of container-managed persistence (CMP) beans in 2.0 is more portable and robust than in the older 1.1 version. Most significant is the introduction of the relationship fields, which allow entity beans to declare relationships with each other as natural references. In order to make this huge leap in component relationships possible, EJB 2.0 had to redesign how entity beans are defined and interact. Our new 3rd edition examines this critical CMP model in detail. Local interfaces are thoroughly discussed as well. Local interfaces allow beans that are co-located to interact without the overhead of remote method calls. This improves the performance of beans considerably and complements the CMP relationship fields. Message driven beans are a new kind of enterprise bean based on asynchronous messaging and the Java Message service (JMS). Instead of responding to Java RMI calls, message driven beans process JMS messages sent by messaging clients. An entire chapter is devoted to message-driven beans and how to use them effectively. In addition, the 3rd edition contains an architecture overview, information on resource management and primary services, design strategies, and XML deployment descriptors.

Table of Contents

  1. Enterprise JavaBeans, 3rd Edition
    1. Dedication
    2. Preface
      1. Author’s Note
      2. What Is Enterprise JavaBeans?
      3. Who Should Read This Book?
      4. Organization
      5. Software and Versions
      6. Example Workbooks
      7. Conventions
      8. Comments and Questions
      9. Acknowledgments
    3. 1. Introduction
      1. Setting the Stage
        1. Distributed Objects
        2. Server-Side Components
        3. Component Transaction Monitors
      2. Enterprise JavaBeans Defined
      3. Distributed Object Architectures
        1. Rolling Your Own Distributed Object
      4. Component Models
      5. Component Transaction Monitors
        1. Transaction Processing Monitors
        2. Object Request Brokers
        3. CTMs: The Hybrid of ORBs and TP Monitors
        4. Analogies to Relational Databases
      6. CTMs and Server-Side Component Models
        1. Microsoft’s .NET Framework
        2. EJB and CORBA CTMs
        3. Benefits of a Standard Server-Side Component Model
        4. EJB 2.0: Asynchronous Messaging
          1. Java Message Service
          2. Message-driven beans
      7. Titan Cruises: An Imaginary Business
      8. What’s Next?
    4. 2. Architectural Overview
      1. The Enterprise Bean Component
        1. Classes and Interfaces
          1. Naming conventions
          2. The remote interface
          3. The remote home interface
          4. EJB 2.0: The bean class
          5. EJB 1.1: The bean class
          6. EJB 2.0 and 1.1: The bean class
          7. The primary key
          8. What about session beans?
          9. EJB 2.0: What about message-driven beans?
        2. Deployment Descriptors and JAR Files
          1. EJB 2.0: Deployment descriptor
          2. EJB 1.1: Deployment descriptor
          3. EJB 2.0 and 1.1: Elements of the XML deployment descriptor
        3. EJB Objects and EJB Home
          1. The EJB object
          2. The EJB home
          3. Deploying a bean
      2. Using Enterprise Beans
        1. Getting Information from an Entity Bean
        2. Modeling Workflow with Session Beans
          1. Stateless and stateful session beans
        3. EJB 2.0: Accessing EJB with Message-Driven Beans
      3. The Bean-Container Contract
        1. The Container-Server Contract
      4. Summary
    5. 3. Resource Management and the Primary Services
      1. Resource Management
        1. Instance Pooling
          1. The entity bean life cycle
          2. Overview of state transitions
          3. Instance swapping
          4. EJB 2.0: Message-driven beans and instance pooling
        2. The Activation Mechanism
      2. Primary Services
        1. Concurrency
          1. Concurrency with session and entity beans
            1. Reentrance
          2. EJB 2.0: Concurrency with message-driven beans
        2. Transactions
        3. Persistence
          1. Object-to-relational persistence
          2. Object database persistence
          3. Legacy persistence
        4. Distributed Objects
        5. EJB 2.0: Asynchronous Enterprise Messaging
        6. Naming
        7. Security
          1. Role-driven access control
          2. EJB 2.0: Unchecked methods
          3. EJB 2.0: The runAs security identity
        8. EJB 2.0: Primary Services and Interoperability
      3. What’s Next?
    6. 4. Developing Your First Enterprise Beans
      1. Choosing and Setting Up an EJB Server
        1. Setting Up Your Java IDE
      2. Developing an Entity Bean
        1. Cabin: The Remote Interface
        2. CabinHome: The Remote Home Interface
        3. CabinBean: The Bean Class
          1. EJB 2.0: The CabinBean class
          2. EJB 1.1: The CabinBean class
          3. EJB 2.0 and 1.1: The callback methods
        4. The Deployment Descriptor
          1. EJB 2.0: The Cabin EJB’s deployment descriptor
          2. EJB 1.1: The Cabin EJB’s deployment descriptor
          3. EJB 2.0 and 1.1: Defining the XML elements
        5. cabin.jar: The JAR File
        6. Creating a CABIN Table in the Database
        7. Deploying the Cabin EJB
        8. Creating a Client Application
          1. Creating a new Cabin EJB
      3. Developing a Session Bean
        1. TravelAgentRemote: The Remote Interface
        2. TravelAgentHomeRemote: The Remote Home Interface
        3. TravelAgentBean: The Bean Class
        4. The TravelAgent EJB’s Deployment Descriptor
          1. EJB 2.0: Deployment descriptor
          2. EJB 1.1: Deployment descriptor
          3. EJB 2.0 and 1.1: Defining the XML elements
        5. Deploying the TravelAgent EJB
        6. Creating a Client Application
    7. 5. The Client View
      1. Locating Beans with JNDI
      2. The Remote Client API
        1. Java RMI-IIOP
          1. Java RMI return types, parameters, and exceptions
            1. Return types and parameters
            2. Exceptions
          2. Java RMI-IIOP type restrictions
          3. Explicit narrowing using PortableRemoteObject
        2. The Remote Home Interface
          1. Removing beans
          2. Bean metadata
          3. The HomeHandle
          4. Creating and finding beans
          5. EJB 2.0: Home methods
        3. The Remote Interface
        4. EJBObject, Handle, and Primary Key
          1. Getting the EJBHome
          2. Primary key
          3. Comparing beans for identity
          4. Removing beans
          5. The enterprise bean Handle
          6. HomeHandle
          7. Inside the Handle
      3. EJB 2.0: The Local Client API
        1. The Local Interface
        2. The Local Home Interface
        3. Deployment Descriptor
        4. Using the Local Client API
        5. When to Use Local Component Interfaces
        6. Are Local Component Interfaces Necessary?
    8. 6. EJB 2.0 CMP: Basic Persistence
      1. Overview
        1. The Abstract Programming Model
        2. Abstract Persistence Schema
        3. Container Tools and Persistence
      2. The Customer EJB
        1. The Customer Table
        2. The CustomerBean
        3. The Remote Interface
        4. The Remote Home Interface
        5. The XML Deployment Descriptor
        6. The EJB JAR File
        7. Deployment
        8. The Client Application
      3. Persistence Fields
      4. Dependent Value Classes
      5. Relationship Fields
    9. 7. EJB 2.0 CMP: Entity Relationships
      1. The Seven Relationship Types
        1. Abstract Persistence Schema
        2. Database Modeling
        3. One-to-One Unidirectional Relationship
          1. Relational database schema
          2. Abstract programming model
          3. Abstract persistence schema
        4. One-to-One Bidirectional Relationship
          1. Relational database schema
          2. Abstract programming model
          3. Abstract persistence schema
        5. One-to-Many Unidirectional Relationship
          1. Relational database schema
          2. Abstract programming model
          3. Abstract persistence schema
        6. The Cruise, Ship, and Reservation EJBs
        7. Many-to-One Unidirectional Relationship
          1. Relational database schema
          2. Abstract programming model
          3. Abstract persistence schema
        8. One-to-Many Bidirectional Relationship
          1. Relational database schema
          2. Abstract programming model
          3. Abstract persistence schema
        9. Many-to-Many Bidirectional Relationship
          1. Relational database schema
          2. Abstract programming model
          3. Abstract persistence schema
        10. Many-to-Many Unidirectional Relationship
          1. Relational database schema
          2. Abstract programming model
          3. Abstract persistence schema
        11. Co-location and the Deployment Descriptor
        12. Cascade Delete and Remove
    10. 8. EJB 2.0 CMP: EJB QL
      1. Declaring EJB QL
      2. The Query Methods
        1. Find Methods
        2. Select Methods
      3. EJB QL Examples
        1. Simple Queries
        2. Simple Queries with Paths
        3. The IN Operator
        4. Using DISTINCT
        5. The WHERE Clause and Literals
        6. The WHERE Clause and Input Parameters
        7. The WHERE Clause and Operator Precedence
        8. The WHERE Clause and CDATA Sections
        9. The WHERE Clause and Arithmetic Operators
        10. The WHERE Clause and Logical Operators
        11. The WHERE Clause and Comparison Symbols
        12. The WHERE Clause and Equality Semantics
        13. The WHERE Clause and BETWEEN
        14. The WHERE Clause and IN
        15. The WHERE Clause and IS NULL
        16. The WHERE Clause and IS EMPTY
        17. The WHERE Clause and MEMBER OF
        18. The WHERE Clause and LIKE
        19. The WHERE Clause and Functional Expressions
      4. Problems with EJB QL
        1. The OBJECT( ) Operator
        2. The Missing ORDER BY Clause
        3. Lack of Support for Date
        4. Limited Functional Expressions
    11. 9. EJB 1.1 CMP
      1. A Note for EJB 2.0 Readers
      2. Overview for EJB 1.1 Readers
      3. Container-Managed Persistence
        1. The Remote Interface
        2. The Remote Home Interface
          1. The find methods
        3. The Primary Key
        4. The ShipBean Class
        5. Implementing the javax.ejb.EntityBean Interface
        6. The Create Methods
          1. ejbCreate( ) and ejbPostCreate( )
          2. Using ejbLoad( ) and ejbStore( ) in container-managed beans
        7. Deployment Descriptor
    12. 10. Bean-Managed Persistence
      1. The Remote Interface
        1. Set and Get Methods
      2. The Remote Home Interface
      3. The Primary Key
      4. The ShipBean
      5. Obtaining a Resource Connection
      6. Exception Handling
      7. The ejbCreate( ) Method
      8. The ejbLoad( ) and ejbStore( ) Methods
      9. The ejbRemove( ) Method
      10. The ejbFind( ) Methods
      11. The Deployment Descriptor
    13. 11. The Entity-Container Contract
      1. The Primary Key
        1. Single-Field Primary Keys
        2. Compound Primary Keys
        3. Undefined Primary Keys
      2. The Callback Methods
        1. setEntityContext( ) and unsetEntityContext( )
        2. ejbCreate( )
        3. ejbCreate( ) and ejbPostCreate( ) Sequence of Events
        4. Using ejbLoad( ) and ejbStore( ) in Container-Managed Persistence
        5. Using ejbLoad( ) and ejbStore( ) in Bean-Managed Persistence
        6. ejbPassivate( ) and ejbActivate( )
        7. ejbRemove( )
      3. EJB 2.0: ejbHome( )
      4. EntityContext
        1. EJBContext
        2. JNDI ENC
      5. The Life Cycle of an Entity Bean
        1. Does Not Exist
        2. The Pooled State
        3. The Ready State
          1. Transitioning from the Pooled state to the Ready state via creation
          2. Transitioning from the Pooled state to the Ready state via a query method
          3. Transitioning from the Pooled state to the Ready state via activation
          4. Transitioning from the Ready state to the Pooled state via passivation
          5. Transitioning from the Ready state to the Pooled state via removal
        4. Life in the Ready State
        5. End of the Life Cycle
    14. 12. Session Beans
      1. The Stateless Session Bean
        1. The ProcessPayment EJB
          1. PAYMENT: The database table
          2. ProcessPaymentRemote: The remote interface
          3. Dependent objects: The CreditCardDO and CheckDO classes
          4. PaymentException: An application exception
          5. ProcessPaymentHomeRemote: The home interface
          6. ProcessPaymentBean: The bean class
          7. JNDI ENC: Accessing environment properties
          8. EJBContext
          9. The ProcessPayment EJB’s deployment descriptor
          10. EJB 2.0: Local component interfaces
      2. The Life Cycle of a Stateless Session Bean
        1. Does Not Exist
        2. The Method-Ready Pool
          1. Transitioning to the Method-Ready Pool
          2. Life in the Method-Ready Pool
          3. Transitioning out of the Method-Ready Pool: The death of a stateless bean instance
      3. The Stateful Session Bean
        1. EJB 2.0: Getting Set Up for the TravelAgent EJB
        2. EJB 1.1: Getting Set Up for the TravelAgent EJB
        3. The TravelAgent EJB
          1. TravelAgent: The remote interface
          2. Dependent object: TicketDO
          3. TravelAgentHomeRemote: The home interface
          4. Taking a peek at the client view
          5. TravelAgentBean: The bean class
          6. JNDI ENC and EJB references
            1. Remote EJB references in the JNDI ENC
            2. EJB 2.0: Local EJB references in the JNDI ENC
          7. The bookPassage( ) method
          8. Why use a Reservation entity bean?
          9. listAvailableCabins( ): Listing behavior
          10. EJB 2.0: The TravelAgent deployment descriptor
          11. EJB 1.1: The TravelAgent deployment descriptor
      4. The Life Cycle of a Stateful Session Bean
        1. Does Not Exist State
        2. Method-Ready State
          1. Transitioning to the Method-Ready state
          2. Life in the Method-Ready state
          3. Transitioning out of the Method-Ready state
        3. Passivated State
          1. System exceptions
    15. 13. Message-Driven Beans
      1. JMS as a Resource
        1. Reimplementing the TravelAgent EJB with JMS
          1. TopicConnectionFactory and Topic
          2. TopicConnection and TopicSession
          3. TopicPublisher
          4. Message types
          5. XML deployment descriptor
          6. JMS application client
        2. JMS Is Asynchronous
        3. JMS Messaging Models: Publish-and-Subscribe and Point-to-Point
          1. Publish-and-subscribe
          2. Point-to-point
          3. Which messaging model should you use?
        4. Entity and Session Beans Should Not Receive Messages
        5. Learning More About JMS
      2. Message-Driven Beans
        1. The ReservationProcessor EJB
          1. The ReservationProcessorBean Class
          2. MessageDrivenBean interface
          3. MessageDrivenContext
          4. MessageListener interface
          5. The onMessage( ) method: Workflow and integration for B2B
          6. Sending messages from a message-driven bean
          7. XML deployment descriptor
          8. <message-selector>
          9. Message selector examples
            1. Managing claims in an HMO
            2. Notification of certain bids on inventory
            3. Selecting recipients for a catalog mailing
          10. <acknowledge-mode>
          11. <message-driven-destination>
          12. The ReservationProcessor clients
            1. The reservation message producer
            2. The ticket message consumer
        2. The Life Cycle of a Message-Driven Bean
          1. Does Not Exist
          2. The Method-Ready Pool
          3. Transitioning to the Method-Ready Pool
          4. Life in the Method-Ready Pool
          5. Transitioning out of the Method-Ready Pool: The death of an MDB instance
    16. 14. Transactions
      1. ACID Transactions
        1. Is the TravelAgent EJB Atomic?
        2. Is the TravelAgent EJB Consistent?
        3. Is the TravelAgent EJB Isolated?
        4. Is the TravelAgent EJB Durable?
      2. Declarative Transaction Management
        1. Transaction Scope
        2. Transaction Attributes
          1. Setting a transaction attribute
          2. Transaction attributes defined
          3. EJB 2.0: Container-managed persistence and transaction attributes
          4. EJB 2.0: Message-driven beans and transaction attributes
        3. Transaction Propagation
        4. EJB 2.0: Collection-Based Relationships and Transactions
      3. Isolation and Database Locking
        1. Dirty, Repeatable, and Phantom Reads
          1. Dirty reads
          2. Repeatable reads
          3. Phantom reads
        2. Database Locks
        3. Transaction Isolation Levels
        4. Balancing Performance Against Consistency
          1. Controlling isolation levels
      4. Nontransactional Beans
      5. Explicit Transaction Management
        1. Transaction Propagation in Bean-Managed Transactions
          1. EJB 2.0: TravelAgentBean
          2. EJB 1.1: TravelAgentBean
          3. EJB 2.0: Message-driven beans and bean-managed transactions
        2. Heuristic Decisions
        3. UserTransaction
        4. Status
        5. EJBContext Rollback Methods
      6. Exceptions and Transactions
        1. Application Exceptions Versus System Exceptions
          1. EJB 2.0: bookPassage( ) method
          2. EJB 1.1: bookPassage( ) method
          3. System exceptions
          4. Application exceptions
      7. Transactional Stateful Session Beans
        1. The Transactional Method-Ready State
          1. Transitioning into the Transactional Method-Ready state
          2. Life in the Transactional Method-Ready state
    17. 15. Design Strategies
      1. Hash Codes in Compound Primary Keys
        1. Well-Distributed Versus Unique Hash Codes
      2. Passing Objects by Value
        1. EJB 1.1: Dependent Objects
        2. Validation Rules in Dependent Objects
        3. Bulk Accessors for Remote Clients
          1. Rules-of-thumb for bulk accessors
        4. Entity Objects
      3. Improved Performance with Session Beans
        1. Network Traffic and Latency with Remote Clients
        2. Striking a Balance with Remote Clients
        3. Listing Behavior
          1. Implementing lists as arrays of structures
          2. Implementing lists as ResultSets
      4. Bean Adapters
      5. Implementing a Common Interface
        1. Why the Bean Class Should Not Implement the Remote Interface
        2. EJB 2.0: Why the Bean Class Should Not Implement the Local Interface
        3. EJB 1.1: The Business Interface Alternative
      6. Entity Beans Without Create Methods
      7. EJB 1.1: Object-to-Relational Mapping Tools
      8. Avoid Emulating Entity Beans with Session Beans
      9. Direct Database Access from Session Beans
      10. Avoid Chaining Stateful Session Beans
    18. 16. XML Deployment Descriptors
      1. What Is an XML Deployment Descriptor?
      2. The Contents of a Deployment Descriptor
      3. The Document Header
      4. The Descriptor’s Body
      5. Describing Enterprise Beans
        1. Session and Entity Beans
        2. Message-Driven Beans
        3. Specifying Primary Keys
          1. Deferring primary key definition
        4. Environment Entries
        5. References to Other Beans
          1. Remote references
          2. EJB 2.0: Local references
        6. References to External Resources
          1. EJB 2.0: Additional administered objects
          2. EJB 2.0: Shareable resources
        7. Security Roles
        8. Declaring EJB QL Elements
      6. EJB 2.0: Describing Relationships
      7. Describing Bean Assembly
        1. Specifying a Bean’s Transaction Attributes
        2. Specifying Security Roles and Method Permissions
          1. Assigning roles to methods
          2. EJB 2.0: Unchecked methods
          3. EJB 2.0: The runAs security identity
          4. EJB 2.0: Exclude list
        3. Identifying Specific Methods
          1. Wildcard declarations
          2. Named method declarations
          3. Specific method declarations
          4. Remote/home/local differentiation
      8. The ejb-jar File
        1. The client-jar File
    19. 17. Java 2, Enterprise Edition
      1. Servlets
      2. JavaServer Pages
      3. Web Components and EJB
      4. J2EE Fills in the Gaps
        1. J2EE Application Client Components
        2. Guaranteed Services
      5. Fitting the Pieces Together
      6. Future Enhancements
    20. A. The Enterprise JavaBeans API
      1. Package: javax.ejb
        1. EJB 2.0: AccessLocalException
        2. CreateException
        3. DuplicateKeyException
        4. EJBContext
        5. EJBException
        6. EJBHome
        7. EJB 2.0: EJBLocalHome
        8. EJB 2.0: EJBLocalObject
        9. EJBMetaData
        10. EJBObject
        11. EnterpriseBean
        12. EntityBean
        13. EntityContext
        14. FinderException
        15. Handle
        16. HomeHandle
        17. EJB 2.0: MessageDrivenBean
        18. EJB 2.0: MessageDrivenContext
        19. NoSuchEntityException
        20. ObjectNotFoundException
        21. RemoveException
        22. SessionBean
        23. SessionContext
        24. SessionSynchronization
        25. EJB 2.0: TransactionRequiredLocalException
        26. EJB 2.0: TransactionRolledbackLocalException
      2. EJB 2.0: Package: javax.jms
        1. MessageListener
      3. EJB 2.0: Package: javax.ejb.spi
        1. HandleDelegate
    21. B. State and Sequence Diagrams
      1. Entity Beans
        1. Life Cycle State Diagram of the Entity Bean
        2. Sequence Diagrams for Container-Managed Persistence
        3. Sequence Diagrams for Bean-Managed Persistence
      2. Session Beans
        1. Stateless Session Beans
        2. Stateful Session Beans
        3. Message-Driven Beans
        4. Interactions Between Exceptions and Transactions
    22. C. EJB Vendors
      1. Commercial Products
      2. Open Source Projects
    23. Index
    24. Colophon