Chapter 7 Enterprise JavaBeans Overview


About Enterprise JavaBeans components

The Enterprise JavaBeans (EJB) technology defines a model for the development and deployment of reusable Java server components, called EJB components.

An EJB component is a nonvisual server component with methods that typically provide business logic in distributed applications. A remote client, called an EJB client, can invoke these methods, which typically results in the updating of a database. Since Jaguar uses CORBA as the basis for the EJB component support, EJB components running in Jaguar can be called by any other type of Jaguar client or component, and even CORBA clients using ORBs from other vendors that are compatible with CORBA 2.3.

The EJB architecture looks like this:


EJB server The EJB server contains the EJB container, which provides the services required by the EJB component. Jaguar CTS is an EJB server.

EJB client An EJB client usually provides the user-interface logic on a client machine. The EJB client makes calls to remote EJB components on a server and needs to know how to find the EJB server and how to interact with the EJB components. An EJB component can act as a EJB client by calling methods in another EJB component.

An EJB client does not communicate directly with an EJB component. The container provides proxy objects that implement the components home and remote interfaces. The component's remote interface defines the business methods that can be called by the client. The client calls the home interface methods to create and destroy proxies for the remote interface.

EJB container The EJB specification defines a container as the environment in which one or more EJB components execute. The container provides the infrastructure required to run distributed components, allowing client and component developers to focus on programming business logic, and not system-level code. In Jaguar, the container encapsulates:

EJB component implementation The Java class that runs in the server implements the Bean's business logic. The class must implement the remote interface methods and additional methods for lifecycle management.

EJB component types

You can implement three types of EJB component, each for a different purpose:

Stateful session Beans

A stateful session Bean manages complex processes or tasks that require the accumulation of data, such as adding items to a Web catalog's shopping cart. Stateful session Beans have the following characteristics:

For example, if you create a session Bean on a Web server that tracks a user's path through the site, the session Bean is destroyed when the user leaves the site or idles beyond a specified time

Stateless session Beans

A stateless session Bean manages tasks that do not require the keeping of client session data between method calls. Stateless session Beans have the following characteristics:

Unlike stateful session Beans, stateless session Beans can be pooled by the server, improving overall application performance.

Entity Beans

An entity Bean models a business concept that is a real-world object. For example, an entity Bean might represent a scheduled airplane flight, a seat on the airplane, or a passenger's frequent-flyer account. Entity Beans have the following characteristics:

EJB transaction attribute values

Each EJB component has a transaction attribute that determines how instances of the component participate in transactions. In Jaguar, you set the transaction attribute in the Transaction tab of the Component Properties dialog box.

When you design an EJB component, you must decide how the Bean will manage transaction demarcation: either programmatically in the business methods, or whether the transaction demarcation will be managed by the container based on the value of the transaction attribute in the deployment descriptor.

A session Bean can use either Bean-managed transaction demarcation or with container-managed transaction demarcation; you cannot create a session Bean where some methods use container-managed demarcation and others use Bean-managed demarcation. An entity Bean must use container-managed transaction demarcation.

Table 7-1 lists the transaction attribute values. Requires, Supports, Requires New, or Mandatory are the values that specify container-managed transaction demarcation. You can set the Transaction Attribute for the component and for individual methods in the home and remote interfaces. Values set at the method level override the component setting.

Table 7-1: Transaction attribute values

Attribute

Description

Not Supported

(The component-level default.) The EJB component's methods never execute as part of a transaction. If the EJB component is activated by a client that has a pending transaction, the EJB component's work is performed outside the existing transaction.

Since entity Beans are almost always involved in transactions, this value is not usually used for an entity Bean.

Supports

The EJB component can execute in the context of a Jaguar transaction, but a transaction is not required to execute the component's methods. If a method is called by a base client that has a pending transaction, the method's database work occurs in the scope of the client's transaction. Otherwise, the EJB component's database work is done outside of any transaction.

Required

The EJB component always executes in a transaction. Use this option when your EJB component's database activity needs to be coordinated with other components, so that all components participate in the same transaction.

Requires New

Whenever the EJB component is instantiated, a new transaction begins.

Mandatory

EJB component methods must be called in the context of a pending transaction. If a client calls a method without an open transaction, the Jaguar ORB throws an exception.

Never

The component's methods never execute as part of a transaction, and the component may cannot be called in the context of a transaction. If a client or another component calls the component with an outstanding transaction, Jaguar throws an exception.

Bean Managed

(For EJB session Beans only.) The EJB component can explicitly begin, commit, and roll back new, independent transactions by using the javax.transaction.UserTransaction interface. Transactions begun by the component execute independently of the client's transaction. If the component has not begun a transaction, the component's database work is performed independently of any Jaguar transaction.

Default to component

(Method-level default) In the Transactions tab of the Method properties window, choose this option if the method should inherit the transaction attribute set in the component properties.

EJB container services

The EJB container provides services to EJB components. The services include transaction and persistence support.

Transaction support An EJB container must support transactions. EJB specifications provide an approach to transaction management called declarative transaction management. In declarative transaction management, you specify the type of transaction support required by your EJB component. When the Bean is deployed, the container provides the necessary transaction support.

Persistence support An EJB container can provide support for persistence of EJB components. An EJB component is persistent if it is capable of saving and retrieving its state. A persistent EJB component saves its state to some type of persistent storage (usually a file or a database). With persistence, an EJB component does not have to be re-created with each use.

An EJB component can manage its own persistence (by means of the logic you provide in the Bean) or delegate persistence services to the EJB container. Container-managed persistence means that the data appears as member data and the container performs all data retrieval and storage operations for the EJB component. See Chapter 29, "Managing Persistent Component State" for more information.

 


Copyright © 2000 Sybase, Inc. All rights reserved.