1 / 34

Middleware Technology (J2EE/EJB) Entity Bean

Middleware Technology (J2EE/EJB) Entity Bean. 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 stream . Marshalling.

malo
Download Presentation

Middleware Technology (J2EE/EJB) Entity Bean

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Middleware Technology (J2EE/EJB) Entity Bean

  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 stream Marshalling Unmarshalling We can push the stream over network or save the stream to storage, such as a file system, database, or JNDI tree.

  3. Persistence Concepts Problem: The stream is not relevant to content. It’s expensive and cumbersome to query objects stored using object serialization. Example: bank account • Object-Relational Mapping When 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

  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

  5. An example of object-relational mapping (Bank Account)

  6. Persistence Concepts • Object Databases Java 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

  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.

  8. Entity Bean • Why we need such persistent data components instead of the database data 1、Objects can be easily handled and managed 2、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)

  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.

  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

  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.

  12. Features of Entity Beans • Several Entity Bean Instances May Represent the Same Underlying Data To 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)

  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.

  14. the same entity bean class instances representing the same underlying data, such as back account (find the same ID) Container Client 1 Instance 1 transaction Storage Client 2 Instance 2 the same underlying data Client 3 Instance 3

  15. Features of Entity Beans • Entity Bean Instances Can Be Pooled The 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 (socket) ejbStore() before passivation

  16. the same entity bean class instances representing the different underlying data, such as back account (the same ID) Container Client 1 Instance 1 Storage Client 2 Instance 2 different rows in storage Client 3 Instance 3

  17. Client 1 poolSize=3 instance pool Client 2 data 1 Bean Instance 1 data 4 data 2 Bean Instance 2 Client 3 data 3 Bean Instance 3 Client 4

  18. 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.

  19. Features of Entity Beans • Creation and Removal of Entity Beans • ejbCreate() 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)

  20. Create an Entity Bean

  21. 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)

  22. 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()

  23. 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())

  24. Bean-Managed Persistent Entity Beans • Entity Bean Coding Basics javax.ejb.EnterpriseBean javax.ejb.EntityBean

  25. BMP Object Model

  26. BMP Lifecycle

  27. Entity Bean Coding Basics • Required Methods in Entity Bean Class setEntityContext() 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)

  28. Entity Bean Configuration

  29. Entity Bean Configuration

  30. DataSource ConfigurationTools --> Server Configuration

  31. Resource Reference

  32. Using Cloudscape • Start server cloudscape –start • Create Table cloudscape -isql < CreateTable.sql • Stop server cloudscape –stop

  33. Third-party Datasource: MySQL • Register the database driver j2eeadmin -addJdbcDriver org.gjt.mm.mysql.Driver • Set classpath Copy mysql-XXX-bin.jar to the directory below Modify %J2EE_HOME%\bin\userconfig.bat SET J2EE_CLASSPATH=%J2EE_HOME%\lib\system\mysql-connector-java-2.0.14-bin.jar • Deploy a datasource j2eeadmin -addJdbcDatasource jdbc/mysql jdbc:mysql://localhost:3306/test

More Related