Enterprise JavaBeans: BMP and CMP Entity Beans

Similar documents
Enterprise JavaBeans. Layer:07. Entity

UNIT-III EJB APPLICATIONS

Enterprise JavaBeans. Layer:08. Persistence

Lab2: CMP Entity Bean working with Session Bean

SCBCD EXAM STUDY KIT. Paul Sanghera CX JAVA BUSINESS COMPONENT DEVELOPER CERTIFICATION FOR EJB MANNING. Covers all you need to pass

<<Interface>> EntityBean (from ejb) EJBHome. <<Interface>> CountHome. (from entity) create() findbyprimarykey() <<Interface>> EJBObject.

J2EE Application Server. EJB Overview. Java versus.net for the Enterprise. Component-Based Software Engineering. ECE493-Topic 5 Winter 2007

Enterprise JavaBeans. Layer:03. Session

Conception of Information Systems Lecture 8: J2EE and EJBs

EJB ENTERPRISE JAVA BEANS INTRODUCTION TO ENTERPRISE JAVA BEANS, JAVA'S SERVER SIDE COMPONENT TECHNOLOGY. EJB Enterprise Java

Life Cycle of an Entity Bean

Stateless Session Bean

Understanding and Designing with EJB

8. Component Software

JBuilder. Getting Started Guide part II. Preface. Creating your Second Enterprise JavaBean. Container Managed Persistent Bean.

Objectives. Software Development using MacroMedia s JRun. What are EJBs? Topics for Discussion. Examples of Session beans calling entity beans

Entity Beans 02/24/2004

Topexam. 一番権威的な IT 認定試験ウェブサイト 最も新たな国際 IT 認定試験問題集

Copyright UTS Faculty of Information Technology 2002 EJB2 EJB-3. Use Design Patterns 1 to re-use well known programming techniques.

Transaction Commit Options

Introduction to Session beans EJB 3.0

Lab3: A J2EE Application with Stateful Session Bean and CMP Entity Beans

Chapter 6 Enterprise Java Beans

The Developer s Guide to Understanding Enterprise JavaBeans. Nova Laboratories

Enterprise Java Beans

these methods, remote clients can access the inventory services provided by the application.

ENTERPRISE beans are the J2EE components that implement Enterprise Java-

Q: I just remembered that I read somewhere that enterprise beans don t support inheritance! What s that about?

8. Component Software

TOPLink for WebLogic. Whitepaper. The Challenge: The Solution:

Distributed Applications (RMI/JDBC) Copyright UTS Faculty of Information Technology 2002 EJB EJB-3

Java Enterprise Edition

NetBeans IDE Field Guide

Enterprise JavaBeans. Session EJBs

The Details of Writing Enterprise Java Beans

Oracle Developer Day Agenda

Enterprise Java and Rational Rose -- Part I

Plan. Department of Informatics. Advanced Software Engineering Prof. J. Pasquier-Rocha Cours de Master en Informatique - SH 2003/04

J2EE Development. Course Detail: Audience. Duration. Course Abstract. Course Objectives. Course Topics. Class Format.

Multi-tier architecture performance analysis. Papers covered

Implementing a Web Service p. 110 Implementing a Web Service Client p. 114 Summary p. 117 Introduction to Entity Beans p. 119 Persistence Concepts p.

IBM WebSphere Application Server. J2EE Programming Model Best Practices

J2EE Web Development 13/1/ Application Servers. Application Servers. Agenda. In the beginning, there was darkness and cold.

Oracle9iAS Containers for J2EE

This chapter describes: AssetBean AssetMgr AssetMgrEJB AssetMgrHome AssetTagAssociationBean AssetTypeBean AssociationBean AssociationMgr

Sharpen your pencil. Container services. EJB Object. the bean

Vision of J2EE. Why J2EE? Need for. J2EE Suite. J2EE Based Distributed Application Architecture Overview. Umair Javed 1

jar command Java Archive inherits from tar : Tape Archive commands: jar cvf filename jar tvf filename jar xvf filename java jar filename.

Oracle Containers for J2EE

Overview p. 1 Server-side Component Architectures p. 3 The Need for a Server-Side Component Architecture p. 4 Server-Side Component Architecture

Exam Questions 1Z0-895

J2EE Access of Relational Data

It Is a Difficult Question! The Goal of This Study. Specification. The Goal of This Study. History. Existing Benchmarks

Enterprise JavaBeans EJB component types

Example Purchase request JMS & MDB. Example Purchase request. Agenda. Purpose. Solution. Enterprise Application Development using J2EE

Oracle8i. Enterprise JavaBeans Developer s Guide and Reference. Release 3 (8.1.7) July 2000 Part No. A

Teamcenter Global Services Customization Guide. Publication Number PLM00091 J

Java- EE Web Application Development with Enterprise JavaBeans and Web Services

Enterprise JavaBeans (I) K.P. Chow University of Hong Kong

New Features in EJB 3.1

Java Training For Six Weeks

1Z Oracle. Java Enterprise Edition 5 Enterprise Architect Certified Master

JAVA. Aspects (AOP) AspectJ

Oracle EXAM - 1Z Java EE 6 Enterprise JavaBeans Developer Certified Expert Exam. Buy Full Product.

Container Managed Persistent in EJB 2.0

Enterprise JavaBeans 2.1

Lab1: Stateless Session Bean for Registration Fee Calculation

Oracle9iAS TopLink. 1 TopLink CMP for BEA WebLogic Server. 1.1 EJB 2.0 Support. CMP-Specific Release Notes

BEA WebLogic Server. Enterprise JavaBeans

Java/J2EE Interview Questions(255 Questions)

Introduction to EJB-CMP/CMR, Part 4

Enterprise Java Security Fundamentals

SUN Sun Certified Enterprise Architect for J2EE 5. Download Full Version :

Application Servers G Session 11 - Sub-Topic 2 Using Enterprise JavaBeans. Dr. Jean-Claude Franchitti

IBM. Application Development with IBM Rational Application Developer for Web Sphere

Enterprise Java Beans for the Oracle Internet Platform

Java Smart Ticket Demo Application Scrutinized

Oracle 10g: Build J2EE Applications

WAS V7 Application Development

Master Thesis An Introduction to the Enterprise JavaBeans technology and Integrated Development Environments for implementing EJB applications

Exam Actual. Higher Quality. Better Service! QUESTION & ANSWER

Borland Application Server Certification. Study Guide. Version 1.0 Copyright 2001 Borland Software Corporation. All Rights Reserved.

Efficient Object-Relational Mapping for JAVA and J2EE Applications or the impact of J2EE on RDB. Marc Stampfli Oracle Software (Switzerland) Ltd.

Enterprise JavaBeans TM

Course Content for Java J2EE

Using Message Driven Beans.

ITdumpsFree. Get free valid exam dumps and pass your exam test with confidence

The 1st Java professional open source Convention Israel 2006

presentation DAD Distributed Applications Development Cristian Toma

JAVA COURSES. Empowering Innovation. DN InfoTech Pvt. Ltd. H-151, Sector 63, Noida, UP

BEA WebLogic Server. Programming WebLogic Enterprise JavaBeans

EJB 2.1 CMP EntityBeans (CMP2)

CO Java EE 7: Back-End Server Application Development

@jbossdeveloper. explained

Chapter 6 Object Persistence, Relationships and Queries

Software Components and Distributed Systems

8. Component Software

~ Ian Hunneybell: CBSD Revision Notes (07/06/2006) ~

Java EE 6: Develop Business Components with JMS & EJBs

Deccansoft Software Services. J2EE Syllabus

Transcription:

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