J2EE Applications Programming - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

J2EE Applications Programming

Description:

ejbCreate Container calls this method when the client calls the create method. ... The container is responsible for transferring the attributes of the EJB to the ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 24
Provided by: ucd76
Category:

less

Transcript and Presenter's Notes

Title: J2EE Applications Programming


1
J2EE Applications Programming
  • Java
  • JDBC
  • Enterprise JavaBean
  • Remote Method Invocation
  • Application
  • Applet
  • Java Bean
  • Servlet/Java Server Page

2
J2EE Platform
3
EJB Architecture
  • Enterprise Beans
  • Session Bean
  • Entity Bean
  • Data Access Object
  • Value Object

4
Entity Bean
  • Shared across multiple clients
  • Unique primary key
  • Object wrapper to RDB
  • Lifespan that goes past a single client's session
  • Recoverable, automatically restored to state of
    last committed transaction or database operation.

5
Dept Table
  • Dept Table
  • DeptNo -- Two digit integer
  • DeptName -- String e.g. Accounting
  • DeptLocation -- String e.g. New York
  • OLD, Homespun Dept Class Methods
  • connect() -- create network connection to
    database
  • createTable() -- create dept table schema
  • insertRow(int id, String name, String location)
    -- create a new row in the dept table
  • commit() -- commits database changes
  • close() -- closes the network connection to
    database
  • displayAllRows() -- display the contents of the
    dept table

6
Entity Beans
  • Entity beans are container managed. The
    container handles data persistence by providing
    calls to
  • ejbCreate Container calls this method when the
    client calls the create method. It inserts the
    bean's data into the database.
  • ejbFindByPrimaryKey Container calls this method
    when a client calls the findByPrimaryKey method.
    It returns the primary key value so the container
    can call ejbLoad method to initialize entity
    bean.
  • The ejbLoad and ejbStore methods are called by
    the container to synchronize the bean's state
    with the underlying database. A "store and load"
    sequence is produced whenever a client invokes a
    set/get method. When the client invokes a finder
    method, load is called to initialize the bean.
  • The ejbPassivate and ejbActivate methods are
    called by the container before the container
    swaps the bean in and out of storage. These
    methods release objects (resources).
  • setEntityContext The entity context is
    dynamically updated by the container, maintaining
    current data for each client.

7
CMP vs. BMP
  • Container Managed Persistence
  • The container is responsible for transferring the
    attributes of the EJB to the relational
    database.
  • Gain Programmer Productivity, Application
    Reliability
  • Loss Machine Efficiency
  • Bean Managed Persistence
  • The programmer is responsible developing the code
    for transferring the attributes of the EJB to
    the relational database.
  • Gain Machine Efficiency
  • Loss Programmer Productivity, Application
    Reliability

8
DeptBean's Attributes
  • package GerlachPackage.impl
  • import javax.ejb.EntityBean
  • import javax.ejb.EntityContext
  • import GerlachPackage.DeptPK
  • public class DeptBean implements EntityBean
  • public EntityContext entityContext
  • public long deptno
  • public String dname
  • public String loc

9
DeptBean's Accessor Methods

public long getDeptno() return deptno
public void setDeptno(long newDeptno) deptno
newDeptno public String getDname() return
dname public void setDname(String newDname)
dname newDname public String getLoc()
return loc public void setLoc(String
newLoc) loc newLoc
10
DeptBean's EJB Methods -- CMP
  • public DeptPK ejbCreate()
  • return null
  • public void ejbPostCreate()
  • public DeptPK ejbCreate(long deptno)
  • this.deptno deptno
  • return new DeptPK(deptno)
  • public void ejbPostCreate(long deptno)
  • public void ejbActivate()
  • public void ejbLoad()
  • public void ejbPassivate()
  • public void ejbRemove()
  • public void ejbStore()
  • public void setEntityContext(EntityContext ctx)
  • this.entityContext ctx

11
Primary Key Class
  • package GerlachPackage
  • import java.io.Serializable
  • public class DeptPK implements Serializable
  • public long deptno
  • public DeptPK()
  • public DeptPK(long deptno)
  • this.deptno deptno
  • public boolean equals(Object other)
  • // Add custom equals() impl here
  • return super.equals(other)
  • public int hashCode()

12
Sample BMP
  • public String ejbFindByPrimaryKey(String
    primaryKey)
  • throws RemoteException, FinderException,
    SQLException
  • //This method locates the row in the table using
    the social security number, and the ejbLoad
    method loads the data into a Bean
  • String keynull
  • try
  • ic new InitialContext()
  • DataSource ds (DataSource)
    ic.lookup(dbName)
  • con ds.getConnection()
  • ps con.prepareStatement("SELECT socsec
    FROM BONUS WHERE socsec ? ")
  • ps.setString(1, primaryKey)
  • ResultSet rs ps.executeQuery()
  • if(rs.next())
  • key primaryKey
  • else
  • System.out.println("Find Error")
  • catch (javax.naming.NamingException ex)
    ex.printStackTrace()
  • try

13
Sample BMP
  • public void ejbLoad()
  • //Load data from database
  • try
  • ic new InitialContext()
  • DataSource ds (DataSource)
    ic.lookup(dbName)
  • con ds.getConnection()
  • ps con.prepareStatement("SELECT FROM
    BONUS WHERE SOCSEC ?")
  • ps.setString(1, this.socsec)
  • ResultSet rs ps.executeQuery()
  • if(rs.next())
  • this.bonus rs.getDouble(2)
  • else
  • System.out.println("Load Error")
  • catch (java.sql.SQLException ex)
    ex.printStackTrace()
  • catch (javax.naming.NamingException ex)
    ex.printStackTrace()
  • try
  • ps.close()
  • con.close()

14
Sample BMP
  • public void ejbStore()
  • //Store data to database after Bean created
  • try
  • DataSource ds (DataSource)ic.lookup(dbName
    )
  • con ds.getConnection()
  • ps con.prepareStatement("UPDATE BONUS SET
    BONUS ? WHERE SOCSEC ?")
  • ps.setDouble(1, bonus)
  • ps.setString(2, socsec)
  • int rowCount ps.executeUpdate()
  • catch (javax.naming.NamingException ex)
    ex.printStackTrace()
  • catch (java.sql.SQLException ex)
    ex.printStackTrace()
  • try
  • ps.close()
  • con.close()
  • catch (java.sql.SQLException ex)
    ex.printStackTrace()

15
Sample BMP
  • public void ejbRemove() throws RemoteException
  • try
  • DataSource ds (DataSource)ic.lookup(dbName
    )
  • con ds.getConnection()
  • ps con.prepareStatement("DELETE FROM
    BONUS WHERE SOCSEC ?")
  • ps.setString(1, socsec)
  • ps.executeUpdate()
  • catch (java.sql.SQLException ex)
    ex.printStackTrace()
  • catch (Exception ex) ex.printStackTrace()
  • try
  • ps.close()
  • con.close()
  • catch (java.sql.SQLException ex)
    ex.printStackTrace()

16
Sample BMP
  • public void ejbActivate()
  • //When a bean is activated, the container calls
    its ejbLoad method.
  • socsec (String)context.getPrimaryKey()
  • public void ejbPassivate()
  • //When a bean is passivated, the container calls
    its ejbStore method.
  • socsec null

17
Sample BMP
  • public void setEntityContext(javax.ejb.EntityCont
    ext ctx)
  • this.context context
  • public void unsetEntityContext()
  • context null

18
EJB Implementation Issues
  • Concurrency Control
  • Connection Pooling
  • Database Synchronization

19
Bean-Managed Persistence
  • ejbLoad is called by the container prior to all
    calls to getMethods and setMethods.
  • ejbStore is called by the container after all
    calls to getMethods and setMethods.
  • To improve efficiency
  • No caching
  • getMethods and setMethods access database
    directly using SQL
  • ejbStore and ejbLoad are no-ops
  • Caching
  • Define transactional boundaries begin/commit
  • Or, use a "modified flag" to control load and
    store methods

20
CMP Life Cycle
  • Client talks to the EJB through its container.
    The client looks up the EJB based on its given
    name. Sample chain of events of running an
    application.
  • Client creates EJB
  • setEntityContext method
  • Create method
  • Post create method
  • Client finds the EJB
  • setEntityContext method
  • Find by primary key
  • Load method
  • Client invokes getMethod
  • Load method
  • Store method
  • Client invokes setMethod
  • Load method
  • Store method

21
Entity Bean Guidelines
  • Bean requires persistent storage
  • Represents a course-grained business object
  • Shared across multiple clients
  • Stateful conversational mode
  • Represent a single logical row of database (table
    or view)
  • Get/Set methods should manipulate attributes of
    the bean
  • Each method secures and releases a database
    connection
  • Examples Purchase order, Customer

22
Assemble and Deploy EJB
  • Compile the Entity Bean
  • Assemble the J2EE Application
  • JNDI name for the enterprise bean ("Dept")
  • Creates a separate .JAR file to hold each EJB
  • Creates a deployment description for the
    application e.g. jgerlach.deploy
  • The EJB .JAR files are packaged into an .EAR file
    with the deployment descriptor
  • Deploy the JEEE Application
  • Transfer (.EAR file) to J2EE installation
    directories and Root Context of the application
    server

23
Settings for EJB Deployment
  • Oracle 9iAS Application Server Connection
  • User id admin
  • Password corbaisfun
  • URL ormi//javacr.cudenver.edu23791
  • EJB Deployment Profile
  • Application name ltfirst letter of first namegt
    ltlast namegt
  • E.g., jgerlach.ear
  • EJB Name ltfirst letter of first namegt ltlast
    namegt ltejb namegt
  • E.g., jgerlachDept
Write a Comment
User Comments (0)
About PowerShow.com