Você está na página 1de 30

EJB Key Concept

Feb. 14, 2001

Soo Dong Kim,

Ph.D.

Object Technology Laboratory Tel. 820-0909 Fax. 826-0909 sdkim@comp.ssu.ac.kr object.soongsil.ac.kr

Contents
EJB Overview Session Bean
Stateless Session Bean Stateful Seesion Bean

Entity Bean
Bean-managed persistence Container-managed persistence

Transaction
2000 Soo Dong Kim EJB Key Concept

Terminology
Enterprise JavaBeans
Is an architecture for component-based distributed computing.

Enterprise Beans
Are components of distributed transactionoriented enterprise application.

EJB Key Concept

Enterprise JavaBeans (EJB)


Enterprise JavaBeans
EJB define how server-side components are written and provides a standard contract between components and the application servers that manage them. EJB promote the spawning of a component marketplace, where vendors can sell reusable components that can be purchased to help solve business problems.
2000 Soo Dong Kim EJB Key Concept

2000 Soo Dong Kim

Enterprise JavaBeans
A server-side component architecture
Allows developers to quickly create scalable enterprise applications using java Provides complex middleware enterprise features at no cost
Transactions, persistence, security, state management, threading

Cross-platform, cross-vendor
2000 Soo Dong Kim

There is one standard, allowing the vendors to focus on the quality of their EJB products.

EJB Key Concept

Enterprise Beans
Essential characteristics :
An enterprise Bean contains business logic. An enterprise Beans instances are created and managed at runtime by a container. An enterprise Bean can be customized at deployment time by editing its environment properties. Various metadata, such as a transaction mode and security attributes, are separated out from the enterprise Bean class.

EJB Key Concept

2000 Soo Dong Kim

Enterprise Beans
Client access is mediated by the container and the EJB server. If an enterprise Bean uses only the standard container services, the enterprise Bean can be deployed in any EJB container. Specialized containers can provide additional services. An enterprise Bean can be included in a composite application without source code changes or recompilation.

EJB Key Concept

Roles
Enterprise JavaBeans architecture
Defines six distinct roles in the application development and deployment life cycle.
Enterprise Bean Provider
An application domain expert Develops reusable components called enterprise Beans.

Application Assembler
Combines enterprise beans into larger deployable application units. Output is one or more ejb-jar files that contain the enterprise beans with their application assembly instruction.

EJB Key Concept

2000 Soo Dong Kim

2000 Soo Dong Kim

Roles
Deployer
An expert at a specific operational environment. Is responsible for the deployment of enterprise Beans and their containers.

EJB Server Provider


A specialist in the area of distributed transaction management, distributed objects, and other lower-level system-level services.

EJB Container Provider


The expertise of a container provider is system-level programming, possibly combined with some applicationdomain experts.

System Administrator
The role of a system administrator is to oversee the wellbeing of a running system.
EJB Key Concept

Roles Map
Container Provider Deployer Tool

Assembly Instructions

Application assembler

Bean Provider manage


Combine

Administrator EJB-JARs EJB Server Provider

EJB Key Concept

10

2000 Soo Dong Kim

2000 Soo Dong Kim

EJB Contracts
Clients view contract
Contract between a client and a container. Includes
Home interface
Defines the methods for the client to create, remove, and find EJB Objects. Client locates an home interface through the JNDI API.

Remote Interface
Defines the business methods callable by the client.

Object identity
Container must generating a new unique identifier for each Session Object. The identifier is not exposed to the client. Bean Provider must supply a primary key at Entity Object creation time

Metadata interface Handle

EJB Key Concept

11

EJB Contracts
Component contract
An enterprise Bean class instances view of its life cycle. javax.ejb.SessionContext interface that a container passes to a session enterprise Bean instance at instance creation. Environment java.util.Properties that a container makes available to an enterprise Bean. List of services that every container must provide for its enterprise Beans.
EJB Key Concept

12

2000 Soo Dong Kim

2000 Soo Dong Kim

EJB Contracts

Client

Clients view Contract

Enterprise Bean
Container Contract

Container
2000 Soo Dong Kim EJB Key Concept

EJB Server

13

Enterprise Beans
Types of Enterprise Beans
Session Bean
Stateless session bean Stateful session bean

Entity Bean
Bean-managed persistent Container-managed persistent

EJB Key Concept

14

2000 Soo Dong Kim

Session Bean Overview


Session Bean
Non-persistent object Is a logical extension of the client program that runs on the server. Is not shared among multiple clients.

Lifetime
Might live for minutes or hours
but probably not weeks, months, or years. Not saved to permanent storage.
EJB Key Concept 2000 Soo Dong Kim

Session beans are no persistent.

15

Session Bean Overview


Stateless Session bean
Store no conversational state from method to method. Easily reused by multiple clients rather than destroyed and re-created per client.

Stateful Session bean


Components that hold conversations with clients.
2000 Soo Dong Kim EJB Key Concept

16

Session Bean Overview


All the functionality for remote access is provided by the container.
Security, concurrency, transactions, swapping to secondary storage

Each session EJB object has an identity


Which does not survive a crash and restart of the container.

EJB Key Concept

17

EJB Container
Is a system that functions as the container for enterprise Beans. Client look up the home interface for the EJB class using JNDI.
By the container

Container is where an Enterprise Bean object lives.


2000 Soo Dong Kim EJB Key Concept

A record lives in a database A file or directory lives in a file system

18

2000 Soo Dong Kim

Multiple EJB classes can be installed in a container. Container allows the clients to look up the home interfaces via JNDI. Home interface provides methods to create and remove the EJB objects.

EJB Container
Container
EJB objects

EJB class 1
EJB Home

Client
EJB objects

EJB class 2
2000 Soo Dong Kim EJB Key Concept

EJB Home

Other EJB Classes

19

Home Interface
EJB Container implements the home interface of each enterprise Bean.
The container makes the home interface available to the client through JNDI.

Home interface allows a client to do


Create a new EJB object. Remove an EJB object. Get the EJBMetaData interface for the enterprise Bean. Obtain a handle for the home object.
The home handle can be serialized.
EJB Key Concept

20

2000 Soo Dong Kim

EJB object
A client never accesses instances of the enterprise Beans class directly.
Uses the enterprise Beans remote interface.

An EJB object supports


Business logic methods Methods of the javax.ejb.EJBObject interface(is implemented by the container).
Get the EJB objects container. Get the EJB objects handle. Test if the EJB object is identical with another EJB object. Remove the EJB object.
EJB Key Concept

21

Session object identity


Session objects hide their identity.
Entity objects expose their identity as a primary key. Result in java.rmi.RemoteException
EJBObject.getPrimaryKey() and EJBHome.remove(Object primaryKey) methods

java.lang.RuntimeException
When the EJBMetaData.getPrimaryKeyClass() is invoked on a meta-data object for a Session Bean.
2000 Soo Dong Kim EJB Key Concept

22

2000 Soo Dong Kim

Session object identity


Since all session objects hide their identity,
There is no need to provide a finder for them. Home interface for a session object must not define any finder methods.

Session EJB object handle can be held beyond the life of a client process
By serializing the handle to persistent store.
2000 Soo Dong Kim EJB Key Concept

23

Clients view of Session Beans life cycle


Clients method on reference generates java.rmi.NoSuchObjectException

Does not exist and not referenced

Release reference

Does not exist and referenced

home.create() Container crash, or bean timeout

object.remove(), home.remove(), system exception in bean, bean timeout, or Container crash

Release reference

Clients method on reference


EJB Key Concept

24

2000 Soo Dong Kim

Exists and not referenced

Handle.getEJBObject( )

Exists and referenced

Object identity
Stateful Session Beans
has a unique identity that is assigned by the container at create time. isIdentical(Object otherObject)

Stateless Session Beans


All EJB objects have the same object identity. The isIdentical(Object otherObject) always returns true. javax.rmi.PortableRemoteObject.narrow(...)
EJB Key Concept 2000 Soo Dong Kim

Type narrowing

25

Stateless Session Bean


No Conversational State
Not customized for a particular client.

Only One Way to Initialize


Initialized with ejbCreate(), which takes no parameters.

Pool and Reuse


Containers can pool instances before clients connect. Support dynamically resizable pools over time.

EJB Object Decoupling


2000 Soo Dong Kim

Reassigned at any time to another EJB object. Important to know when debugging EJB applications.
EJB Key Concept

26

Stateful Session Bean


Conversational Beans
They hold conversations with clients that span multiple method invocations. They store conversational state within the bean. That conversational state is specific to a particular client.

EJB Key Concept

27

Effect of Pooling (Stateful Session Bean)


Pooling is not as simple as Stateless Session.
Each bean is storing state on behalf of a particular clients therefore container cant easily pool beans and dynamically assign.

EJB containers exploit the swapping paradigm .


To conserve stateful session bean resources Passivation - Swap out
After passivation, allow resources to be reclaimed. The bean resumes the conversation with the original client.
EJB Key Concept 2000 Soo Dong Kim

Activation - Swap in

28

2000 Soo Dong Kim

Effect of Pooling (Stateful Session Bean)


How does the container decide which beans to activate/ passivate?
To passivate beans
Specific to each container
Mostly LRU strategy

Container decides when passivation makes sense. One exception


any bean involved in a transaction cant be passivated until the transaction completes

.
2000 Soo Dong Kim

To activate beans
Most containers will use a just-in-time algorithm.
Beans should be activated on demand, as client requests come in.
EJB Key Concept

29

Passivation of Stateful Session Bean


Client
1.Involve business method 2. Pick the least recently used bean

EJB Object
Remote Interface

3. Call ejbPassivate() 4. Serialize the bean state

Enterprise Bean

5. Store passivated bean state

Other Enterprise Beans

EJB Key Concept

30

2000 Soo Dong Kim

Storage

Activation of Stateful Session Bean


Client
1.Involve business method 3.Reconstruct bean

EJB Object
Remote Interface

4.Call ejbActivate() 5. Invoke business method

Enterprise Bean

2. Retrieve passivated bean state

Other Enterprise Beans

EJB Key Concept

31

Entity Beans Overview


Entity Beans
A component that represents such persistent data. Do not contain business process logic. Multiple clients can use entity beans simultaneously. Does not perform complex tasks or workflow logic.

Lifetime
2000 Soo Dong Kim

Longer than a clients session Depending on how long the data sits in the database.
EJB Key Concept

32

2000 Soo Dong Kim

Storage

Features of Entity Beans


Long-lived. Survive failures. Entity bean instances are a view into a database. Several entity bean instances may represent the same underlying data. Entity bean instances can be pooled. Entity beans can be created, removed, or found. Entity beans can represent legacy data and legacy systems. Entity beans can be modified without going through EJB
EJB Key Concept

33

View into a Database


ejbLoad( )
Reads the data in from the persistent storage into the entity beans in-memory fields.

ejbStore( )
Saves your bean instances current fields to the underlying data storage.

EJB Key Concept

34

2000 Soo Dong Kim

2000 Soo Dong Kim

Can be Pool
Entity bean may be holding resources
Socket connections.

ejbActivate( )
When transitioning your bean out of a generic instance pool.
Callback that your container will invoke on your bean instance.

ejbPassivate( )
2000 Soo Dong Kim

When transitioning your bean into a generic instance pool.


Callback that your container will invoke.
EJB Key Concept

35

Passivation and Activation


Container
1: ejbStore() 2:ejbPassivate()

Bean Instance
Passivation entails a state save.

1:ejbActivate() 2:ejbLoad()

Activation entails a state load.

EJB Key Concept

36

2000 Soo Dong Kim

Creation of Entity Beans


ejbCreate()
Do not need to write any ejbCreate( ) methods with entity beans.
Session beans require ejbCreate() methods. Via direct database inserts or legacy systems. Is optional.

Parameters to ejbCreate() can vary.


Multiple ways to initialize an entity bean instance.

Duplicate methods in your home interface.


2000 Soo Dong Kim

Define a corresponding create() in the home interface.


EJB Key Concept

37

Creating Entity Bean.


EJB Container/Server
1: create()

Client Code
6:return EJB object

Home Object

2:ejbCreate()

4:return primary key

Enterprise Bean

5: Create EJB object

EJB Object
3:Create database data

Entity bean data


EJB Key Concept

38

2000 Soo Dong Kim

Entity Beans
ejbFind( )
Uses to find an existing entity bean in storage. Finder methods do not create any new database data.

ejbRemove()
To destroy an entity beans data in a database. Does not mean the in-memory entity bean instance is going to be destroyed. There is only one form of ejbRemove(). Is not called if the client times out.
EJB Key Concept

39

Entity Bean life cycle


Does not exist
1. newInstance() 2. setEntityContext(ec) Instance throws system exception from any method 1. unsetEntityContext() 2. JVM will garbage collect And call finalize() ejbFind<METHOD>()

pooled
ejbCreate(args) ejbActivate() ejbPostCreate(args)

ejbPassivate()

ejbRemove()

Business method
EJB Key Concept

40

2000 Soo Dong Kim

ejbLoad()

ready

ejbStore()

2000 Soo Dong Kim

Bean-managed persistence
Characteristic :
Entity bean that must be persisted by hand. Hard-code database access with in class.
You must write to a persistence API.

Not automated, developer manually creates mapping through JDBC calls. Entitity bean provider implement the entity beans persistence directly

Object Persistence
2000 Soo Dong Kim

Data access protocol for transferring the state of entity between the entity bean instances and the underlying database
EJB Key Concept

41

Container-managed persistence
Characteristic :
Automatic persistence for your entity beans The container/server performs every function of your components data access layer. Theoretical database independence The container provides tools that generate code in the EJB class. The bean provider must specify in the deployment descriptor the list of the instance fields for which the container provider tools must generate access calls.
EJB Key Concept

42

2000 Soo Dong Kim

Container-managed fields
Container-managed persistent entity bean
Allows the container to handle some or all of its data access logic.
Container implicitly perform all database operations behind the scenes.

Must make some of entity bean classs fields public.


Container can set the fields
When it performs database operations on behalf of your bean.
2000 Soo Dong Kim EJB Key Concept

43

Entity Beans can be modified.


Modified without going through EJB
EJB Container/Server

Entity Bean

Existing App

O/R Mapping

Direct Database Modifications

Entity Bean data

EJB Key Concept

44

2000 Soo Dong Kim

Transactions
Concept
Appear to execute as one large, atomic operation Guarantee an all-or-nothing value proposition
All of your operations will succeed, or none of them will.

Allow multiple users to share the same data Guarantee that any set of data they update will be completely .
2000 Soo Dong Kim EJB Key Concept

45

The Motivation for Transactions


A few motivational problems that transactions address
Atomic Operation Network or Machine Failure Multiple Users Sharing Data

EJB Key Concept

46

2000 Soo Dong Kim

The ACID Properties


Atomicity
If interrupted by failure, all effects are undone. Operations performed within a transaction undergo an all-or-noting paradigm.

Consistency
Effects of a transaction preserve invariant properties. A transaction will leave the systems state to be consistent after a transaction completes.

EJB Key Concept

47

The ACID Properties


Isolation
Intermediate states are invisible to others. Without isolation, your application state may become inconsistent. Locks on data are automatically assigned as necessary.

Durability
2000 Soo Dong Kim EJB Key Concept

Effects of a transaction are persistent. Updates to managed resources, such as database records, survive failures.

48

2000 Soo Dong Kim

Transactional Models
Flat Transactions
Performed atomically as a single unit of work. Decide to end the transaction
Successful transaction is committed.
All of the persistent operations become permanent changes.

Failed transaction is aborted.


All changes are rolled back

EJB Key Concept

49

Transactional Models
The Flat Transaction Process
Final State (Transaction Succeeded)

If all goes well, commit transaction

Begin transaction Initial State (No Transaction Occurring)

Transaction Occurring
If problem occurs, Abort transaction
2000 Soo Dong Kim

Final State (Transaction Rolled Back)


EJB Key Concept

50

2000 Soo Dong Kim

Your application may have performed are automatically undone by the underlying system.

Transactional Models
Nested Transactions
Allows you to embed atomic units of work within other units of work.
The unit of work is nested within another unit of work can roll back without forcing the entire transaction to roll back.

If the embedded unit can be made to succeed


the larger unit can succeed.
2000 Soo Dong Kim

If the embedded unit of work cannot be made to work


ultimately force the entire unit to fail.
EJB Key Concept

51

Transactional Models
The Nested Transaction Process State Final
(Transaction Succeeded) If all goes well, commit transaction

Begin transaction Initial State (No Transaction Occurring)

Transaction Occurring
If problem occurs, Abort transaction Perform one or more smaller-grained transactions Final State (Transaction Rolled Back)
EJB Key Concept

52

2000 Soo Dong Kim

EJB Transaction Attribute


Bean Managed Transaction
TX_BEAN_MANAGED

Container Managed Transaction


TX_NOT_SUPPORTED TX_REQUIRED TX_REQUIRES_NEW TX_SUPPORTS TX_MANDATORY TX_NEVER

EJB Key Concept

53

Container Managed Transaction


TX_REQUIRED
Client(bean or app) TX_REQUIRED

bean

Client(bean or app)

TX_REQUIRED

bean
2000 Soo Dong Kim

Thread of execution Beans transaction context

Clients transactional context No transactional context


EJB Key Concept

54

2000 Soo Dong Kim

Container Managed Transaction


TX_REQUIRS_NEW
Client(bean or app) TX_REQUIRES_NEW

bean

Client(bean or app)

TX_REQUIRES_NEW

bean
2000 Soo Dong Kim

Thread of execution Beans transaction context

Clients transactional context No transactional context


EJB Key Concept

55

Transactional Isolation
Four transaction Isolation levels in EJB
TRANSACTION_READ_UNCOMMITTED Not offer any isolation guarantees. But offers the highest performance. TRANSACTION_READ_COMMITTED Solves the dirty read problem. TRANSACTION_REPEATABLE_READ Solves the previous problems as well as the unrepeatable read problem. TRANSACTION_SERIALIZABLE Solves the previous problems as well as the phantom problem.
EJB Key Concept

56

2000 Soo Dong Kim

Two-Phase Commit
The Two phase commit protocol
The two-phase commit protocol is useful
Allows for many transaction managers and resource managers to participate in a transaction across a deployment. If any participant votes that the transaction should abort, all participants must roll back.

EJB Key Concept

57

Two-Phase Commit
Transaction Participants
Transaction Manager
1:prepare to commit 2:return

Transaction Coordinator

4:commit 5:return

Transaction Manager

Resource manager

Transaction Manager
3:log result The commit operation is logged in case of a crash.

Resource manager
2000 Soo Dong Kim EJB Key Concept

58

2000 Soo Dong Kim

- End -

EJB Key Concept

59

2000 Soo Dong Kim

Você também pode gostar