Difference between Stateless and Stateful Session beans.
Persistence Persistence Java Object Serialization Three ways to make an object persistent Object Relational Mapping Object database Persistence Storage Obj 1 Obj 2 Persistence Objects Storage Storage Database
Java Object Serialization Object 1 Object 2 Object n State of the object Compact representation Marshall an object graph into a compact representation Serialization of object graph into byte stream Developer pushes data over the network or saves the stream to a storage Byte Stream
Object Relational Mapping Database API Relational Database Manual Mapping Use an Object- Relational Mapping Product Object Travel Account String Name String TktNo Double Amount
Object Database Persistence
Object database persistence is designed to store Java Objects as whole objects which means that there is no need to program a relational database.
The Object Query Language (OQL) provides the facility to query the persisted objects.
This language adds a layer of abstraction from the relational database queries.
The EJB QL eliminates the inconsistencies caused due to the use of the different Query languages used by different application servers.
Components deployed in Multi-tier Deployment- Application logic components Application logic components are components that provide methods which perform common tasks Computing the price of a ticket Billing of products
Components deployed in Multi-tier Deployment - Persistent Data Components Object Serialization of data Database Examples are air ticket information such as ticket number and amount Employee data such as salaries and place of work
Files in an Entity bean Entity Bean Remote Interface Primary Key Class Entity Bean Class Deployment Descriptors Home Interface Local Interfaces
Entity Bean class Models Java Class Persistent Data Maps Database Schema Entity Definition For example , an entity bean class can map to a relational table definition. An entity bean instance of that class will then map to a row in that table.
The Entity Bean’s Remote Interface Client Entity Bean Invokes Business Method signatures Remote Interface
The Home Interface create find destroy The home interface is used by the clients to create, find and destroy entity bean objects. Client Uses Methods Home Interface Entity Bean Object Entity Bean Object Entity Bean Object
Local Interface Local Clients Entity Beans Methods Entity Beans Exposes Local Interface allows the beans to expose its methods to other beans that reside within the same container (local clients). Container
The Primary Key Class
Primary keys make each entity bean look different.
A primary key is an object itself which contains other objects and data that is necessary to identify an entity bean data instance.
The primary key class has to be serializable, and has to follow the rules of Java object serialization.
The unique identifier, or primary key, enables client to locate the particular entity bean.
The Deployment Descriptors Container Deployment descriptors List of properties Contacts the deployment descriptor for bean deployment Informs the container about the bean and classes
Pooling of Entity Bean Instances EJB Container Entity Bean 1 Entity Bean 2 Entity Bean n Instantiates Storage Data 1 Data 2 Data n Represents Entity Beans Pooled and Recycled Entity Bean 1 When the bean instance is used ,it is assigned to handle different client request.
Ways to Persist Entity Beans Data Store Entity Bean Entity Beans map themselves in the data store Bean-managed Persistence Container- Managed Persistence Deployment Descriptor Persistent Beans Deployment Descriptor tells the container about the persistent fields and then the container handles the data logic
Working with Entity Beans
The entity bean instance and the underlying database can be considered as one and the same.
When the bean-managed persistent bean calls the ejbCreate() method, it creates the database data.
In case of container-managed persistence, the container contains required data access logic, leaving the bean’s methods empty of data access logic.
The finder methods are used to find the existing entity bean in storage. The finder methods do not create any new database data, but they load old entity bean data.
Modifying Entity Beans Directly though the Database EJB Container/ Server Entity Beans Existing Application Database O/R Mapping Direct Database Modifications Bean data
Developing and Using Entity Beans
In order to write an entity bean class, the javax.ejb.EntityBean interface has to be implemented. It is this interface that defines the call back method used by the container which the bean must implement.
Used to initialize the fields of a bean instance which can be used for a particular client to create the underlying database data.
The parameters vary with respect to the ejbCreate() method. Therefore, there are multiple ways to initialize an entity bean instance.
The ejbCreate() methods in the home interface have to be duplicated in the bean class.
ejbFind ( ) Creating a BMP EJB and an EJB object Home object EJB object Entity bean instance EJB Container/ Server 1. Call create() 6. Returns object to the client 2. Call ejbCreate() 4. Returns primary key Client Code 3. Create database data 5. Create EJB object
ejbFind ( ) - rules
All finder methods have to begin with “ejbFind”.
There has to be at least one finder method by the name ejbFindByPrimaryKey(). The method finds a unique entity bean instance based on the unique primary key.
There can be various finder methods, with different names and different parameters.
The finder method has to return the primary key for the entity bean it finds. Other than this, it can also give an enumeration of primary keys.
The client will never call the finder methods on the bean instance itself
ejbRemove( ) - I
Called to remove data from the database
The instance of the bean can be recycled to handle data from a different database
Does not take any parameters
ejbRemove( )- II Destroying an entity bean’s data representation Home object EJB object Entity bean instance EJB Container/ Server 1. Call remove() 2. Call ejbRemove() Client Code 3. Remove database data 1. Call remove() 2. Call ejbRemove()
Enterprise beans have a context object which identifies the bean’s environment.
Context objects are accessible to the beans in order to retrieve information, such as transaction and security information.
The interface to be used for entity beans is the javax.ejb.EntityContext.
Two new methods have been added to entity beans:
Allows JMS applications to receive messages asynchronously (the sender is independent of the receiver receiving and processing the messages).
Includes business logic, which may include operations such as:
* Performing computation on received data
* Initiating a step or condition in a workflow
* Storing data
* Sending a message
Uses of Message-Driven Beans
Integrating two applications in a loosely coupled but reliable manner.
Mere message delivery or message content should drive other events in the system.
Create message selectors which are designed to consume only specific messages and thus act as triggers.
Implementation of Message Beans
It mainly implements two interfaces javax.ejb.MessageDrivenBean interface and the javax.jms.MessageListener interface.
The developer needs to implement the main business-logic in the onMessage() method. The bean implements three other methods viz. ejbCreate() ejbRemove() and setMessageDrivenContext() which the container uses for managing the lifecycle of the message-driven bean.
Summary - 1
There are three main ways to make objects persistent:
Java Object Serialization
Object Database Management System
Object serialization is a method by which the current state of Java objects can be captured and saved permanently.
There are two ways of mapping the objects to the relational data:
Manual mapping: This is done using a database access API, which can be JDBC or SQL/J.
Use an object-relational mapping product. It can be Sun’s JavaBlend or Object people’s TOP link.
Two kinds of components are deployed in a multi-tier deployment:
Application logic components
Persistent data components
The entity bean comprises the following files:
The entity bean class
The remote interface
The Local Interface
The home interface
The primary key class
The deployment descriptors
ejbActivate(): When a bean has to be transitioned out of an instance pool, the ejbActivate() callback method is used.
ejbPassivate(): When the bean is being sent into the instance pool, this method is called.
Enterprise beans have a context object that identifies the bean’s environment. These context objects are accessible to the beans in order to retrieve transaction and security information.