enterprise javabeans · enterprise javabeans layer:07 entity. last revised: 10/21/2001 copyright...
TRANSCRIPT
Enterprise JavaBeans
Layer:07
Entity
Last Revised: 10/21/2001 Copyright (C) 2001 2
Agenda
• Build entity beans.• Describe the bean's lifecycle.• Describe the server's free pool.
Entity Beans
Last Revised: 10/21/2001 Copyright (C) 2001 4
Purpose
• Entity beans represent business data.• In general, an entity bean should only be
used to represent a conceptual entity that requires some kind of persistent storage.
• A poorly architected entity bean will destroy your performance.
Last Revised: 10/21/2001 Copyright (C) 2001 5
Structure
• The bean class may extend any java class.• It must implement the EntityBean
interface.• Each entity bean must minimally provide a findByPrimaryKey() method.
Last Revised: 10/21/2001 Copyright (C) 2001 6
Bean Instance Lifecycle
Last Revised: 10/21/2001 Copyright (C) 2001 7
EntityBean Interface(1 of 2)
• The EntityBean interface declares a set of container-callback methods.
• These methods must be implemented by the bean class and are used by the container to manage the bean instance.
• The complete interface is on the following slide.
Last Revised: 10/21/2001 Copyright (C) 2001 8
EntityBean Interface(2 of 2)
1. public interface EntityBeanextends EnterpriseBean {
2. public void ejbActivate();
3. public void ejbLoad();
4. public void ejbPassivate();
5. public void ejbRemove();
6. public void ejbStore();
7. public voidsetEntityContext(EntityContext sc);
8. public void unsetEntityContext()
9. }
Last Revised: 10/21/2001 Copyright (C) 2001 9
ejbActivate(1 of 2)
• The ejbActivate() method is called by the container whenever an entity bean is "loaned out" from the free pool.
• This method should prepare the bean to perform work based on the business methods as well as the synchronization callbacks.
Last Revised: 10/21/2001 Copyright (C) 2001 10
ejbActivate(2 of 2)
1. public void ejbActivate() {
2. // TODO: perform any required resource
3. // allocations to ensure that the
4. // bean is capable of handling
5. // business requests.
6. }
Last Revised: 10/21/2001 Copyright (C) 2001 11
ejbLoad(1 of 2)
• The ejbLoad() method is called by the container whenever an entity bean is associated with an EJBObject.
• This method is used to synchronize the bean's internal data members with those of the underlying data store.
• The container is required to call this method at least once after the start of a transaction.
Last Revised: 10/21/2001 Copyright (C) 2001 12
ejbLoad(2 of 2)
1. public void ejbLoad() {
2. // TODO: synchronize the bean's data
3. // members with the appropriate
4. // fields from the underlying data
5. // store.
6. //
7. // TODO: any final initialization to be
8. // complete prior to the invocation
9. // of a business method.
10. }
Last Revised: 10/21/2001 Copyright (C) 2001 13
ejbPassivate(1 of 2)
• The ejbPassivate() method is called by the container whenever a bean is returned to the free pool.
• This method should set all persistable fields to null.
Last Revised: 10/21/2001 Copyright (C) 2001 14
ejbPassivate(2 of 2)
1. public void ejbPassivate() {
2. // TODO: set all non-persistable fields
3. // to null.
4. }
Last Revised: 10/21/2001 Copyright (C) 2001 15
ejbRemove(1 of 2)
• The ejbRemove() method is called by the container when:– A client calls the remove() method on their
remote reference or on the home interface.• For entity beans, a call to ejbRemove()
typically indicates that a physical delete against the database will be performed.
Last Revised: 10/21/2001 Copyright (C) 2001 16
ejbRemove(2 of 2)
1. public void ejbRemove() {
2. // TODO: perform a physical or logical
3. // delete according to the business
4. // rules.
5. //
6. // TODO: de-allocate scarce resources and
7. // set object references to null to
8. // help the garbage collector.
9. }
Last Revised: 10/21/2001 Copyright (C) 2001 17
ejbStore(1 of 2)
• The ejbStore() method is called by the container whenever an entity bean is dissociated with an EJBObject.
• This method is used to synchronize the underlying data store with the bean's internal data members.
• The container is required to call this method at least once before the end of a transaction.
Last Revised: 10/21/2001 Copyright (C) 2001 18
ejbStore(2 of 2)
1. public void ejbStore() {
2. // TODO: any final initialization to be
3. // complete prior to persisting the
4. // bean's state.
5. //
6. // TODO: synchronize the underlying data
7. // store with the bean's internal
8. // data members.
9. }
Last Revised: 10/21/2001 Copyright (C) 2001 19
setEntityContext(1 of 2)
• The setEntityContext() method is called by the container when the bean is first added to the free pool.
• Be sure to capture a reference to the EntityContext argument for later use.
• Use this method for one-time initialization with the benefit of the EntityContext.
Last Revised: 10/21/2001 Copyright (C) 2001 20
setEntityContext(2 of 2)
1. public voidsetEntityContext(EntityContext ec) {
2. this.context = ec;
3. // TODO: any other one-time actions
4. }
EntityContext
Last Revised: 10/21/2001 Copyright (C) 2001 22
Purpose
• The EntityContext interface inherits from the EJBContext.
• It provides additional methods that are critical for proper integration of the entity beans into their containers.
Last Revised: 10/21/2001 Copyright (C) 2001 23
Structure(1 of 2)
1. public interface EntityContextextends EJBContext {
2. public EJBObject getEJBObject()throws IllegalStateException;
3. public Object getPrimaryKey()throws IllegalStateException;
4. }
Last Revised: 10/21/2001 Copyright (C) 2001 24
Structure(2 of 2)
• Each of these methods can throw an IllegalStateException if it's invoked at any time while the bean instance is not associated with an EJBObject.
• We'll look at the lifecycles a bit later to see when this situation might occur.
Primary Keys
Last Revised: 10/21/2001 Copyright (C) 2001 26
Purpose
• Primary keys are used to uniquely identify bean instances within a given home.
• Each primary key is associated with an EJBObject, not with a bean instance.
• This allows any bean instance to satisfy any business method on behalf of any client.
• This increases the scalability of the server.
Last Revised: 10/21/2001 Copyright (C) 2001 27
Structure(1 of 2)
• Primary keys can be of any type although they should be compatible with RMI-IIOP.
• If we're dealing with only a single column key within the data store, then we can use one of the wrapper classes such as String, Double, or Integer.
• Otherwise we need to build a custom class.
Last Revised: 10/21/2001 Copyright (C) 2001 28
Structure(2 of 2)
• Primary keys are often constructed using public data members to allow a performance improvement when using CMP.
• In EJB 2.0, this restriction was relaxed and now primary keys can follow the JavaBeans naming conventions.
Last Revised: 10/21/2001 Copyright (C) 2001 29
BookKey
1. public class BookKeyimplements java.io.Serializable {
2. public String isbn = null;
3. public BookKey(String isbn) {
4. this.isbn = isbn;
5. }
6. public String getISBN() {
7. return this.isbn;
8. }9. }
Scalability
Last Revised: 10/21/2001 Copyright (C) 2001 31
Free Pool(1 of 3)
• Entity beans are stored within a free pool.• This is similar to a database connection pool
in that entity bean instances are loaned out to clients on a demand basis.
• The policies around the use and management of this pool are left to the server and container providers.
Last Revised: 10/21/2001 Copyright (C) 2001 32
Free Pool(2 of 3)
• The setEntityContext() method is called when the bean is first added to the free pool.
• The unsetEntityContext() method is called when the bean is removed from the free pool for the last time.
Last Revised: 10/21/2001 Copyright (C) 2001 33
Free Pool(3 of 3)
• The ejbActivate() method is called when the bean is "loaned out" of the free pool.
• The ejbPassivate() method is called when the bean is returned to the free pool.
Runtime Environment
Last Revised: 10/21/2001 Copyright (C) 2001 35
Creating Entity Beans
Last Revised: 10/21/2001 Copyright (C) 2001 36
Creating(1 of 9)
• At some point the free pool will be populated with a set of entity bean instances.
• The specifics are left to the container.• When each bean is added to the free pool its setEntityContext() method will be called.
Last Revised: 10/21/2001 Copyright (C) 2001 37
Creating(2 of 9)
• The client has already performed a JNDI lookup on the home interface as usual.
• When the client is ready to create a new entry in the backing store, then the client will call the appropriate create()method on the home interface.
Last Revised: 10/21/2001 Copyright (C) 2001 38
Creating(3 of 9)
• The home object will request a bean instance of the free pool.
• The ejbActivate() method will be called on the bean instance to prepare it for the creation process.
Last Revised: 10/21/2001 Copyright (C) 2001 39
Creating(4 of 9)
• The home object calls the ejbCreate()on the bean instance.
• The bean instance can perform any required preprocessing of the data.
Last Revised: 10/21/2001 Copyright (C) 2001 40
Creating(5 of 9)
• The bean instance uses JDBC to perform that actual insertion into the database.
• It is the bean's job to catch and handle any SQLExceptions that might result from this process.
Last Revised: 10/21/2001 Copyright (C) 2001 41
Creating(6 of 9)
• The primary key of the data that was just created is returned from the ejbCreate() method using the appropriate primary key type.
• It's the bean instance's job to create and populate the key.
Last Revised: 10/21/2001 Copyright (C) 2001 42
Creating(7 of 9)
• The home object creates a new EJBObject.• The primary key returned from the ejbCreate() method is placed into that EJBObject.
Last Revised: 10/21/2001 Copyright (C) 2001 43
Creating(8 of 9)
• The home object calls the appropriate version of the ejbPostCreate()method on the bean instance.
• This allows the bean to complete its initialization with access to the EJBObject.
Last Revised: 10/21/2001 Copyright (C) 2001 44
Creating(9 of 9)
• The home object returns the remote reference to the client the same way that it was done using session beans.
Last Revised: 10/21/2001 Copyright (C) 2001 45
Using Entity Beans
Last Revised: 10/21/2001 Copyright (C) 2001 46
Using(1 of 6)
• Eventually the client invokes a business method on the entity bean's remote reference.
Last Revised: 10/21/2001 Copyright (C) 2001 47
Using(2 of 6)
• The EJBObject asks the container for a bean instance to be used to satisfy the client's request.
Last Revised: 10/21/2001 Copyright (C) 2001 48
Using(3 of 6)
• The container locates and activates a bean instance from the free pool.
• The bean's ejbActivate() method is called to make it method-ready.
Last Revised: 10/21/2001 Copyright (C) 2001 49
Using(4 of 6)
• The container invokes the bean's ejbLoad() method to synchronize the bean's state with the state of the primary key associated with the EJBObject on which the client made the original call.
Last Revised: 10/21/2001 Copyright (C) 2001 50
Using(5 of 6)
• The bean instance makes a call to the database to retrieve the appropriate data.
Last Revised: 10/21/2001 Copyright (C) 2001 51
Using(6 of 6)
• The EJBObject makes the same call to the bean instance that the client made on the remote reference.
Last Revised: 10/21/2001 Copyright (C) 2001 52
Passivating Entity Beans
Last Revised: 10/21/2001 Copyright (C) 2001 53
Passivating(1 of 3)
• At some point the container will want the bean instance returned to the free pool so that it can be loaned to another client.
• The container will call the bean's ejbStore() method to synchronize the underlying backing store with the bean's state.
Last Revised: 10/21/2001 Copyright (C) 2001 54
Passivating(2 of 3)
• The bean issues and appropriate update against the underlying data source.
Last Revised: 10/21/2001 Copyright (C) 2001 55
Passivating(3 of 3)
• The container calls the bean's ejbPassivate() method and places the bean back into the free pool.
Last Revised: 10/21/2001 Copyright (C) 2001 56
Removing Entity Beans
Last Revised: 10/21/2001 Copyright (C) 2001 57
Removing(1 of 5)
• At some point the client may choose to remove the bean.
• The client calls the remove() method on their remote reference.
Last Revised: 10/21/2001 Copyright (C) 2001 58
Removing(2 of 5)
• The EJBObject informs the container that it requires a bean to perform a remove.
• The container goes through the normal synchronization process when providing the bean instance to the EJBObject.
Last Revised: 10/21/2001 Copyright (C) 2001 59
Removing(3 of 5)
• The container calls the bean's ejbRemove() method.
Last Revised: 10/21/2001 Copyright (C) 2001 60
Removing(4 of 5)
• The bean issue's the appropriate database calls for the removal.
• This could be logical or physical.• The bean could throw an exception to
ensure that a deletion never takes place.
Last Revised: 10/21/2001 Copyright (C) 2001 61
Removing(5 of 5)
• The container call's the bean's ejbPassivate() method and returns the bean instance to the free pool.
Last Revised: 10/21/2001 Copyright (C) 2001 62
Design
• Entity beans are not meant to be table wrappers– This is one of the leading causes of poor
performance using entity beans.• They're meant to be course-grained business
objects.
Last Revised: 10/21/2001 Copyright (C) 2001 63
Summary
• Build entity beans.• Describe the bean's lifecycle.• Describe the server's free pool.
Last Revised: 10/21/2001 Copyright (C) 2001 64
Next Steps
• Please read chapter 6, pp.153-211.• Layer:07 homework will be available on the
course website.