Ejbe slides

531 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
531
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
25
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Ejbe slides

  1. 1. Module EJBE Entity EJBs At the end of this module you will be able to: • understand how entity EJBs stay persistent • write entity EJBs from scratch • connect an entity EJB to a data source Developing Enterprise Applications with BEA WebLogic Server EJBE-1 Copyright © 2000, BEA Systems Inc.
  2. 2. Section 1 Entity EJB Basics At the end of this section you will be able to: • design an entity EJB that is mapped to a datasource • describe the differences between CMP and BMP • write a container managed entity EJB Developing Enterprise Applications with BEA WebLogic Server EJBE-2 Copyright © 2000, BEA Systems Inc.
  3. 3. What is an Entity EJB? § Entity EJBs have the following behavior: Ø they are a representation of persistent data Ø they can survive a crash Ø multiple clients can be using EJBs that represent the same data Ø the EJB instance contains a “copy” of the data in the persistent store Developing Enterprise Applications with BEA WebLogic Server EJBE-3 Copyright © 2000, BEA Systems Inc.
  4. 4. Persistence Management § The attributes of the object must be stored using a persistent mechanism. § Examples of persistence management include: Ø serializationto a file Ø relational database mappings through JDBC Object in memory Persistent Storage Client -clientID database, -corporateName files, -address etc... -town -postcode Developing Enterprise Applications with BEA WebLogic Server EJBE-4 Copyright © 2000, BEA Systems Inc.
  5. 5. Shared Entity EJBs § When multiple clients share an entity EJB they: Ø receive their own instance Ø share the underlying data Ø do not have to handle synchronization Client Context Persistent Storage Client Entity EJB Client Server Client Container Developing Enterprise Applications with BEA WebLogic Server EJBE-5 Copyright © 2000, BEA Systems Inc.
  6. 6. Primary Keys § Every entity EJB has a set of attributes that are unique when aggregated together. § The aggregated attributes are called the primary key of an entity EJB. Developing Enterprise Applications with BEA WebLogic Server EJBE-6 Copyright © 2000, BEA Systems Inc.
  7. 7. Object-Relational Mapping § An O-R mapping stores each object in a single row of a table. § A database column is associated with each attribute of the class. Client •Client Id Primary •Corporatename key •Address •Town •Postcode clientId Corporate Name Address Town Postcode 411MEB MEB 1, Lichfield Road B ’ham B7 4TU 411PIT PITCH 34, London Road Chester C60 7YJ 411WC World Company 1, The Strand London W1 8PL Developing Enterprise Applications with BEA WebLogic Server EJBE-7 Copyright © 2000, BEA Systems Inc.
  8. 8. Identical Enitity EJBs § Two entity EJBs are identical if the contents of their primary keys are identical. Primary Key Client Context Persistent Storage Client Entity EJB Client Server Client Container Developing Enterprise Applications with BEA WebLogic Server EJBE-8 Copyright © 2000, BEA Systems Inc.
  9. 9. Types of Persistence § There are two types of persistent EJBs: Ø Container-Managed Persistence (CMP) Ø Bean-Managed Persistence (BMP) The Logic is CMP here! Client Client Context Persistent Storage Entity EJB BMP Server Client Container Developing Enterprise Applications with BEA WebLogic Server EJBE-9 Copyright © 2000, BEA Systems Inc.
  10. 10. Entity vs. Session EJB Authoring Differences Between Writing Entity EJBs and Session EJBs: Differences Between Writing Entity EJBs and Session EJBs: 1. You must write aaprimary key class. 1. You must write primary key class. 2. The home interface must have aafindByPrimaryKey(<PrimaryKey>) 2. The home interface must have findByPrimaryKey(<PrimaryKey>) method. method. 3. The home interface can have other find(…) methods. 3. The home interface can have other find(…) methods. 4. The bean class must implement the javax.ejb.EntityBean interface. 4. The bean class must implement the javax.ejb.EntityBean interface. 5. The bean class must provide an ejbCreate(…) AND an 5. The bean class must provide an ejbCreate(…) AND an ejbPostCreate(…) for each create(…) listed in the home interface. ejbPostCreate(…) for each create(…) listed in the home interface. Developing Enterprise Applications with BEA WebLogic Server EJBE-10 Copyright © 2000, BEA Systems Inc.
  11. 11. Primary Key Classes § A primary key object is the unique identifier of an EJB. § A primary key class must: Ø bedeclared public Ø implement java.io.Serializable Ø have a no-argument constructor Ø have all of its attributes be declared public Sample Primary Key Class: Sample Primary Key Class: public class AlarmClockPK implements java.io.Serializable { public class AlarmClockPK implements java.io.Serializable { public int serialNumber; public int serialNumber; public String currentStation; public String currentStation; }} Developing Enterprise Applications with BEA WebLogic Server EJBE-11 Copyright © 2000, BEA Systems Inc.
  12. 12. Primary Key Attributes § The EJB class must contain the same parameters listed in the primary key. AlarmClockPK AlarmClockBean •int serialNum; •int serialNum; •String currentStation; •String currentStation; •long currentVolume; •Date wakeUpTime; •boolean isFm; Developing Enterprise Applications with BEA WebLogic Server EJBE-12 Copyright © 2000, BEA Systems Inc.
  13. 13. CMP EJB Creation Container-Provided Classes Entity Synchronization : Client EJBHome EJBObject Context instance database create(args) ejbCreate(args) extract container managed fields create entity representation in DB new ejbPostCreate(args) Return remote stub containing Create primary key primary key instance! instance with values extracted! Developing Enterprise Applications with BEA WebLogic Server EJBE-13 Copyright © 2000, BEA Systems Inc.
  14. 14. Home Interfaces § Entity EJB home interfaces must include one or more find(…) methods. Syntax for findByPrimaryKey(…) Method: Syntax for findByPrimaryKey(…) Method: public <RemoteIF> findByPrimaryKey(<PrimaryKey> pk) throws FinderException, RemoteException; Syntax for Other find(…) Methods: Syntax for Other find(…) Methods: public <RemoteIF> findWhatever(…) throws FinderException, RemoteException; public Enumeration findWhatever(…) throws FinderException, RemoteException; Developing Enterprise Applications with BEA WebLogic Server EJBE-14 Copyright © 2000, BEA Systems Inc.
  15. 15. Attribute and find(…) Method Mapping Developing Enterprise Applications with BEA WebLogic Server EJBE-15 Copyright © 2000, BEA Systems Inc.
  16. 16. Attribute and find(…) Method Mapping Developing Enterprise Applications with BEA WebLogic Server EJBE-16 Copyright © 2000, BEA Systems Inc.
  17. 17. Finder Expressions Syntax for Expressions: Syntax for Expressions: operator operand1 operand2 Available Operators: Available Operators: (), =, <, >, <=, >=, !, &, |, like Available Operands: Available Operands: 1. Another Expression 2. An EJB Attribute 3. A find(…) Method Parameter (Must precede with $) Finder Expression Examples: Finder Expression Examples: (> balance $amount) (& (> bal $amount) (! (= accountType ‘checking’))) (= 1 1) (like lastName M%) Developing Enterprise Applications with BEA WebLogic Server EJBE-17 Copyright © 2000, BEA Systems Inc.
  18. 18. Writing the Bean Class § The bean class must implement the EntityBean interface. The EntityBean Interface: The EntityBean Interface: public interface EntityBean extends EnterpriseBean { public interface EntityBean extends EnterpriseBean { public void ejbActivate(); public void ejbActivate(); public void ejbPassivate(); public void ejbPassivate(); public void ejbLoad(); public void ejbLoad(); public void ejbStore(); public void ejbStore(); public void setEntityContext(EntityContext ctx); public void setEntityContext(EntityContext ctx); public void unsetEntityContext() public void unsetEntityContext() public void ejbRemove(); public void ejbRemove(); }} Some Other Guidelines: Some Other Guidelines: 1. Your ejbCreate(…) methods should return void for CMP. 1. Your ejbCreate(…) methods should return void for CMP. 2. You do not have to implement any ejbFind(…) methods for CMP. 2. You do not have to implement any ejbFind(…) methods for CMP. Developing Enterprise Applications with BEA WebLogic Server EJBE-18 Copyright © 2000, BEA Systems Inc.
  19. 19. Entity EJB Life Cycle instance throws does not system exception from exist any method newInstance() unsetEntityContext() setEntityContext(ec) ejbFind<METHOD>() ejbCreate(args) ejbPostCreate(args) pooled ejbRemove() ejbActivate() ejbPassivate() ready ejbStore() ejbLoad() business method Developing Enterprise Applications with BEA WebLogic Server EJBE-19 Copyright © 2000, BEA Systems Inc.
  20. 20. Passivation Container-Provided Classes Entity Synchro- : Client EJBHome EJBObject Container Context instance database nization business method business method ejbStore() extract container managed fields updated entity state in DB ejbPassivate() Developing Enterprise Applications with BEA WebLogic Server EJBE-20 Copyright © 2000, BEA Systems Inc.
  21. 21. Activation Container-Provided Classes Entity Synchro- : Client EJBHome EJBObject Container Context instance database nization business method ejbActivate() read entity state in DB set container-managed fields ejbLoad() business method Developing Enterprise Applications with BEA WebLogic Server EJBE-21 Copyright © 2000, BEA Systems Inc.
  22. 22. Standard Entity EJB Exceptions § The EJB specification defines five standard EJB application exceptions: Ø javax.ejb.CreateException Ø javax.ejb.DuplicateException Ø javax.ejb.FinderException Ø javax.ejb.ObjectNotFoundException Ø javax.ejb.RemoveException Developing Enterprise Applications with BEA WebLogic Server EJBE-22 Copyright © 2000, BEA Systems Inc.
  23. 23. Finally, An Example! § Let’s write an entity EJB that tracks different alarm clock settings. § The EJB will track: Ø the time the alarm is supposed to go off Ø the current radio station selected Ø AM/FM radio selection Developing Enterprise Applications with BEA WebLogic Server EJBE-23 Copyright © 2000, BEA Systems Inc.
  24. 24. The Remote Interface The AlarmClock Remote Interface: The AlarmClock Remote Interface: public interface AlarmClock extends Remote, EJBObject { public interface AlarmClock extends Remote, EJBObject { public Date getAlarmTime() throws RemoteException; public Date getAlarmTime() throws RemoteException; public String getRadioStation() throws RemoteException; public String getRadioStation() throws RemoteException; public boolean isFmSet() throws RemoteException; public boolean isFmSet() throws RemoteException; public void isFmSet(boolean fm) throws RemoteException; public void isFmSet(boolean fm) throws RemoteException; public void setAlarmTime(Date time) throws RemoteException; public void setAlarmTime(Date time) throws RemoteException; public void setRadioStation(String station) public void setRadioStation(String station) throws RemoteException; throws RemoteException; }} Developing Enterprise Applications with BEA WebLogic Server EJBE-24 Copyright © 2000, BEA Systems Inc.
  25. 25. The Home Interface and Primary Key AlarmClockPK Primary Key Class: AlarmClockPK Primary Key Class: public class AlarmClockPK implements java.io.Serializable { public class AlarmClockPK implements java.io.Serializable { public int serialNumber; public int serialNumber; public String currentStation; public String currentStation; }} The AlarmClockHome Home Interface: The AlarmClockHome Home Interface: public interface AlarmClockHome extends EJBHome { public interface AlarmClockHome extends EJBHome { AlarmClock create() throws CreateException, RemoteException; AlarmClock create() throws CreateException, RemoteException; AlarmClock create(Date time, String station, boolean fm) AlarmClock create(Date time, String station, boolean fm) throws CreateException, RemoteException; throws CreateException, RemoteException; AlarmClock findByPrimaryKey(AlarmClockPK id) AlarmClock findByPrimaryKey(AlarmClockPK id) throws FinderException, RemoteException; throws FinderException, RemoteException; Enumeration findAllFMSettings() Enumeration findAllFMSettings() throws FinderException, RemoteException; throws FinderException, RemoteException; }} Developing Enterprise Applications with BEA WebLogic Server EJBE-25 Copyright © 2000, BEA Systems Inc.
  26. 26. The Bean Class Finally! The AlarmClockBean EJB Class: The AlarmClockBean EJB Class: public class AlarmClockBean implements EntityBean { public class AlarmClockBean implements EntityBean { public int id; public int id; transient protected EntityContext context; transient protected EntityContext context; // Container managed fields // Container managed fields public String station; public String station; public String wakeUpTime; public String wakeUpTime; public boolean isFm; public boolean isFm; Developing Enterprise Applications with BEA WebLogic Server EJBE-26 Copyright © 2000, BEA Systems Inc.
  27. 27. The Bean Class Finally! The AlarmClockBean EJB Class Continued: The AlarmClockBean EJB Class Continued: public void ejbActivate() {} public void ejbActivate() {} public void ejbLoad() {} public void ejbLoad() {} public void ejbPassivate() {} public void ejbPassivate() {} public void ejbRemove() {} public void ejbRemove() {} public void ejbStore() {} public void ejbStore() {} public void ejbCreate() { public void ejbCreate() { // Generate a unique ID for this EJB. // Generate a unique ID for this EJB. // Most EJBs pass receive this value as a parameter. // Most EJBs pass receive this value as a parameter. this.id = (int)(Math.random()*100000); this.id = (int)(Math.random()*100000); // Get the station and time from the environment // Get the station and time from the environment // properties. // properties. Properties props = context.getEnvironment(); Properties props = context.getEnvironment(); this.station = props.getProperty("Station"); this.station = props.getProperty("Station"); this.isFm = props.getProperty("isFM"); this.isFm = props.getProperty("isFM"); this.wakeUpTime = props.getProperty("Time"); this.wakeUpTime = props.getProperty("Time"); }} Developing Enterprise Applications with BEA WebLogic Server EJBE-27 Copyright © 2000, BEA Systems Inc.
  28. 28. EJBE-1, 2 The Bean Class Finally! The AlarmClockBean EJB Class Continued: The AlarmClockBean EJB Class Continued: public void ejbCreate(String time, String station, boolean isFm){ public void ejbCreate(String time, String station, boolean isFm){ // Generate a unique ID for this EJB. // Generate a unique ID for this EJB. // Most EJBs pass this value in. // Most EJBs pass this value in. this.id = (int)(Math.random()*100000); this.id = (int)(Math.random()*100000); this.wakeUpTime = time; this.wakeUpTime = time; this.station = station; this.station = station; this.isFm = new Boolean(isFm).toString(); this.isFm = new Boolean(isFm).toString(); }} public void ejbPostCreate() {} public void ejbPostCreate() {} public void ejbPostCreate(String time, String station, public void ejbPostCreate(String time, String station, boolean isFm) {} boolean isFm) {} Developing Enterprise Applications with BEA WebLogic Server EJBE-28 Copyright © 2000, BEA Systems Inc.
  29. 29. BMP Considerations Things you Have to do For BMP Entity EJBs: Things you Have to do For BMP Entity EJBs: 1. You must code the persistence management for the ejbCreate(…), 1. You must code the persistence management for the ejbCreate(…), ejbRemove(), ejbLoad(), and ejbStore() methods. ejbRemove(), ejbLoad(), and ejbStore() methods. 2. The ejbCreate(…) methods must return aaprimary key instance. 2. The ejbCreate(…) methods must return primary key instance. 3. The ejbFind(…) methods must be implemented. 3. The ejbFind(…) methods must be implemented. Developing Enterprise Applications with BEA WebLogic Server EJBE-29 Copyright © 2000, BEA Systems Inc.
  30. 30. BMP EJB Creation Container-Provided Classes Entity Synchronization : Client EJBHome EJBObject Context instance database create(args) ejbCreate(args) create representation in DB Return primary key instance with unique values inserted new ejbPostCreate(args) Return remote stub containing primary key instance! Developing Enterprise Applications with BEA WebLogic Server EJBE-30 Copyright © 2000, BEA Systems Inc.
  31. 31. The Alarm With BMP Developing Enterprise Applications with BEA WebLogic Server EJBE-31 Copyright © 2000, BEA Systems Inc.
  32. 32. The Alarm With BMP Developing Enterprise Applications with BEA WebLogic Server EJBE-32 Copyright © 2000, BEA Systems Inc.
  33. 33. The Alarm With BMP Developing Enterprise Applications with BEA WebLogic Server EJBE-33 Copyright © 2000, BEA Systems Inc.
  34. 34. The Alarm With BMP Developing Enterprise Applications with BEA WebLogic Server EJBE-34 Copyright © 2000, BEA Systems Inc.
  35. 35. The Alarm With BMP Developing Enterprise Applications with BEA WebLogic Server EJBE-35 Copyright © 2000, BEA Systems Inc.
  36. 36. The Alarm With BMP Developing Enterprise Applications with BEA WebLogic Server EJBE-36 Copyright © 2000, BEA Systems Inc.
  37. 37. EJBE-3 The Alarm With BMP Developing Enterprise Applications with BEA WebLogic Server EJBE-37 Copyright © 2000, BEA Systems Inc.
  38. 38. Transactions § Transactions can group multiple statements into an atomic unit to ensure integrity. § EJBs can have their transaction demarcation level set: Ø forthe entire EJB Ø per method Developing Enterprise Applications with BEA WebLogic Server EJBE-38 Copyright © 2000, BEA Systems Inc.
  39. 39. EJB Transaction Attributes § The following entities can demarcate transactions: Ø an EJB client Ø the container (automatic demarcation) Ø the EJB component itself § The EJB specification defines 6 transaction attributes: Ø TX_NOT_SUPPORTED Ø TX_SUPPORTS Ø TX_MANDATORY Ø TX-REQUIRED Ø TX_REQUIRES_NEW Ø TX_BEAN_MANAGED Developing Enterprise Applications with BEA WebLogic Server EJBE-39 Copyright © 2000, BEA Systems Inc.
  40. 40. TX_NOT_SUPPORTED § TX_NOT_SUPPORTED transactions cause client transactions to be suspended during the EJB’s execution. Scenario 1: 1-begin 2-method Container Container method EJB 3-commit Client Active Transaction Scenario 2: No Transaction Active Container method method EJB Client Developing Enterprise Applications with BEA WebLogic Server EJBE-40 Copyright © 2000, BEA Systems Inc.
  41. 41. TX_SUPPORTS § TX_SUPPORTS implies that the EJB will be included in any client-started transactions. Scenario 1: 1-begin 2-method Container Container method EJB 3-commit Client Active Transaction Scenario 2: No Transaction Active Container method method EJB Client Developing Enterprise Applications with BEA WebLogic Server EJBE-41 Copyright © 2000, BEA Systems Inc.
  42. 42. TX_MANDATORY § TX_MANDATORY transactions require the client to pass a global transaction to the EJB. Scenario 1: 1-begin 2-method Container Container method EJB 3-commit Client Active Transaction Scenario 2: No Transaction Active Container TransactionRequired method Exception EJB Client Developing Enterprise Applications with BEA WebLogic Server EJBE-42 Copyright © 2000, BEA Systems Inc.
  43. 43. TX_REQUIRED § TX_REQUIRED transactions force the EJB to be executed within a transaction started either by the client or the container. Scenario 1: 1-begin 2-method Container Container method EJB 3-commit Client Active Transaction Scenario 2: No Transaction Active 1-begin Container 2-method EJB method Client 3-commit Developing Enterprise Applications with BEA WebLogic Server EJBE-43 Copyright © 2000, BEA Systems Inc.
  44. 44. TX_REQUIRES_NEW § TX_REQUIRES_NEW transactions force the EJB to execute within a new transaction that can only be started by the container. Scenario 1 (try to avoid): 1-begin 1-begin 2-method Container Container 2-method EJB 3-commit 3-commit Client Active Transaction No Transaction Active Scenario 2: Localized Transaction 1-begin Container 2-method EJB method Client 3-commit Developing Enterprise Applications with BEA WebLogic Server EJBE-44 Copyright © 2000, BEA Systems Inc.
  45. 45. TX_BEAN_MANAGED § TX_BEAN_MANAGED transactions are demarcated within the EJB only. Scenario 1 (try to avoid): 1-begin 1-begin 2-method Container Container method EJB 2-method 3-commit 3-commit Client Active Transaction No Transaction Active Scenario 2: Localized Transaction Container 1-begin method method EJB 2-method Client 3-commit Developing Enterprise Applications with BEA WebLogic Server EJBE-45 Copyright © 2000, BEA Systems Inc.
  46. 46. The UserTransaction Interface § Use the javax.jts.UserTransaction object to demarcate transactions within a client or EJB. <<Interface>> UserTransaction begin() commit() rollback() setRollbackOnly() getStatus() setTransactionTimeout() To Obtain a New javax.jts.UserTransaction Instance From JNDI: To Obtain a New javax.jts.UserTransaction Instance From JNDI: UserTransaction trans = (UserTransaction)ctx.lookup(“javax.jts.UserTransaction”); Developing Enterprise Applications with BEA WebLogic Server EJBE-46 Copyright © 2000, BEA Systems Inc.
  47. 47. Other Transaction Issues § An EJB’s conversational state is NOT encompassed in a transaction. § A rollback() on a transaction that encompasses an EJB will not revert the EJB’s attributes! § Container and client managed transaction objects can be accessed by the context: To Obtain Change the Status of the Current Transaction Within an EJB: To Obtain Change the Status of the Current Transaction Within an EJB: contextObject.setRollbackOnly(); boolean setForRollback = contextObject.getRollbackOnly(); Developing Enterprise Applications with BEA WebLogic Server EJBE-47 Copyright © 2000, BEA Systems Inc.
  48. 48. Reverting Conversational State § You can have your EJB implement the SessionSynchronization interface. SessionSynchronization Interface Methods: SessionSynchronization Interface Methods: // Called by the application server right after the start of aanew transaction. If you // Called by the application server right after the start of new transaction. If you // store attributes that may need to be reverted, do it here. // store attributes that may need to be reverted, do it here. public void afterBegin(); public void afterBegin(); // Called immediately before the completion of the transaction. It is NOT known if // Called immediately before the completion of the transaction. It is NOT known if // is committed or rolled back at this point. // is committed or rolled back at this point. public void beforeCompletion(); public void beforeCompletion(); // Called immediately after the completion of aatransaction. If the committed // Called immediately after the completion of transaction. If the committed // variable is true, then the transaction was successful. If the committed value // variable is true, then the transaction was successful. If the committed value // is false, the transaction was rolled back. You can recover EJB state from // is false, the transaction was rolled back. You can recover EJB state from // within this method if you need to. // within this method if you need to. public void afterCompletion(boolean committed); public void afterCompletion(boolean committed); }} Developing Enterprise Applications with BEA WebLogic Server EJBE-48 Copyright © 2000, BEA Systems Inc.
  49. 49. Review § In this section we talked about : Ø container-managed and bean-managed persistence Ø the differences between session and entity EJBs Ø primary key classes Ø transactions and their demarcation levels Developing Enterprise Applications with BEA WebLogic Server EJBE-49 Copyright © 2000, BEA Systems Inc.
  50. 50. Review § In this module we talked about: Ø container and bean managed persistence Ø primary key classes Ø differences between session and entity EJBs Ø writing ejbPostCreate(…) and ejbFind(…) methods Ø writing ejbLoad() and ejbStore() methods Ø transactions Developing Enterprise Applications with BEA WebLogic Server EJBE-50 Copyright © 2000, BEA Systems Inc.

×