Top Banner
Middleware Technology (J2EE/EJB) Entity Bean
34

Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

Dec 28, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

Middleware Technology (J2EE/EJB)Entity Bean

Page 2: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

2

Introduction to Entity Beans

• Persistence Concepts

Entity beans are persistent objects that can be stored in permanent storage.

3 popular ways to persists objects

Java Object Serialization

object graph byte streamMarshalling

Unmarshalling

We can push the stream over network or save the stream to storage, such as a file system, database, or JNDI tree.

Page 3: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

3

Persistence Concepts

Problem:The stream is not relevant to content. It’s expensive and cumbersome to query objects stored using object serial

ization.Example: bank account

Object-Relational MappingWhen saving Java objects, we use JDBC to map the object data into a relational database.

Java class Database table definition

instance of that class a row in that table

fields in that instance cells in that row

Page 4: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

4

Persistence Concepts

When we want to load our objects from the database, we could instantiate an object from that class, read the data in from the database, and then populate that object instance’s fields with the relational data read in.

Mapping of objects to relational data can be done in two ways. hand-craft

database access API: JDBC or SQL/J automation

mapping product: WebGain’s TOPLink, SUN’s JavaBlend

Page 5: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

5

An example of object-relational mapping (Bank Account)

Page 6: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

6

Persistence Concepts

Object DatabasesJava objects are stored as whole objects in the ODBMS (Object DataBase Management System). There is no O/R mapping layer.We program to the object database’s API, not the relational database API.

OQL (Object Query Language) rather than SQL (Structured Query Language, SELECT, DELETE, INSERT, UPDATE)

Certain applications, for instance, geospatial, CAD/CAM, are complete unfit for relational databases, which go really well with object databases.

Vendors: ObjectStore, Versant, POET

Page 7: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

7

Entity Bean

• Two different kinds of components deployed Application Logic Component

Session Bean

method provider, action(verb), handle business processes

Persistent Data Component

Entity Bean

data, handle business data, using some persistence mechanism to render themselves into persistent storage.

Page 8: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

8

Entity Bean

• Why we need such persistent data components instead of the database data1、 Objects can be easily handled and managed2、We can group related data in a unified object. (view from multiple tables)3、We can associate methods with data (get/set)4、We can gain implicit middleware services from application server

• Entity Bean Files (2.x) remote/local interface (.java) home/local home interface (.java) enterprise bean class (.java)

Page 9: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

9

Features of Entity Beans

• Entity Bean Survive Failures

Entity beans are persistent objects which can survive critical failures, such as application server crashing, or machine crashing. Entity beans are just representations of data in a permanent, fault-tolerant underlying storage.

If a machine crashes, all we need to do is read the data back in from the permanent database and instantiate an entity bean Java object instance whose fields contain the data read in from the database.

Page 10: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

10

Features of Entity Beans

• Entity Bean Instances Are a View into a Database

This means if you update the in-memory entity bean instance, the database should automatically be updated as well. (Synchronization)

So there must be a mechanism to transfer information back and forth between the Java objects and the database. Entity bean class must implement two special methods, ejbStore and ejbLoad.

ejbStore: saving bean instance’s current fields to the underlying data storage

ejbLoad: reading data in from persistent storage into the entity bean’s in-memory fields

Page 11: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

11

Features of Entity Beans

The EJB container is in charge of the above methods invocations. This is one of the value-adds of the container.

Page 12: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

12

Features of Entity Beans

• Several Entity Bean Instances May Represent the Same Underlying DataTo facilitate many clients accessing the same data, one possibility is to allow many clients to share the same entity bean instance.

Unfortunately, it is not very appropriate for EJB.First, writing thread-safe code is difficult and error prone.Second, having multiple threads of execution makes transactions almost impossible to control.All bean instances are single-threaded in EJB. (Session, Entity, Message-Driven)

Page 13: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

13

Features of Entity Beans

To boost performance, we could allow container to instantiate multiple instances of the same entity bean class so that many clients could concurrently interact with separate instances, each representing the same underlying entity data.

Having multiple bean instances represent the same data, a new problem should be resolved: data corruption. Each entity bean instance needs to be routinely synchronized with the underlying storage.

Transactions allow each client request to be isolated from every other request.

Page 14: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

14

Instance 1

Instance 2

Instance 3

Storage

the same underlying data

transaction

Client 1

Client 2

Client 3

the same entity bean class instances representing the same underlying data, such as back account (find the same ID)

Container

Page 15: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

15

Features of Entity Beans

• Entity Bean Instances Can Be PooledThe container may pool and reuse entity bean instances to represent different instances of the same type of data in an underlying storage.The container performs this by dynamically assigning the entity bean instance to different client-specific EJB objects.

Entity bean class must implement two callback methods, ejbPassivate() and ejbActivate().

ejbActivate(): associating bean instance with a specific EJB object and a specific primary key, and required resource

as well (socket) ejbLoad after activation

ejbPassivate(): disassociating bean instance from a specific EJB object and a specific primary key, and required resource as well (socke

t) ejbStore() before passivation

Page 16: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

16

Page 17: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

17

Instance 1

Instance 2

Instance 3

Storage

different rows in storage

Client 1

Client 2

Client 3

the same entity bean class instances representing the different underlying data, such as back account (the same ID)

Container

Page 18: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

18

instance pool

Client 1

Client 2

Client 3

Client 4

Bean Instance 1

Bean Instance 2

Bean Instance 3

data 1

data 2

data 3

data 4

poolSize=3

Page 19: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

19

Features of Entity Beans

• Two ways to persist Entity Beans (2.x) BMP (bean-managed persistence)

We must write code to handle the persistent operations, including saving, loading, finding, creating and removing. (UPDATE, SELECT, INSERT, DELETE)

JDBC API

CMP (container-managed persistence)

The container perform the persistent operations for us. We could just inform the container about how we’d like to do by using SQL, or EJB QL.

Page 20: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

20

Features of Entity Beans

• Creation and Removal of Entity BeansejbCreate()

This method is responsible for creating database data.For each ejbCreate() defined in the bean class, we must define a corresponding ejbPostCreate() in the bean class and a corresponding create() in the home interface. We could define several overloaded ejbCreate() and create() methods if needed.

Bean Class: (primary key container)public AccountPK ejbCreate(String accountID, String owner)

Home interface: (EJB object client)public Account create(String accountID, String owner)

Page 21: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

21

Create an Entity Bean

Page 22: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

22

Creation and Removal of Entity Beans

ejbRemove()This method is responsible for removing database data.Note that, there is only one form of ejbRemove() method without any parameters. And ejbRemove() does not mean the in-memory entity bean instance is going to be destroyed, ejbRemove() destroys only database data.Both Home object and EJB object provide the remove() method which could be called by client.

(See also: J2EE/EJB API)

Page 23: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

23

Features of Entity Beans

• Entity Beans Can Be Found

Because entity beans are permanent objects, they can be found rather than created. This is a great difference between session beans and entity beans.

We can define the finder methods in our home interface. And the same as create() method, there should be the corresponding ejbFindXXX() methods in our bean class.

Bean Class: ejbFindByBalance()

Home interface findByBalance()

Page 24: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

24

Entity Context

• All enterprise beans have a context object that identifies the environment of the bean. (SessionContext, EntityContext, MessageDrivenContext)

EJBContext, EntityContext, setEntityContext() (See also: J2EE/EJB API)

getEJBLocalObject()/getEJBObject()to retrieve the current,client-specific EJB object that is associated with the entity bean.

getPrimaryKey()to retrieves the primary key that is currently associated with this entity bean instance. (ejbLoad(), ejbRemove())

Page 25: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

25

Bean-Managed Persistent Entity Beans

• Entity Bean Coding Basics

javax.ejb.EnterpriseBean

javax.ejb.EntityBean

Page 26: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

26

BMP Object Model

Page 27: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

27

BMP Lifecycle

Page 28: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

28

Entity Bean Coding Basics

• Required Methods in Entity Bean ClasssetEntityContext() ejbLoad()

ejbFind<...>(<...>) ejbStore() ejbHome<...>(<...>) ejbRemove() ejbCreate(<...>) ejbActivate()

ejbPostCreate(<...>) ejbPassivate()(See also: J2EE/EJB API or Table 6.1 Page 130)

(Demo1: lec8/BMP/readme.txt) (Demo2: lec8/BMPMysql/readme.txt

Deploy the third-party datasource:MySQL)

Page 29: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

29

Entity Bean Configuration

Page 30: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

30

Entity Bean Configuration

Page 31: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

31

DataSource ConfigurationTools --> Server Configuration

Page 32: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

32

Resource Reference

Page 33: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

33

Using Cloudscape

• Start servercloudscape –start

• Create Tablecloudscape -isql < CreateTable.sql

• Stop servercloudscape –stop

Page 34: Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.

34

Third-party Datasource: MySQL

• Register the database driverj2eeadmin -addJdbcDriver org.gjt.mm.mysql.Driver

• Set classpathCopy mysql-XXX-bin.jar to the directory belowModify %J2EE_HOME%\bin\userconfig.bat

SET J2EE_CLASSPATH=%J2EE_HOME%\lib\system\mysql-connector-java-2.0.14-bin.jar

• Deploy a datasourcej2eeadmin -addJdbcDatasource jdbc/mysql

jdbc:mysql://localhost:3306/test