Session Bean - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

Session Bean

Description:

Or, use synchronize feature of Java to control concurrent access. Synchronize on a method to restrict access to the method to one caller at a time. ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 8
Provided by: ucd76
Category:

less

Transcript and Presenter's Notes

Title: Session Bean


1
Session Bean
  • Private object dedicated to the single client
    that initiated its birth -- "stateful"
  • Short life span.
  • Or, may be "stateless" to support multiple
    clients simultaneously
  • Must be "thread safe"
  • Can perform database manipulations.
  • Typically, read only database operations
  • Sessions Beans do not survive a crash.
  • Anonymous, no primary key identifier
  • Example Applications
  • Catalog Bean, Employee ID Verification Bean

2
Thread Safe EJB
  • Implement EJB methods as thread safe
  • Use of local variables (temporary) are OK
  • Use of session variables are OK
  • Use of final class variables (constants) are OK
  • Use of instance attributes -- Not OK
  • Writing to a fixed data file -- Not OK
  • Or, use synchronize feature of Java to control
    concurrent access
  • Synchronize on a method to restrict access to the
    method to one caller at a time.
  • Synchronize on (this) to restrict access to the
    EJB to one caller at a time.

3
Sample Stateless Session Bean
  • package mypackage.impl
  • import javax.ejb.SessionBean
  • import javax.ejb.SessionContext
  • import java.sql.
  • import java.util.
  • public class LocationsEJBBean implements
    SessionBean
  • public void ejbCreate()
  • public void ejbActivate()
  • public void ejbPassivate()
  • public void ejbRemove()

4
getLocations method
  • public Object getLocations()
  • String driver "oracle.jdbc.driver.OracleDrive
    r"
  • String url "jdbcoraclethin_at_ouray.cudenver.
    edu1521oracle"
  • String username "jgerlach"
  • String password "xxxx"
  • Connection connection
  • Statement statement
  • try
  • Class.forName(driver)
  • System.out.println("JDBC Driver loaded
    successfully")
  • catch(ClassNotFoundException cnfe)
  • cnfe.printStackTrace() return null
  • try
  • connection DriverManager.getConnection(url,
    username,password)
  • connection.setAutoCommit(false)
  • statement connection.createStatement()

5
Sample Client
  • Hashtable env new Hashtable()
  • env.put(Context.INITIAL_CONTEXT_FACTORY,
    "com.evermind.server.rmi.RMIInitialContextFactory"
    )
  • env.put(Context.SECURITY_PRINCIPAL,
    "admin")
  • env.put(Context.SECURITY_CREDENTIALS,
    "welcome")
  • env.put(Context.PROVIDER_URL,
    "ormi//localhost23891/current-workspace-app")
  • Context ctx new InitialContext(env)
  • LocationsEJBHome locationsEJBHome
    (LocationsEJBHome)ctx.lookup("GerlachLocationsEJB"
    )
  • LocationsEJB locationsEJB
  • // Use a create() methods below to create a
    new instance
  • locationsEJB locationsEJBHome.create()
  • Object r locationsEJB.getLocations( )
  • if (r null) System.out.println("Got
    nothing back") return
  • Vector v (Vector) r
  • for (int i 0 i lt v.size() i)
  • System.out.println(v.get(i))

6
Session EJBs and Entity EJBs
ShoppingCart EJB
searchCatalog() getItem() addItemToOrder() deleteI
temFromOrder() placeOrder() setUpAccount()
Catalog EJB
Entity EJB Stateless Session EJB
Stateful Session EJB
7
Value Object
  • A serializable object that is passed between
    client and server.
  • Fine grain object, only getMethods
  • Reduces strain on server and network by running
    the object on the client
  • Examples account info, credit card info
Write a Comment
User Comments (0)
About PowerShow.com