CIS 386 Course Advanced Enterprise Java Programming Enterprise JavaBeans: BMP and CMP Entity Beans René Doursat Guest Lecturer Golden Gate University, San Francisco February 2003
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 2
Algorithmic Computation: No External Data (a, b, c) Client Calculator Session Bean log(a + b)/c 2 one-shot request stateless Client E2-E4 F7-F5... Chess Player Session Bean checkmate conversation stateful (= chessboard) 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 3
Business Computation: Manages External Data John Doe? Client 411 Session Bean Phone Book 555 1212 one-shot request stateless login Client ok add item... Shopping Session Bean Inventory, Customers, Shopping Carts checkout conversation stateful (= session ID) 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 4
Simple Session: Direct Data Access Possible Client John Doe? 411 Session Bean SELECT phone WHERE name= John Doe Phone Book 555 1212 one-shot request stateless 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 5
Complex Session: How To Access Data? login?? Client ok add item... Shopping Session Bean Inventory, Customers, Shopping Carts checkout 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 6
Complex Session: Direct Data Access Not Advised Add-Item Manage- Account Inventory Log-In Check-Out CODE DUPLICATION! Customers Shopping Carts Client Shopping Cancel- Purchase 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 7
Complex Session: The Case For An Object Layer Add-Item Manage- Account Inventory Product Log-In Customers Check-Out Customer Account Shopping Carts Client Shopping Shopping Cart Cancel- Purchase 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 8
Logic vs. Data: Toward Encapsulation procedural architecture object-oriented architecture LOGIC DATA 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 9
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 10
Logic vs. Data: Examples Session Beans Entity Beans Bank teller Credit card authorizer DNA sequencer Order entry system Catalog engine Auction broker Order approval router Bank account Credit card DNA strand Order, Line item Product Bid, Item Purchase order (after Mastering EJBs, Ed Roman, p231) 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 11
Logic vs. Data: Thick vs. Thin Bank teller session bean Bank account entity bean complex bank operations simple conversational state (customer ID) elementary account ops (deposit, withdraw) extensive account data 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 12
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 13
Feature Comparison Session Beans Entity Beans 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 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) 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 14
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 widespread product 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 15
SessionBean Bean Class Methods EntityBean setsessioncontext(ctx) setentitycontext(ctx) ejbcreate... (... ) ejbfind... (... ) ejbactivate() ejbhome... (... ) ejbpassivate() ejbcreate(... ) ejbremove() ejbpostcreate(... ) ejbactivate() SessionBean & EntityBean businessmethod(... ) get... (... ) set... (... ) ejbpassivate() ejbremove() unsetentitycontext() 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 16
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 17
Without Object Pooling: Multiply And Waste Server Client #1 new Object() if objects are lightweight : OK (as long as they are freed or garbage collected) Client #2... but if they are expensive resources (connections, threads, beans, etc.): BAD! Client #3 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 18
Object Pooling: Conserve And Reuse Server Client #1 Pool Client #2 Pool.getObject() Use a dispatch center (the pool) to assign taxicabs to customers. Client #3 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 19
Object Pooling: Cycle 1 2 3 4 2 3 4 1 3 4 1 2 3 4 5 1 4 5 1 4 5 2 3 2 3 2 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 20
Object Pooling: Cycle 5 4 5 4 4 3 2 3 2 3 2 5 3 2 2 5 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 21
Entity Bean s Lifecyle: The Easter Eggs Lab Does Not Exist 1. newinstance() 2. setentitycontext() 1. unsetentitycontext() 2. Garbage Collect ejbhome() Pooled ejbfind() ejbselect() ejbcreate() 1. ejbactivate() 2. ejbpassivate() ejbremove() 2. Ready 1. businessmethod() ejbselect() 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 22
Entity Beans: BMP & CMP BMP Entity Beans CMP Entity Beans 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 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 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 23
Entity Bean s Business Logic: BMP & CMP class MyBean abstract container-generated class MyBean class MyBeanSubclass extends MyBean private instance fields int xxx; private instance fields getxxx() return xxx; abstract getxxx() setxxx( arg ) xxx = arg; abstract setxxx( arg ) businessmethod(... )...... businessmethod(... ) 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 24
Entity Bean s Container Methods: BMP & CMP setentitycontext(ctx) ctx = ctx ctx = ctx setentitycontext(ctx) ejbfind... (... ) SELECT EJB-QL ejbfind... (... ) ejbhome... (... ) SELECT SELECT or ejbselect() ejbhome... (... ) ejbcreate(... ) INSERT set... () ejbcreate(... ) ejbpostcreate(... )...... ejbpostcreate(... ) ejbactivate()...... ejbactivate() SELECT post-select UPDATE pre-update ejbpassivate()...... ejbpassivate() ejbremove() DELETE pre-delete ejbremove() unsetentitycontext() ctx = null ctx = null unsetentitycontext() JDBC JDBC 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 25
Entity Bean s Container Methods: CMP ctx = ctx setentitycontext(ctx) ejbselect... () abstract + EJB-QL EJB-QL SELECT or ejbselect() set... () ejbfind... (... ) ejbhome... (... ) ejbcreate(... )... ejbpostcreate(... )... ejbactivate() post-select pre-update... ejbpassivate() pre-delete ejbremove() ctx = null unsetentitycontext() 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 26 JDBC
Exception Handling Application Exceptions System Exceptions Declare in signature CHECKED MyBusinessException... CreateException RemoveException FinderException ObjectNotFoundExc. NamingException SQLException... Wrap in EJBException! Wrap some in BusinessExc.! RUNTIME NullPointerException IndexOutOfBoundsExc. IllegalArgumentExc.... EJBException NoSuchEntityException Just let go... The caller s fault or the internal business logic s fault! The system s infrastructure s fault! 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 27
Entity Bean s Container Methods: BMP setentitycontext(ctx) public void setentitycontext(entitycontext ctx) ejbfind... (... ) ejbhome... (... ) ejbcreate(... ) ejbpostcreate(... ) ejbactivate() { this.ctx = ctx; ejbpassivate() public void unsetentitycontext() { this.ctx = null; ejbremove() unsetentitycontext() JDBC 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 28
Entity Bean s Container Methods: CMP SAME setentitycontext(ctx) public void setentitycontext(entitycontext ctx) ejbfind... (... ) ejbhome... (... ) ejbcreate(... ) ejbpostcreate(... ) ejbactivate() { this.ctx = ctx; ejbpassivate() public void unsetentitycontext() { this.ctx = null; ejbremove() unsetentitycontext() JDBC 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 29
Entity Bean s Container Methods: BMP setentitycontext(ctx) ejbfind... (... ) [1] ejbhome... (... ) ejbcreate(... ) public Key ejbfind... ( args ) throws FinderException { try {...get DB connection...prepare SELECT statement...set args in statement ejbpostcreate(... ) ejbactivate() ejbpassivate() ejbremove() unsetentitycontext()...execute statement query if (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 30
Entity Bean s Container Methods: BMP setentitycontext(ctx) ejbfind... (... ) [N] ejbhome... (... ) ejbcreate(... ) public Collection ejbfind... ( args ) throws FinderException { try {...get DB connection...prepare SELECT statement...set args in statement ejbpostcreate(... ) ejbactivate() ejbpassivate() ejbremove() unsetentitycontext()...execute statement query while (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 31
IMPLEMENT IN EJB-QL Entity Bean s Container Methods: CMP setentitycontext(ctx) ejbfind... (... ) ejbhome... (... ) ejbcreate(... ) ejbpostcreate(... ) ejbactivate() ejbpassivate() 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> ejbremove() unsetentitycontext() JDBC 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 32
Entity Bean s Container Methods: CMP ABSTRACT METHOD AND EJB-QL setentitycontext(ctx) ejbfind... (... ) ejbselect... () ejbhome... (... ) ejbcreate(... ) ejbpostcreate(... ) ejbactivate() ejbpassivate() 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> ejbremove() unsetentitycontext() JDBC 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 33
Entity Bean s Container Methods: BMP setentitycontext(ctx) ejbfind... (... ) ejbhome... (... ) ejbcreate(... ) public Value ejbhome... ( args ) throws BusinessException { try {...get DB connection...prepare SELECT statement...set args in statement ejbpostcreate(... ) ejbactivate() ejbpassivate() ejbremove() unsetentitycontext()...execute statement query while (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 34
SAME OR USE ejbselect() Entity Bean s Container Methods: CMP setentitycontext(ctx) ejbfind... (... ) ejbhome... (... ) ejbcreate(... ) public Value ejbhome... ( args ) throws BusinessException { try {...get DB connection...prepare SELECT statement...set args in statement Collection c = ejbselect() ejbpostcreate(... ) ejbactivate() ejbpassivate() ejbremove() unsetentitycontext()...execute statement query while (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: BMP setentitycontext(ctx) public Key ejbcreate( args ) throws CreateException { ejbfind... (... )...initialize fields with args try {...get DB connection ejbhome... (... ) ejbcreate(... )...prepare INSERT statement...set key & fields in statement ejbpostcreate(... ) ejbactivate() ejbpassivate() ejbremove() unsetentitycontext()...execute statement update if (zero count) {...throw CreateException...return key catch (SQLException, NamingException) {...throw EJBException finally {...close statement...close connection JDBC 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 36
NO JDBC: set... () ONLY Entity Bean s Container Methods: CMP setentitycontext(ctx) ejbfind... (... ) public Key ejbcreate( args ) throws CreateException {...initialize fields with args, using setxxx() ejbhome... (... ) ejbcreate(... ) ejbpostcreate(... ) ejbactivate()...return key ejbpassivate() ejbremove() unsetentitycontext() JDBC 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 37
Entity Bean s Container Methods: BMP setentitycontext(ctx) public void ejbpostcreate( args ) ejbfind... (... ) ejbhome... (... ) ejbcreate(... ) ejbpostcreate(... ) ejbactivate() ejbpassivate() ejbremove() unsetentitycontext() {...use EJBObject, if needed JDBC 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 38
Entity Bean s Container Methods: CMP SAME setentitycontext(ctx) public void ejbpostcreate( args ) ejbfind... (... ) ejbhome... (... ) ejbcreate(... ) ejbpostcreate(... ) ejbactivate() ejbpassivate() ejbremove() unsetentitycontext() {...use EJBObject, if needed JDBC 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 39
Entity Bean s Container Methods: BMP setentitycontext(ctx) ejbfind... (... ) ejbhome... (... ) ejbcreate(... ) ejbpostcreate(... ) ejbactivate() public void ejbremove() throws RemoveException { try {...get DB connection...prepare DELETE statement...set key in statement...execute statement update if (zero count) {...throw RemoveException ejbpassivate() ejbremove() unsetentitycontext() catch (SQLException, NamingException) {...throw EJBException finally {...close statement...close connection JDBC 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 40
Entity Bean s Container Methods: CMP NO JDBC: pre-delete ONLY setentitycontext(ctx) ejbfind... (... ) public void ejbremove() throws RemoveException {...possible pre-delete processing ejbhome... (... ) ejbcreate(... ) ejbpostcreate(... ) ejbactivate() ejbpassivate() ejbremove() unsetentitycontext() JDBC 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 41
Entity Bean s Container Methods: BMP setentitycontext(ctx) public void ejbactivate() ejbfind... (... ) ejbhome... (... ) ejbcreate(... ) ejbpostcreate(... ) ejbactivate() {...acquire resources, if needed ejbpassivate() public void ejbpassivate() {...release held resources, if any ejbremove() unsetentitycontext() JDBC 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 42
Entity Bean s Container Methods: CMP SAME setentitycontext(ctx) public void ejbactivate() ejbfind... (... ) ejbhome... (... ) ejbcreate(... ) ejbpostcreate(... ) ejbactivate() {...acquire resources, if needed ejbpassivate() public void ejbpassivate() {...release held resources, if any ejbremove() unsetentitycontext() JDBC 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 43
Entity Bean s Container Methods: BMP setentitycontext(ctx) public void ejbfind... (... ) ejbhome... (... ) ejbcreate(... ) ejbpostcreate(... ) ejbactivate() ejbpassivate() ejbremove() unsetentitycontext() { try {...get DB connection...prepare SELECT statement...set key in statement...execute statement query if (empty result) {...throw NoSuchEntityException...get fields from result catch (SQLException, NamingException) {...throw EJBException finally {...close statement...close connection JDBC 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 44
Entity Bean s Container Methods: CMP NO JDBC: post-select ONLY setentitycontext(ctx) ejbfind... (... ) public void { ejbhome... (... ) ejbcreate(... ) ejbpostcreate(... ) ejbactivate() ejbpassivate() ejbremove() unsetentitycontext()...possible post-select processing JDBC 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 45
Entity Bean s Container Methods: BMP setentitycontext(ctx) public void ejbfind... (... ) ejbhome... (... ) ejbcreate(... ) ejbpostcreate(... ) ejbactivate() ejbpassivate() ejbremove() unsetentitycontext() { try {...get DB connection...prepare UPDATE statement...set key & fields in statement...execute statement update if (zero count) {...throw EJBException catch (SQLException, NamingException) {...throw EJBException finally {...close statement...close connection JDBC 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 46
Entity Bean s Container Methods: CMP NO JDBC: pre-update ONLY setentitycontext(ctx) public void ejbfind... (... ) ejbhome... (... ) ejbcreate(... ) ejbpostcreate(... ) ejbactivate() ejbpassivate() ejbremove() unsetentitycontext() {...possible pre-update processing JDBC 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 47
EJBs Are Single-Threaded Container Strategy 1: serialize access to a single instance Container Strategy 2: allow parallel access to multiple instances 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 48
Entity Beans Are Single-Threaded Container Strategy 1: serialize access to a single instance Container Strategy 2: allow parallel access to multiple instances and keep data synchronized in DB 2/2003 CIS 386 - Enterprise JavaBeans: BMP and CMP Entity Beans 49