Dibyendu Baksi, PhD - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Dibyendu Baksi, PhD

Description:

A sample application 'Java Pet Store' Demo. Illustrates blueprints guidelines ... language called EJB Query Language, or EJB QL that defines query methods--finder ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 40
Provided by: ccGa
Category:
Tags: phd | baksi | code | dibyendu | finder | pet | zip

less

Transcript and Presenter's Notes

Title: Dibyendu Baksi, PhD


1
Dibyendu Baksi, PhD Java Architect, Sun Java
Center, Sun Microsystems, Inc. 3655, North Point
Parkway, Suite 600, Alpharetta, GA
30005 Dibyendu.Baksi_at_sun.com
2
Part II Outline
  • Session and Entity Beans
  • Persistence strategies
  • J2EE Patterns
  • JDBC
  • JDO
  • JCA
  • Data Caching
  • State Tracking

3
J2EE Application Configurations
  • Stand-alone Client EJB Container RDBMS/EIS
    Resources This configuration is the first step
    in the evolutionary migration from a two-tier
    client server system.
  • Browser Web Container RDBMS/EIS Resources
    This configuration is popular in small scale web
    applications that are not supposed to serve an
    unpredictable number of users and is more
    appropriate for systems that reside within the
    intranet of an organization. This is suitable for
    applications where use of an application server
    is overkill.
  • Browser Web Container EJB Container
    RDBMS/EIS Resources This configuration is the
    recommended full-blown architecture where the
    application has to be robust and scalable with a
    multitude of users demanding the maximum
    performance out of the system.

4
EJBs Revisited
  • EJBs are generally Remote Distributed Objects
  • They work across JVMs and are portable
    distributed components
  • EJBs are generally based on RMI
  • Remote EJBs have stubs and skeletons that are
    generated for marshalling.
  • They have some standard methods such as
    ejbCreate(), ejbRemove(), ejbLoad(), ejbStore(),
    etc.
  • The bean implementation class is the one where
    all business methods are
  • Use where the application dictates robustness
    and scalability and physical deployment on a
    heterogeneous computing environment
  • Each EJB server is a separate JVM (separate
    process)
  • Web Server with a Servlet Engine serves the
    purpose of web container
  • Try Tomcat from www.apache.org (free)
  • Application Servers come with EJB Server which
    has EJB container and lots of tools that help you
    to build and deploy a J2EE application
  • Try J2EESDK1.3 from java.sun.com or jboss from
    www.jboss.org (both are free)

5
Meaning of a transaction attribute
Required Default
  • Assume Parent and Child to be EJBs
  • Assume each of them to have a business method
  • Assume that the method on Parent bean calls
    the method on the Child bean
  • Required attribute is for the Child Bean
  • If the method in the Parent bean was under a
    transaction context
  • method call in the Child bean gets the same
    context and participates in the txn.
  • Otherwise
  • container starts a new transaction context for
    the child
  • parent beans method does not take part in the
    txn.

6
Deployment Descriptor Example
7
Session Beans
  • Stateful session beans are to be used to
  • Hold business logic specific to a single client
  • Represent non-persistent state
  • Use as a Facade, single point of entry
  • Representing workflow between business objects
  • Shopping cart bean example
  • Stateless session beans are used to
  • To provide service objects
  • e.g., mailing service
  • To provide procedural view of data
  • Caller provides all the input, and gets all the
    output
  • To operate on multiple data entities at a time,
  • e.g., updating inventory for a bunch of items

8
Entity Beans
  • Use entity beans for multi-user data centric
    operations
  • Represent persistent data
  • One logical record in the database
  • e.g., account, order,
  • Providing concurrent access to multiple clients
  • Providing robust, long-lived data
  • Survives server crashes
  • Don't map database tables one-to-one to remote
    entity beans.
  • Map Business Entities
  • Two ways to use entity beans for persistence
  • BMP You code it yourself (inside ejbCreate(),
    ejbRemove(), ejbLoad() and ejbStore())
  • CMP Let the container do it for you (Define
    cmp and cmr fields in an abstract class)
    this is the preferred way

9
Entity Bean (Contd.)
10
A Major Design Issue
  • How to design persistence in J2EE ?
  • Servlets using JDBC/JDO
  • Stateless Session Bean using JDBC
  • Entity Bean with BMP
  • Entity Bean CMP
  • Some factors affecting the design decision are
  • Time to market
  • Maintainability
  • Reuse
  • Performance
  • Useful Design Tips
  • JDBC code in session bean simple, quick,
    performance, lightweight
  • JDBC code in DAO coupled to session beans domain
    object maintain, reuse, easy CMP migration,
    lightweight
  • JDBC code in DAO coupled to entity beans details
    object maintain, reuse, easier migration and
    conceptual integrity

11
Explore the J2EE Patterns
  • Use solution patterns to standard problems of
    application design learnt from experience
  • Design Patterns by Eric Gamma et al
  • Guidelines and best practices for designing
    n-tier enterprise applications with J2EE
  • Consists of
  • Java Series books
  • Designing Enterprise Applications with the Java
    2 Platform Enterprise Edition (downloadable)
  • J2EE Patterns
  • A sample application Java Pet Store Demo
  • Illustrates blueprints guidelines
  • Starting point for your application
  • http//java.sun.com/j2ee/

12
J2EE Patterns
13
Data Access Objects
  • Use data access objects for data access
  • For each EJB accessing data, write one data
    access class
  • This class contains all the portable SQL or other
    code to access data
  • Good place for Abstract Factory Pattern

14
Data Access Object Pattern
15
Data Access Object Pattern
16
Container-Managed Persistence (CMP)
  • Session beans and entity beans can have
  • Local InterfaceThe local interfaces for session
    and entity beans provide support for light weight
    access from enterprise beans that are local
    clients. That is, the bean uses a local interface
    if the bean wants to provide tight coupling with
    its clients with pass-by-reference semantics. The
    local interface is a standard Java interface that
    does not inherit from RMI.
  • Remote interfaceA bean that needs to have remote
    capabilities--the bean inherits from RMI and
    interacts with distributed clients--uses a remote
    interface. A bean can have both a local and a
    remote interface--that is, it can provide both a
    local and a remote client view.
  • Multiple entity beans may have container-managed
    relationships (CMR ) among themselves. The
    automatic management of relationships is taken
    care of by the container.
  • It uses a local model that can be more tightly
    coupled than the remote model to avoid the
    performance overhead of the remote model.
  • Instance variables can be designated as
  • Container-managed persistent fields (cmp fields)
  • Container-managed relationship fields (cmr
    fields)
  • Implementation classes for entity beans that use
    CMP are abstract classes.
  • These cmp and cmr fields are defined through the
    deployment descriptor.

17
CMP (Contd.)
  • The EJB container maintains the referential
    integrity of the entity bean relationships.
  • A bean typically uses its remote interface to
    expose its methods across the network tier. A
    bean uses the local interface to expose its
    methods to other beans that reside within the
    same container. It is thus possible to directly
    access a bean through its local interface without
    the overhead of a remote method call.
  • The bean uses the local interface to maintain its
    references to other beans. For example, entity
    beans use local interfaces to maintain
    relationships to other entity beans.
  • The EJB 2.0 specification also introduces a query
    language called EJB Query Language, or EJB QL
    that defines query methods--finder and select
    methods--for entity beans with container-managed
    persistence and it is intended to be portable
    across EJB containers.
  • EJB QL is based on SQL92 although is enhanced to
    allow querying or navigation over entity bean
    relationships.
  • The query is specified in the deployment
    descriptor. The container then generates the QL
    implementation for the query.

18
An Example
19
CMP Abstract Schema
20
CMP Summarized
21
Database Access
  • Do it primarily from the EJB tier
  • Do it directly from web-tier, if the following
    conditions are met
  • Access to the data is read-only and
    non-transactional
  • The application can tolerate stale data
  • High performance is needed

22
JDBC Basics
  • Java Database Connectivity API (Application
    Programmers Interface )
  • Java specific PI to Databases
  • ANSI SQL-92 Standard
  • X/OPEN CLI for databases
  • Two levels in JDBC
  • JDBC is overall API for connecting to databases
  • Low-level JDBC Driver developers use JDBC to
    make JDBC drivers
  • High-level Java developers use JDBC to call
    JDBC driver to access databases
  • Its contained in a package called java.sql
  • Its a series of inter-connected classes
    interfaces
  • One driver included in Java Sun JDK
  • ODBC driver lets you access local data
    sources via ODBC in Java
  • Must get JDBC driver for your database

23
JDBC Architecture
24
JDBC Drivers
Type 1
Type 2
Type 4
Type 3
  • JDBC-ODBC Bridge should be considered only a
    transitional solution.
  • Consider type 2 drivers for small scale intranet
    applications.
  • Use type 3 or type 4 drivers for
    Internet-related heavy-duty applications.

25
Example JDBC Code
  • public class CreateSuppliers
  • public static void main(String args)
  • String url "jdbcodbcmyDataSource"
  • Connection con
  • String createString
  • createString "create table SUPPLIERS "
  • "(SUP_ID int, "
    "SUP_NAME varchar(40), "
  • "STREET varchar(40), "
  • "CITY varchar(20), "
  • "STATE char(2), ZIP char(5))"
  • Statement stmt
  • try
  • Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
  • catch(java.lang.ClassNotFoundException e)
    // do something
  • try
  • con DriverManager.getConnection(url,
    "dibyendu", "j_bream")
  • stmt con.createStatement()
  • stmt.executeUpdate(createString)

26
New JDBC Features
  • JDBC 2.0 has
  • Scrollable resultsets
  • Updatable resultsets
  • Batch updates
  • JDBC 3.0 has
  • Transaction savepoints
  • Auto-generated keys

27
ODMG 3.0 and Java Binding
  • Object Data Management Group founded 1991 by
    OODBMS vendors
  • ODMG 1.0 specification released 1993
  • ODMG 3.0 specification released 2000
  • Java Language Binding implemented by several
    vendors
  • Tight integration with programming language No
    impedance mismatch
  • Persistence by Reachability
  • Object model Basic types, collections, arrays,
    Inheritance, encapsulation
  • Java classes define database layout No DDL
    needed

28
JDO Spec and Objectives
  • Provides a Java language- centric view to
    application developers
  • Transparent persistence interface for application
    servers and data stores
  • JDO Java Community Process SM initiative JSR-
    00012
  • Expert group started August 1999
  • Participant Review Draft released April 2000
  • Public Draft released now
  • Suitable for embedded devices to enterprise
    information systems
  • Persistence by Reachability
  • Transaction demarcation
  • Support for Enterprise JavaBeans architecture
    and connector architecture
  • Basic types boolean, byte, short, int, long,
    char, float, double
  • Persistence- capable classes
  • User- defined classes
  • System- defined classes (String, Date, Vector,
    ... )
  • Non- persistence- capable classes Classes that
    use native calls
  • Socket, Exception, Thread

29
Major JDO interfaces
  • Transaction
  • Delimit transaction boundaries
  • Optional interface in application
    servers
  • Modeled after OMG userTransaction
  • JDOHelper
  • PersistenceCapable
  • Any user domain class
  • No required methods for persistence
  • PersistenceManager
  • Main persistence-aware application interface
  • Life cycle of persistence-capable instances
  • Query factory
  • Transaction factory
  • PersistenceManagerFactory
  • Can use JNDI to look up
  • Application server resource
  • Uses URL to identify data source
  • Local, remote resources
  • Supports user name, password
  • Query
  • One of two bootstrap techniques
  • getObjectById
  • Modeled after Java boolean expression
  • query.setFilter("salary 30000")
  • Filter Collections, Extents (subclasses)
  • Navigational expressions
  • Parameter substitution

30
JDO and POET
31
JDO and App Server
  • JDO instead of CMP of Entity Beans
  • Persistence helper classes for Session Beans
  • Delegate classes for BMP Entity Beans
  • Delegate classes for CMP Entity Beans

32
Java Connector Architecture
  • JDBC JCA and RDBMS EIS
  • Resource Adapter (more like a JDBC driver)
    implements JCA spec
  • Resource Adapter Archive (RAR) file gets
    deployed in the App server

33
How to Cache Data?
  • Approaches
  • JSP helper beans cache business data
  • Long lived user specific beans in session scope
  • Short lived beans in request scope
  • Servlet generates results, JSP displays results
  • Enterprise beans provide coarse-grained get/set
    methods to access the business data (Value
    Objects)
  • JSP helper beans update their business data as
    needed

34
Caching Data at the Web-Tier
  • Business data is often needed at the web-tier
  • Problems with caching
  • Maintaining data consistency
  • Extra complexity due to caching
  • When not to cache data in the web-tier
  • When web-server/app-server are co-located
  • Performance is not a big concern

35
Session State Management
  • Option I Store state information in cookies
  • HttpServletResponse.addCookie()
  • HttpServletRequest.getCookies()
  • Pros
  • Scalable
  • Cons
  • Low-level difficult-to-use API
  • What if cookies are turned off
  • Inefficient (too much data transfer)
  • security risks in transit, at Client
  • Option II Use User-Session Tracking
  • HttpSession.getAttribute()/setAttribute()
  • Pros
  • Can work with cookies disabled
  • Minimal data exchange with browser
  • Efficient local method calls at web server
  • Cons
  • Difficult to use API (get/set based)

36
Session State Management(Contd.)
  • Option III Use a stateful session bean
  • Pros
  • Easiest to use (Can use O-O approach)
  • Scalable
  • Cons
  • Less efficient
  • But optimized local calls
  • Option IV Store client id in a cookie and store
    all the information in the
  • database
  • Better to avoid except for one time access like
    reading previous session activity at login time
  • Pros
  • Scalable
  • Limited by database scalability
  • Cons
  • Database access on each request
  • Difficult to use
  • More porting issues

37
Where to go from here ?
  • But I want more detailed information.
  • I want to see code
  • I want someone to teach me this stuff

38
Explore J2EE
  • Everything is available off
  • http//java.sun.com/j2ee
  • Blueprints
  • A Java Series book Designing Enterprise
    Applications with the Java 2 Platform
    Enterprise Edition (downloadable)
  • A sample applicationJava Pet Store Demo
  • http//java.sun.com/j2ee/blueprints
  • Simplified J2EE guide available at
  • http//java.sun.com/j2ee/white/j2ee_guide.pdf
  • Useful Enterprise Java URLs
  • www.theserverside.com
  • www.onjava.com
  • www.javaworld.com

39
Thank You
Write a Comment
User Comments (0)
About PowerShow.com