SlideShare una empresa de Scribd logo
1 de 50
Descargar para leer sin conexión
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Attribute and find(…) Method Mapping




        Developing Enterprise Applications with BEA WebLogic Server   EJBE-15
                          Copyright © 2000, BEA Systems Inc.
Attribute and find(…) Method Mapping




         Developing Enterprise Applications with BEA WebLogic Server   EJBE-16
                           Copyright © 2000, BEA Systems Inc.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
The Alarm With BMP




Developing Enterprise Applications with BEA WebLogic Server   EJBE-31
                  Copyright © 2000, BEA Systems Inc.
The Alarm With BMP




Developing Enterprise Applications with BEA WebLogic Server   EJBE-32
                  Copyright © 2000, BEA Systems Inc.
The Alarm With BMP




Developing Enterprise Applications with BEA WebLogic Server   EJBE-33
                  Copyright © 2000, BEA Systems Inc.
The Alarm With BMP




Developing Enterprise Applications with BEA WebLogic Server   EJBE-34
                  Copyright © 2000, BEA Systems Inc.
The Alarm With BMP




Developing Enterprise Applications with BEA WebLogic Server   EJBE-35
                  Copyright © 2000, BEA Systems Inc.
The Alarm With BMP




Developing Enterprise Applications with BEA WebLogic Server   EJBE-36
                  Copyright © 2000, BEA Systems Inc.
EJBE-3
         The Alarm With BMP




         Developing Enterprise Applications with BEA WebLogic Server   EJBE-37
                           Copyright © 2000, BEA Systems Inc.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.

Más contenido relacionado

La actualidad más candente

JEE Course - EJB
JEE Course - EJBJEE Course - EJB
JEE Course - EJBodedns
 
Enterprise Java Beans( E)
Enterprise  Java  Beans( E)Enterprise  Java  Beans( E)
Enterprise Java Beans( E)vikram singh
 
Free EJB Tutorial | VirtualNuggets
Free EJB Tutorial | VirtualNuggetsFree EJB Tutorial | VirtualNuggets
Free EJB Tutorial | VirtualNuggetsVirtual Nuggets
 
Ejb training institute in navi-mumbai
Ejb training institute in navi-mumbaiEjb training institute in navi-mumbai
Ejb training institute in navi-mumbaivibrantuser
 
The Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans TechnologyThe Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans TechnologySimon Ritter
 
N(i)2 technical architecture 2.0 (v1 1)
N(i)2 technical architecture 2.0 (v1 1)N(i)2 technical architecture 2.0 (v1 1)
N(i)2 technical architecture 2.0 (v1 1)kvz
 
Ejb3.1 for the starter
Ejb3.1 for the starterEjb3.1 for the starter
Ejb3.1 for the startershohancse
 
HowTo Build an OSGI EJB3 Server
HowTo Build an OSGI EJB3 ServerHowTo Build an OSGI EJB3 Server
HowTo Build an OSGI EJB3 Serverekkehard gentz
 
Ejb course in-mumbai
Ejb course in-mumbaiEjb course in-mumbai
Ejb course in-mumbaivibrantuser
 
Developing Enterprise Applications Using Java Technology
Developing Enterprise Applications Using Java TechnologyDeveloping Enterprise Applications Using Java Technology
Developing Enterprise Applications Using Java TechnologySimon Ritter
 
Summer training java
Summer training javaSummer training java
Summer training javaArshit Rai
 
Summer training java
Summer training javaSummer training java
Summer training javaArshit Rai
 
Session 1 Tp1
Session 1 Tp1Session 1 Tp1
Session 1 Tp1phanleson
 
Composite Applications with SOA, BPEL and Java EE
Composite  Applications with SOA, BPEL and Java EEComposite  Applications with SOA, BPEL and Java EE
Composite Applications with SOA, BPEL and Java EEDmitri Shiryaev
 
DB2 z/OS &amp; Java - What\'s New?
DB2 z/OS &amp; Java - What\'s New?DB2 z/OS &amp; Java - What\'s New?
DB2 z/OS &amp; Java - What\'s New?Laura Hood
 

La actualidad más candente (19)

JEE Course - EJB
JEE Course - EJBJEE Course - EJB
JEE Course - EJB
 
Enterprise Java Beans( E)
Enterprise  Java  Beans( E)Enterprise  Java  Beans( E)
Enterprise Java Beans( E)
 
Free EJB Tutorial | VirtualNuggets
Free EJB Tutorial | VirtualNuggetsFree EJB Tutorial | VirtualNuggets
Free EJB Tutorial | VirtualNuggets
 
Enterprise java beans
Enterprise java beansEnterprise java beans
Enterprise java beans
 
enterprise java bean
enterprise java beanenterprise java bean
enterprise java bean
 
Ejb training institute in navi-mumbai
Ejb training institute in navi-mumbaiEjb training institute in navi-mumbai
Ejb training institute in navi-mumbai
 
The Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans TechnologyThe Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans Technology
 
EJB 3.1 by Bert Ertman
EJB 3.1 by Bert ErtmanEJB 3.1 by Bert Ertman
EJB 3.1 by Bert Ertman
 
N(i)2 technical architecture 2.0 (v1 1)
N(i)2 technical architecture 2.0 (v1 1)N(i)2 technical architecture 2.0 (v1 1)
N(i)2 technical architecture 2.0 (v1 1)
 
Ejb3.1 for the starter
Ejb3.1 for the starterEjb3.1 for the starter
Ejb3.1 for the starter
 
HowTo Build an OSGI EJB3 Server
HowTo Build an OSGI EJB3 ServerHowTo Build an OSGI EJB3 Server
HowTo Build an OSGI EJB3 Server
 
Ejb course in-mumbai
Ejb course in-mumbaiEjb course in-mumbai
Ejb course in-mumbai
 
Developing Enterprise Applications Using Java Technology
Developing Enterprise Applications Using Java TechnologyDeveloping Enterprise Applications Using Java Technology
Developing Enterprise Applications Using Java Technology
 
Summer training java
Summer training javaSummer training java
Summer training java
 
Summer training java
Summer training javaSummer training java
Summer training java
 
Session 1 Tp1
Session 1 Tp1Session 1 Tp1
Session 1 Tp1
 
Composite Applications with SOA, BPEL and Java EE
Composite  Applications with SOA, BPEL and Java EEComposite  Applications with SOA, BPEL and Java EE
Composite Applications with SOA, BPEL and Java EE
 
DB2 z/OS &amp; Java - What\'s New?
DB2 z/OS &amp; Java - What\'s New?DB2 z/OS &amp; Java - What\'s New?
DB2 z/OS &amp; Java - What\'s New?
 
Corba
CorbaCorba
Corba
 

Destacado

Bea Web Logic Workshop81 Jump Start Guide
Bea Web Logic Workshop81 Jump Start GuideBea Web Logic Workshop81 Jump Start Guide
Bea Web Logic Workshop81 Jump Start Guideguest4263ad
 
WLP overview
WLP overviewWLP overview
WLP overview95wolf
 
Web logic platform 8.1
Web logic platform 8.1Web logic platform 8.1
Web logic platform 8.1williams2014
 
Oracle WebLogic: Feature Timeline from WLS9 to WLS 12c
Oracle WebLogic: Feature Timeline from WLS9 to WLS 12cOracle WebLogic: Feature Timeline from WLS9 to WLS 12c
Oracle WebLogic: Feature Timeline from WLS9 to WLS 12cfrankmunz
 
J2ee Transaction Overview
J2ee Transaction OverviewJ2ee Transaction Overview
J2ee Transaction OverviewTerry Cho
 
WebLogic Administration course outline
WebLogic Administration course outlineWebLogic Administration course outline
WebLogic Administration course outlineVybhava Technologies
 
Weblogic configuration & administration
Weblogic   configuration & administrationWeblogic   configuration & administration
Weblogic configuration & administrationMuhammad Mansoor
 

Destacado (11)

Bea Web Logic Workshop81 Jump Start Guide
Bea Web Logic Workshop81 Jump Start GuideBea Web Logic Workshop81 Jump Start Guide
Bea Web Logic Workshop81 Jump Start Guide
 
WLP overview
WLP overviewWLP overview
WLP overview
 
Capacity guide
Capacity guideCapacity guide
Capacity guide
 
Web logic platform 8.1
Web logic platform 8.1Web logic platform 8.1
Web logic platform 8.1
 
Oracle WebLogic: Feature Timeline from WLS9 to WLS 12c
Oracle WebLogic: Feature Timeline from WLS9 to WLS 12cOracle WebLogic: Feature Timeline from WLS9 to WLS 12c
Oracle WebLogic: Feature Timeline from WLS9 to WLS 12c
 
Jdbc
JdbcJdbc
Jdbc
 
WLST
WLSTWLST
WLST
 
J2ee Transaction Overview
J2ee Transaction OverviewJ2ee Transaction Overview
J2ee Transaction Overview
 
WebLogic Administration course outline
WebLogic Administration course outlineWebLogic Administration course outline
WebLogic Administration course outline
 
Weblogic configuration & administration
Weblogic   configuration & administrationWeblogic   configuration & administration
Weblogic configuration & administration
 
Blog
BlogBlog
Blog
 

Similar a Ejbe slides

Connecting ejb with mule
Connecting ejb with muleConnecting ejb with mule
Connecting ejb with muleRuman Khan
 
Java Web Programming [1/9] : Introduction to Web Application
Java Web Programming [1/9] : Introduction to Web ApplicationJava Web Programming [1/9] : Introduction to Web Application
Java Web Programming [1/9] : Introduction to Web ApplicationIMC Institute
 
ADVANCED JAVA MODULE I & II.ppt
ADVANCED JAVA MODULE I & II.pptADVANCED JAVA MODULE I & II.ppt
ADVANCED JAVA MODULE I & II.pptrani marri
 
J2ee web services(overview)
J2ee web services(overview)J2ee web services(overview)
J2ee web services(overview)Prafull Jain
 
Introduction to Java EE EJB Component
Introduction to Java EE EJB ComponentIntroduction to Java EE EJB Component
Introduction to Java EE EJB ComponentGanesh P
 
Session 5 Tp5
Session 5 Tp5Session 5 Tp5
Session 5 Tp5phanleson
 
Chemical Database Management J Chem Base And J Chem Cartridge: US UGM 2008
Chemical Database Management J Chem Base And J Chem Cartridge: US UGM 2008Chemical Database Management J Chem Base And J Chem Cartridge: US UGM 2008
Chemical Database Management J Chem Base And J Chem Cartridge: US UGM 2008ChemAxon
 
Enterprise Java Beans - EJB
Enterprise Java Beans - EJBEnterprise Java Beans - EJB
Enterprise Java Beans - EJBPeter R. Egli
 
Session 3 Tp3
Session 3 Tp3Session 3 Tp3
Session 3 Tp3phanleson
 
EMF-IncQuery 0.7 Presentation for Itemis
EMF-IncQuery 0.7 Presentation for ItemisEMF-IncQuery 0.7 Presentation for Itemis
EMF-IncQuery 0.7 Presentation for ItemisIstvan Rath
 
Ejb - september 2006
Ejb  - september 2006Ejb  - september 2006
Ejb - september 2006achraf_ing
 

Similar a Ejbe slides (20)

Ch4 ejb
Ch4 ejbCh4 ejb
Ch4 ejb
 
Unite5-EJB-2019.ppt
Unite5-EJB-2019.pptUnite5-EJB-2019.ppt
Unite5-EJB-2019.ppt
 
Virtual classroom
Virtual classroomVirtual classroom
Virtual classroom
 
Ejb
EjbEjb
Ejb
 
Connecting ejb with mule
Connecting ejb with muleConnecting ejb with mule
Connecting ejb with mule
 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
 
Java Web Programming [1/9] : Introduction to Web Application
Java Web Programming [1/9] : Introduction to Web ApplicationJava Web Programming [1/9] : Introduction to Web Application
Java Web Programming [1/9] : Introduction to Web Application
 
Ejb notes
Ejb notesEjb notes
Ejb notes
 
ADVANCED JAVA MODULE I & II.ppt
ADVANCED JAVA MODULE I & II.pptADVANCED JAVA MODULE I & II.ppt
ADVANCED JAVA MODULE I & II.ppt
 
J2ee web services(overview)
J2ee web services(overview)J2ee web services(overview)
J2ee web services(overview)
 
Introduction to Java EE EJB Component
Introduction to Java EE EJB ComponentIntroduction to Java EE EJB Component
Introduction to Java EE EJB Component
 
Introcution to EJB
Introcution to EJBIntrocution to EJB
Introcution to EJB
 
J2EE Online Training
J2EE Online TrainingJ2EE Online Training
J2EE Online Training
 
Session 5 Tp5
Session 5 Tp5Session 5 Tp5
Session 5 Tp5
 
Chemical Database Management J Chem Base And J Chem Cartridge: US UGM 2008
Chemical Database Management J Chem Base And J Chem Cartridge: US UGM 2008Chemical Database Management J Chem Base And J Chem Cartridge: US UGM 2008
Chemical Database Management J Chem Base And J Chem Cartridge: US UGM 2008
 
Enterprise Java Beans - EJB
Enterprise Java Beans - EJBEnterprise Java Beans - EJB
Enterprise Java Beans - EJB
 
Session 3 Tp3
Session 3 Tp3Session 3 Tp3
Session 3 Tp3
 
What's new in Exchange 2013?
What's new in Exchange 2013?What's new in Exchange 2013?
What's new in Exchange 2013?
 
EMF-IncQuery 0.7 Presentation for Itemis
EMF-IncQuery 0.7 Presentation for ItemisEMF-IncQuery 0.7 Presentation for Itemis
EMF-IncQuery 0.7 Presentation for Itemis
 
Ejb - september 2006
Ejb  - september 2006Ejb  - september 2006
Ejb - september 2006
 

Más de Meenakshi Chandrasekaran (12)

Evoting2003
Evoting2003Evoting2003
Evoting2003
 
Certification
CertificationCertification
Certification
 
Links todwnload
Links todwnloadLinks todwnload
Links todwnload
 
Pl sql
Pl sqlPl sql
Pl sql
 
Hibernate 1x2
Hibernate 1x2Hibernate 1x2
Hibernate 1x2
 
Workbench en
Workbench enWorkbench en
Workbench en
 
My sql installation
My sql installationMy sql installation
My sql installation
 
Work benchinstallation
Work benchinstallationWork benchinstallation
Work benchinstallation
 
Jdbc
JdbcJdbc
Jdbc
 
Tjava10a
Tjava10aTjava10a
Tjava10a
 
J2 ee
J2 eeJ2 ee
J2 ee
 
Bio_Computing
Bio_ComputingBio_Computing
Bio_Computing
 

Ejbe slides

  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Attribute and find(…) Method Mapping Developing Enterprise Applications with BEA WebLogic Server EJBE-15 Copyright © 2000, BEA Systems Inc.
  • 16. Attribute and find(…) Method Mapping Developing Enterprise Applications with BEA WebLogic Server EJBE-16 Copyright © 2000, BEA Systems Inc.
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. The Alarm With BMP Developing Enterprise Applications with BEA WebLogic Server EJBE-31 Copyright © 2000, BEA Systems Inc.
  • 32. The Alarm With BMP Developing Enterprise Applications with BEA WebLogic Server EJBE-32 Copyright © 2000, BEA Systems Inc.
  • 33. The Alarm With BMP Developing Enterprise Applications with BEA WebLogic Server EJBE-33 Copyright © 2000, BEA Systems Inc.
  • 34. The Alarm With BMP Developing Enterprise Applications with BEA WebLogic Server EJBE-34 Copyright © 2000, BEA Systems Inc.
  • 35. The Alarm With BMP Developing Enterprise Applications with BEA WebLogic Server EJBE-35 Copyright © 2000, BEA Systems Inc.
  • 36. The Alarm With BMP Developing Enterprise Applications with BEA WebLogic Server EJBE-36 Copyright © 2000, BEA Systems Inc.
  • 37. EJBE-3 The Alarm With BMP Developing Enterprise Applications with BEA WebLogic Server EJBE-37 Copyright © 2000, BEA Systems Inc.
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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.