Advanced Enterprise Java Programmingdoursat.free.fr/docs/teaching/CIS386_S03_Entity_Beans.pdf ·...

Preview:

Citation preview

CIS 386 CourseAdvanced Enterprise Java Programming

Enterprise JavaBeans:BMP and CMP Entity Beans

René Doursat

Guest LecturerGolden Gate University, San Francisco

February 2003

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 2

EJB Trail

• Session Beans– Stateless– Stateful

• Entity Beans– Bean-Managed Persistence (BMP)– Container-Managed Persistence (CMP)

• Message-Driven Beans

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 3

Algorithmic Computation: No External Data

Chess PlayerSession Bean

CalculatorSession Bean

Client

Client

(a, b, c)

log(a + b)/c2

E2-E4

F7-F5

checkmate

. . .

• conversation• stateful (= chessboard)

• one-shot request• stateless

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 4

Business Computation: Manages External Data

Phone Book

Inventory,Customers,

Shopping Carts

ShoppingSession Bean

411Session Bean

Client

Client

login

ok

checkout

. . .

add item

John Doe?

555 1212• one-shot request• stateless

• conversation• stateful (= session ID)

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 5

Simple Session: Direct Data Access Possible

Phone Book411

Session BeanClient

John Doe?

555 1212

SELECT phone WHERE name=‘John Doe’

• one-shot request• stateless

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 6

Complex Session: How To Access Data?

Inventory,Customers,

Shopping Carts

ShoppingSession BeanClient

login

ok

checkout

. . .

add item

??

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 7

Complex Session: Direct Data Access Not Advised

Client

Add-Item

Log-In

Check-Out

Manage-Account

Cancel-Purchase

Shopping

Inventory

Customers

Shopping Carts

CODEDUPLICATION!

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 8

Complex Session: The Case For An Object Layer

ShoppingClient

Add-Item

Log-In

Check-Out

Manage-Account

Cancel-Purchase

ShoppingCart

Product

CustomerAccount

Inventory

Customers

Shopping Carts

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 9

Logic vs. Data: Toward Encapsulation

procedural architecture object-oriented architecture

LOGIC DATA

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 10

Logic vs. Data: Reintroducing Separation (at a higher level)

EJB architecture

session beans entity beans

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 11

Logic vs. Data: Examples

Session Beans Entity Beans

Bank teller Bank account

Credit card authorizer Credit card

DNA sequencer DNA strand

Order entry system Order, Line item

Catalog engine Product

Auction broker Bid, Item

Order approval router Purchase order

(after Mastering EJBs, Ed Roman, p231)

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 12

Logic vs. Data: Thick vs. Thin

Bank tellersession bean

Bank accountentity bean

complex bankoperations

elementary account ops(deposit, withdraw)

extensiveaccount data

simple conversationalstate (customer ID)

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 13

Definitions

Given that Enterprise Beans are server-side components deployable in a distributed multi-tier environment:

• Session Beans model the business’s processes: they perform work for clients calling them and are short-lived.

• Entity Beans model the business’s fundamental underlying data: they are persistent objects stored in permanent storage.

• Message-Driven Beans are a type of asynchronous Session Beans.

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 14

Feature Comparison

Session Beans Entity Beans

• allow shared access from multiple users

• is associated to a primary key (defining data uniqueness)

• provide an object view of data in the database

• can be long-lived

• survive container crash (automatic state reset)

• execute on behalf of a single client

• can be transaction-aware

• do not represent directly shared data in the database (but may access and update such data)

• relatively short-lived

• removed when container crashes

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 15

Concepts of Persistence

• Serialization– bit-blobs written to storage media– not practical for selective search

• Object-to-Relational Mapping– data stored to and loaded from RDBMS– generally: 1 object class = 1 table, 1 object instance = 1 row, but

not always– mapping scheme can be complex

• ODBMS– ideal for transparent object persistence, however not a wide-

spread product

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 16

Bean Class MethodsSessionBean EntityBean

setSessionContext(ctx) setEntityContext(ctx)

ejbCreate . . . ( . . . ) ejbFind . . . ( . . . )

ejbActivate() ejbHome . . . ( . . . )

ejbPassivate() ejbCreate( . . . )

ejbRemove() ejbPostCreate( . . . )

ejbActivate()

ejbLoad()

SessionBean & EntityBean ejbStore()

businessMethod( . . . ) ejbPassivate()

get . . . ( . . . ) ejbRemove()

set . . . ( . . . ) unsetEntityContext()

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 17

EJB Trail

• Session Beans– Stateless– Stateful

• Entity Beans– Bean-Managed Persistence (BMP)– Container-Managed Persistence (CMP)

• Message-Driven Beans

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 18

Without Object Pooling: Multiply And Waste

Server

Client #3

Client #1

Client #2

new Object()

if objects are “lightweight”: OK(as long as they are freed orgarbage collected)

. . . but if they are expensiveresources (connections,threads, beans, etc.): BAD!

“You don’t want to build a new taxicab and hire a new driver for each incoming customer call.”

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 19

Object Pooling: Conserve And Reuse

Server

Client #3

Client #1

Client #2

Pool

Pool.getObject() “Use a dispatch center (the pool) to assign taxicabs to customers.”

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 20

Object Pooling: Cycle

1234

234

134

2

1

345

2

145

2

1

3

45

23

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 21

Object Pooling: Cycle

5

2

4

3

5

2

4

3

2

4

3

5

23

5

2

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 22

Entity Bean’s Lifecyle: The “Easter Eggs” Lab

Does Not Exist

Pooled

Ready

1. newInstance()2. setEntityContext()

1. unsetEntityContext()2. Garbage Collect

ejbHome() ejbFind()ejbSelect()

1’. ejbStore()2. ejbLoad()

businessMethod()ejbSelect()

2’. ejbPassivate()1. ejbActivate() ejbRemove()ejbCreate()

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 23

Entity Beans: BMP & CMP

BMP Entity Beans CMP Entity Beans

• persistence logic is declared in the deployment descriptors

• smaller Java code for bean class– no persistent fields, abstract get/set– almost no JDBC

• bigger XML deployment descriptors– persistent fields– EJB-QL search queries– container-specific DB mapping

• persistence logic is programmed in the bean class

• big Java code for bean class– persistent fields & get/set methods – edit & search queries with JDBC

• small XML deployment descriptors

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 24

Entity Bean’s Business Logic: BMP

. . .

return xxx;

xxx = arg;

businessMethod( . . . )

setXxx( arg )

getXxx()

private instance fields

class MyBean

int xxx;

& CMP

. . .

abstract

abstract

businessMethod( . . . )

setXxx( arg )

getXxx()

private instance fields

class MyBeanabstract

class MyBeanSubclassextends MyBean

container-generated →

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 25

Entity Bean’s Container Methods:

ejbFind . . . ( . . . )

ejbHome . . . ( . . . )

ejbCreate( . . . )

ejbPostCreate( . . . )

ejbActivate()

ejbLoad()

ejbStore()

ejbPassivate()

ejbRemove()

setEntityContext(ctx)

unsetEntityContext()

& CMP

SELECTor ejbSelect()

ctx = ctx

EJB-QL

set . . . ()

. . .

. . .

. . .

post-SELECT

pre-UPDATE

pre-DELETE

ctx = null

JDBC

ejbFind . . . ( . . . )

ejbHome . . . ( . . . )

ejbCreate( . . . )

ejbPostCreate( . . . )

ejbActivate()

ejbLoad()

ejbStore()

ejbPassivate()

ejbRemove()

setEntityContext(ctx)

unsetEntityContext()JDBC

SELECT

SELECT

INSERT

SELECT

UPDATE

DELETE

ctx = ctx

ctx = null

. . .

. . .

. . .

BMP

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 26

Entity Bean’s Container Methods: CMP

ejbFind . . . ( . . . )

ejbHome . . . ( . . . )

ejbCreate( . . . )

ejbPostCreate( . . . )

ejbActivate()

ejbLoad()

ejbStore()

ejbPassivate()

ejbRemove()

setEntityContext(ctx)

unsetEntityContext()

SELECTor ejbSelect()

ctx = ctx

EJB-QL

set . . . ()

. . .

. . .

. . .

post-SELECT

pre-UPDATE

pre-DELETE

ctx = null

ejbSelect . . . ()

abstract+ EJB-QL

JDBC

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 27

Exception Handling

• MyBusinessException• . . .• CreateException• RemoveException• FinderException• ObjectNotFoundExc.

Application Exceptions System ExceptionsC

HEC

KED

RU

NTI

ME • NullPointerException• IndexOutOfBoundsExc.• IllegalArgumentExc.• . . .

• NamingException• SQLException• . . .

The caller’s fault or theinternal business logic’s fault!

The system’sinfrastructure’s fault!

• EJBException• NoSuchEntityException

Wrap in

EJBException!

Just let go...

Wrap some in

BusinessExc.!

Declare insignature

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 28

Entity Bean’s Container Methods: BMP

ejbFind . . . ( . . . )

ejbHome . . . ( . . . )

ejbCreate( . . . )

ejbPostCreate( . . . )

ejbActivate()

ejbLoad()

ejbStore()

ejbPassivate()

ejbRemove()

setEntityContext(ctx) public void setEntityContext(EntityContext ctx)

{this.ctx = ctx;

}

public void unsetEntityContext()

{this.ctx = null;

}

unsetEntityContext()JDBC

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 29

Entity Bean’s Container Methods: CMP

ejbFind . . . ( . . . )

ejbHome . . . ( . . . )

ejbCreate( . . . )

ejbPostCreate( . . . )

ejbActivate()

ejbLoad()

ejbStore()

ejbPassivate()

ejbRemove()

setEntityContext(ctx)

unsetEntityContext()

→ SAME

public void setEntityContext(EntityContext ctx)

{this.ctx = ctx;

}

public void unsetEntityContext()

{this.ctx = null;

}

JDBC

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 30

Entity Bean’s Container Methods: BMP

ejbHome . . . ( . . . )

ejbCreate( . . . )

ejbPostCreate( . . . )

ejbActivate()

ejbLoad()

ejbStore()

ejbPassivate()

ejbRemove()

setEntityContext(ctx)

unsetEntityContext()

ejbFind . . . ( . . . ) [1]

public Key ejbFind . . . ( args )throws FinderException

{

try {...get DB connection

...prepare SELECT statement

...set args in statement

...execute statement queryif (empty result) {

...throw FinderException}...return key from result

}catch (SQLException, NamingException) {

...throw EJBException}finally {

...close statement

...close connection}

}

JDBC

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 31

Entity Bean’s Container Methods: BMP

ejbHome . . . ( . . . )

ejbCreate( . . . )

ejbPostCreate( . . . )

ejbActivate()

ejbLoad()

ejbStore()

ejbPassivate()

ejbRemove()

setEntityContext(ctx)

unsetEntityContext()

ejbFind . . . ( . . . ) [N]

public Collection ejbFind . . . ( args )throws FinderException

{

try {...get DB connection

...prepare SELECT statement

...set args in statement

...execute statement querywhile (not empty result) {

...fill coll. with keys from result}...return coll.

}catch (SQLException, NamingException) {

...throw EJBException}finally {

...close statement

...close connection}

}

JDBC

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 32

Entity Bean’s Container Methods: CMP

ejbHome . . . ( . . . )

ejbCreate( . . . )

ejbPostCreate( . . . )

ejbActivate()

ejbLoad()

ejbStore()

ejbPassivate()

ejbRemove()

setEntityContext(ctx)

unsetEntityContext()

ejbFind . . . ( . . . )

public Key/Collection ejbFind . . . ( args )throws FinderException

ejb-jar.xml

<entity>. . .<persistence-type>Container</persistence-type>. . .<query>

<query-method><method-name>find . . . </method-name><method-params>args</method-params>

</query-method><ejb-ql>

SELECT OBJECT(q) FROM . . .</ejb-ql>

</query>. . .

</entity>

→ IMPLEMENT IN EJB-QL

JDBC

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 33

Entity Bean’s Container Methods: CMP

ejbHome . . . ( . . . )

ejbCreate( . . . )

ejbPostCreate( . . . )

ejbActivate()

ejbLoad()

ejbStore()

ejbPassivate()

ejbRemove()

setEntityContext(ctx)

unsetEntityContext()

→ ABSTRACT METHOD AND EJB-QL

ejbFind . . . ( . . . )ejbSelect . . . ()

public abstract Collection ejbSelect . . . ()throws FinderException;

ejb-jar.xml

<entity>. . .<persistence-type>Container</persistence-type>. . .<query>

<query-method><method-name>ejbSelect . . . </method-name><method-params></method-params>

</query-method><ejb-ql>

SELECT q.xxx FROM . . .</ejb-ql>

</query>. . .

</entity>

JDBC

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 34

Entity Bean’s Container Methods: BMP

ejbCreate( . . . )

ejbPostCreate( . . . )

ejbActivate()

ejbLoad()

ejbStore()

ejbPassivate()

ejbRemove()

setEntityContext(ctx)

unsetEntityContext()

ejbFind . . . ( . . . )

ejbHome . . . ( . . . )

public Value ejbHome . . . ( args )throws BusinessException

{

try {...get DB connection

...prepare SELECT statement

...set args in statement

...execute statement querywhile (not empty result) {

...aggregate value from result}...return value

}catch (SQLException, NamingException) {

...throw EJBException}finally {

...close statement

...close connection}

}

JDBC

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 35

Entity Bean’s Container Methods: CMP

ejbCreate( . . . )

ejbPostCreate( . . . )

ejbActivate()

ejbLoad()

ejbStore()

ejbPassivate()

ejbRemove()

setEntityContext(ctx)

unsetEntityContext()

ejbFind . . . ( . . . )

ejbHome . . . ( . . . )

→ SAME OR USE ejbSelect()

public Value ejbHome . . . ( args )throws BusinessException

{

try {...get DB connection

...prepare SELECT statement

...set args in statement

...execute statement querywhile (not empty result) {

...aggregate value from result}...return value

}catch (SQLException, NamingException) {

...throw EJBException}finally {

...close statement

...close connection}

}

Collection c =ejbSelect()

JDBC

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 36

Entity Bean’s Container Methods: BMP

setEntityContext(ctx) public Key ejbCreate( args )throws CreateException

{...initialize fields with argstry {

...get DB connection

...prepare INSERT statement

...set key & fields in statement

...execute statement updateif (zero count) {

...throw CreateException}...return key

}catch (SQLException, NamingException) {

...throw EJBException}finally {

...close statement

...close connection}

}

ejbFind . . . ( . . . )

ejbHome . . . ( . . . )

ejbCreate( . . . )

ejbPostCreate( . . . )

ejbActivate()

ejbLoad()

ejbStore()

ejbPassivate()

ejbRemove()

unsetEntityContext()JDBC

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 37

Entity Bean’s Container Methods: CMP

ejbActivate()

ejbLoad()

ejbStore()

ejbPassivate()

setEntityContext(ctx)

unsetEntityContext()

ejbFind . . . ( . . . )

ejbHome . . . ( . . . )

ejbCreate( . . . )

ejbPostCreate( . . . )

ejbRemove()

JDBC

→ NO JDBC: set . . . () ONLY

public Key ejbCreate( args )throws CreateException

{...initialize fields with args, using setXxx()

...return key

}

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 38

Entity Bean’s Container Methods: BMP

setEntityContext(ctx) public void ejbPostCreate( args )

{...use EJBObject, if needed

}ejbFind . . . ( . . . )

ejbHome . . . ( . . . )

ejbCreate( . . . )

ejbPostCreate( . . . )

ejbActivate()

ejbLoad()

ejbStore()

ejbPassivate()

ejbRemove()

unsetEntityContext()JDBC

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 39

Entity Bean’s Container Methods: CMP

ejbActivate()

ejbLoad()

ejbStore()

ejbPassivate()

setEntityContext(ctx)

unsetEntityContext()

ejbFind . . . ( . . . )

ejbHome . . . ( . . . )

ejbCreate( . . . )

ejbPostCreate( . . . )

ejbRemove()

JDBC

→ SAME

public void ejbPostCreate( args )

{...use EJBObject, if needed

}

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 40

Entity Bean’s Container Methods: BMP

setEntityContext(ctx) public void ejbRemove()throws RemoveException

{

try {...get DB connection

...prepare DELETE statement

...set key in statement

...execute statement updateif (zero count) {

...throw RemoveException}

}catch (SQLException, NamingException) {

...throw EJBException}finally {

...close statement

...close connection}

}

ejbFind . . . ( . . . )

ejbHome . . . ( . . . )

ejbCreate( . . . )

ejbPostCreate( . . . )

ejbActivate()

ejbLoad()

ejbStore()

ejbPassivate()

ejbRemove()

unsetEntityContext()JDBC

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 41

Entity Bean’s Container Methods: CMP

ejbActivate()

ejbLoad()

ejbStore()

ejbPassivate()

setEntityContext(ctx)

unsetEntityContext()

ejbFind . . . ( . . . )

ejbHome . . . ( . . . )

ejbCreate( . . . )

ejbPostCreate( . . . )

ejbRemove()

JDBC

→ NO JDBC: pre-DELETE ONLY

public void ejbRemove()throws RemoveException

{...possible pre-DELETE processing

}

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 42

Entity Bean’s Container Methods: BMP

setEntityContext(ctx) public void ejbActivate()

{...acquire resources, if needed

}

public void ejbPassivate()

{...release held resources, if any

}

ejbFind . . . ( . . . )

ejbHome . . . ( . . . )

ejbCreate( . . . )

ejbPostCreate( . . . )

ejbActivate()

ejbLoad()

ejbStore()

ejbPassivate()

ejbRemove()

unsetEntityContext()JDBC

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 43

Entity Bean’s Container Methods: CMP

ejbLoad()

ejbStore()

ejbRemove()

setEntityContext(ctx)

unsetEntityContext()

ejbFind . . . ( . . . )

ejbHome . . . ( . . . )

ejbCreate( . . . )

ejbPostCreate( . . . )

ejbActivate()

ejbPassivate()

JDBC

→ SAME

public void ejbActivate()

{...acquire resources, if needed

}

public void ejbPassivate()

{...release held resources, if any

}

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 44

Entity Bean’s Container Methods: BMP

setEntityContext(ctx) public void ejbLoad()

{

try {...get DB connection

...prepare SELECT statement

...set key in statement

...execute statement queryif (empty result) {

...throw NoSuchEntityException}...get fields from result

}catch (SQLException, NamingException) {

...throw EJBException}finally {

...close statement

...close connection}

}

ejbFind . . . ( . . . )

ejbHome . . . ( . . . )

ejbCreate( . . . )

ejbPostCreate( . . . )

ejbActivate()

ejbLoad()

ejbStore()

ejbPassivate()

ejbRemove()

unsetEntityContext()JDBC

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 45

Entity Bean’s Container Methods: CMP

ejbRemove()

setEntityContext(ctx)

unsetEntityContext()

ejbFind . . . ( . . . )

ejbHome . . . ( . . . )

ejbCreate( . . . )

ejbPostCreate( . . . )

ejbActivate()

ejbPassivate()

ejbLoad()

ejbStore()

JDBC

→ NO JDBC: post-SELECT ONLY

public void ejbLoad()

{

...possible post-SELECT processing}

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 46

Entity Bean’s Container Methods: BMP

setEntityContext(ctx) public void ejbStore()

{

try {...get DB connection

...prepare UPDATE statement

...set key & fields in statement

...execute statement updateif (zero count) {

...throw EJBException}

}catch (SQLException, NamingException) {

...throw EJBException}finally {

...close statement

...close connection}

}

ejbFind . . . ( . . . )

ejbHome . . . ( . . . )

ejbCreate( . . . )

ejbPostCreate( . . . )

ejbActivate()

ejbLoad()

ejbStore()

ejbPassivate()

ejbRemove()

unsetEntityContext()JDBC

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 47

Entity Bean’s Container Methods: CMP

ejbRemove()

setEntityContext(ctx)

unsetEntityContext()

ejbFind . . . ( . . . )

ejbHome . . . ( . . . )

ejbCreate( . . . )

ejbPostCreate( . . . )

ejbActivate()

ejbPassivate()

ejbLoad()

ejbStore()

JDBC

→ NO JDBC: pre-UPDATE ONLY

public void ejbStore()

{...possible pre-UPDATE processing

}

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 48

EJBs Are Single-Threaded

Container Strategy 1:serialize accessto a single instance

Container Strategy 2:allow parallel accessto multiple instances

2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 49

Entity Beans Are Single-Threaded

Container Strategy 2:allow parallel accessto multiple instances ejbLoad()

ejbStore()

and keep datasynchronized in DB

Container Strategy 1:serialize accessto a single instance

Recommended