Beruflich Dokumente
Kultur Dokumente
955
Theory and Fundamentals
At the end of this module you will be able to:
9 EJ B fundamentals
9 Transaction fundamentals
9 J DBC fundamentals
9 J MS and MOM fundamentals
Module 26
Theory and Fundamentals-1
2003 BEA Systems, Inc. 956
Road Map
1. EJ B Fundamentals
Maj or EJB T ypes and T hei r Purpose
EJB Pool and Cache Management
2. Transaction fundamentals
3. J DBC fundamentals
4. J MS and MOM fundamentals
Theory and Fundamentals-2
2003 BEA Systems, Inc. 957
Enterprise JavaBeans
fEnterpri se J avaBeans (EJ B) standardizes the
development and deployment of server
components built in J ava.
fThe EJ B specification defines relationships
between:
the EJ B and its container
the container and the application server
the container and the client
Theory and Fundamentals-3
2003 BEA Systems, Inc. 958
Types of EJBs
fThe EJ B specification discusses four types of
objects:
stateless session beans
stateful session beans
entity beans
message driven beans
Theory and Fundamentals-4
2003 BEA Systems, Inc. 959
What Is a Stateless Session EJB?
fStateless session EJ Bs:
provide independent services
do not maintain state on behalf of client
are synchronous
do not survive EJ B server crashes
are maintained in memory
calls can be handled by any instance with the same
results
Stateless Session EJB
Theory and Fundamentals-5
2003 BEA Systems, Inc. 960
What Is a Stateful Session EJB?
fStateful session EJ Bs:
provide conversational interaction
store state on behalf of the client
are synchronous
do not survive EJ B server crashes
are maintained in memory
each instance is associated to a single client
Stateful Session EJB
Theory and Fundamentals-6
2003 BEA Systems, Inc. 961
Session EJB Examples
f Stateless session EJ Bs:
an EJ B that checks to see if
a stock symbol is valid
an EJ B that determines the
local billing price for a
phone call
an EJ B that calculates an
insurance quote based on a
provided customer profile
an EJ B that assembles a
standard report from several
departments
f Stateful session EJ Bs:
an EJ B that books a flight
and car rental at a travel
agents Web site
an EJ B that orders spare
parts for a car as part of an
application
an EJ B that manages the
shopping cart in a Web site
Theory and Fundamentals-7
2003 BEA Systems, Inc. 962
What Is an Entity EJB?
fEntity EJ Bs have the following behavior:
they are representati ons of persistent data
they can survive a crash
multiple clients can be using EJ Bs that represent the
same data
the EJ B manages an in-memory copy of the data in the
persistent store
Entity EJB
Theory and Fundamentals-8
2003 BEA Systems, Inc. 963
Entity EJB Examples
fSome entity EJ B examples include:
an EJ B that represents a football players career
statistics
an EJ B that represents a stocks historical prices
an EJ B that represents a genome sequence
an EJ B that represents the outline for a course
an EJ B that contains your personal profile for accessing
your favorite Web site
Theory and Fundamentals-9
2003 BEA Systems, Inc. 964
Message-Driven Beans
fMessage-driven beans (MDB):
are asynchronous stateless components
behave similarly to stateless session beans
are J MS message consumers
fClients do not interact directly with MDBs
Container
Client
Sends
JMS Destination
Message
Theory and Fundamentals-10
2003 BEA Systems, Inc. 965
Message-Driven EJB Examples
fMessage-driven EJ Bs:
an EJ B that stores logging messages
an EJ B that processes a Web sites feedback messages
sent from a Servlet
an EJ B that processes news article postings
Theory and Fundamentals-11
2003 BEA Systems, Inc. 966
JAR File Structure
fEJ B components come
packaged in a J AR file.
fThe format of the J AR
file is shown here:
fEJ Bs are configured
by modifying the XML
files.
Theory and Fundamentals-12
2003 BEA Systems, Inc. 967
EJB Pooling & Caching in WLS
fEJ B objects can be pooled:
creating many instances at deployment time
servicing many concurrent clients faster
fEJ B objects can also be cached:
the number of objects is limited to a certain ceiling,
so memory cannot be filled up
only the recently used objects stay in memory
other objects are either
saved to persistent storage, or
destroyed (if they are timed out)
f Pooling and Caching are set up by editing the file
META-INF\weblogic-ejb-jar.xml.
Theory and Fundamentals-13
2003 BEA Systems, Inc. 968
EJB Administrator Tasks with WLS
f You can set up Stateless session EJ Bs for:
pool size
f You can configure Stateful session EJ Bs:
cache size
idle timeout - if memory is short, not used objects can be disposed
of after a certain period of time
cache type - if memory is short, objects can be stored to persistent
storage (if they are not timed out); this is called passi vati on
f Entity EJ Bs are both pooled and cached, so you can set
their:
pool size
cache size and idle timeout
Theory and Fundamentals-14
2003 BEA Systems, Inc. 969
Stateless Session EJB Pool Settings
fYou can manage the stateless session EJ B pool by:
capping the number of instances
setting an initial pool size
META-INF\weblogic-ejb-jar.xml Snippet:
<!- Other Tags As Appropriate Here -->
<weblogic-enterprise-bean>
<ejb-name>HelloEJB</ejb-name>
<stateless-session-descriptor>
<pool>
<max-beans-in-free-pool>15</max-beans-in-free-pool>
<initial-beans-in-free-pool>5</initial-beans-in-free-pool>
</pool>
</stateless-session-descriptor>
<jndi-name>HelloWorld</jndi-name>
</weblogic-enterprise-bean>
10
0101
1110
Theory and Fundamentals-15
2003 BEA Systems, Inc. 970
Stateful Session EJB Cache Settings
fYou can manage the stateless session EJ B cache by:
capping the number of instances
setting a timeout period
setting the passivation strategy for the cached EJ Bs
META-INF\weblogic-ejb-jar.xml Snippet:
<!- Other Tags As Appropriate Here -->
<weblogic-enterprise-bean>
<ejb-name>HelloEJB</ejb-name>
<stateful-session-descriptor>
<stateful-session-cache>
<max-beans-in-cache>1000</max-beans-in-cache>
<idle-timeout-seconds>60</idle-timeout-seconds>
<cache-type>NRU</cache-type>
</stateful-session-cache>
</stateful-session-descriptor>
<jndi-name>HelloEJB</jndi-name>
</weblogic-enterprise-bean>
10
0101
1110
Theory and Fundamentals-16
2003 BEA Systems, Inc. 971
Entity EJB Pool & Cache Settings
f You can manage the entity EJ B:
pool by capping the number of instances and setting an
initial pool size
cache by capping the number of instances and setting a
timeout period
META-INF\weblogic-ejb-jar.xml Snippet:
<weblogic-enterprise-bean>
<ejb-name>DogEBean</ejb-name>
<entity-descriptor>
<pool>
<max-beans-in-free-pool>15</max-beans-in-free-pool>
<initial-beans-in-free-pool>5</initial-beans-in-free-pool>
</pool>
<entity-cache>
<max-beans-in-cache>100</max-beans-in-cache>
<idle-timeout-seconds>30</idle-timeout-seconds>
</entity-cache>
</entity-descriptor> example
10
0101
1110
Theory and Fundamentals-17
2003 BEA Systems, Inc. 972
Pool Size Determination
fHow many EJ Bs should be in the pool?
fFactors to consider:
number of threads set in WLS
number of concurrent clients
number of dependent backend resources (for
example, database connections)
fPool size will equally affect all servers of a
cluster.
Theory and Fundamentals-18
2003 BEA Systems, Inc. 973
Cache Size Determination
fSome criteria that affect the size of your
cache include:
the total amount of J ava heap space available in
the VM
the number of different stateful session and
entity beans deployed
the amount of memory a single EJ B consumes
while active
the number of concurrent clients
clustering requires more memory because of
replication of EJ Bs
Theory and Fundamentals-19
2003 BEA Systems, Inc. 974
Section Review
9 Major EJ B types and their purpose
9 EJ B pool and cache management
In this section we discussed:
Theory and Fundamentals-20
2003 BEA Systems, Inc. 975
Road Map
1. EJ B Fundamentals
2. Transaction fundamentals
T ransacti ons
T he ACI D Properti es of T ransacti ons
Commi t and Rollback
T he T wo-phase Commi t Protocol
3. J DBC fundamentals
4. J MS and MOM fundamentals
Theory and Fundamentals-21
2003 BEA Systems, Inc. 976
What Is a Transaction?
fA transacti on is a mechanism to handle groups of
operations as though they were one.
fEither all operations in a transaction occur or
none at all.
fOperations involved in a transaction may rely on
many different servers and databases.
Theory and Fundamentals-22
2003 BEA Systems, Inc. 977
ACID Properties of a Transaction
fA transaction is formally defined by the set of
properties known by the acronym ACID.
fThe acronym ACID stands for:
Atomic
Consistent
Isolated
Durable
Theory and Fundamentals-23
2003 BEA Systems, Inc. 978
Transfering Without Transactions
fSuccessful transfer:
fUnsuccessful transfer (accounts are left in an
inconsistent state):
$1000
+$100
$1100
$500
-$100
$400
2) Deposit: $100
1) Withdraw: $100
Transfer: $100
From: Acct 1
To: Acct 2
ATM Bank
Account 1
Account 2
$1000
$500
-$100
$400
1) Withdraw: $100
Transfer: $100
From: Acct 1
To: Acct 2
ATM Bank
Account 1
Account 2
Failed
Deposit
Theory and Fundamentals-24
2003 BEA Systems, Inc. 979
Successful Transfer with Transactions
Transaction Started by Bank
fChanges within a transaction are buffered.
fIf a transfer is successful, changes are commi tted
(made permanent).
Transfer: $100
From: Acct 1
To: Acct 2
ATM Bank
$1100
$400
Commit
Transfer
Successful
ATM Bank
Account 1
Account 2
Transaction Started by Bank $500
-$100
$400
1) Withdraw: $100
Account 1
Account 2
$1000
+$100
$1100
2) Deposit: $100
Commit
Theory and Fundamentals-25
2003 BEA Systems, Inc. 980
Transaction Started by Bank
fChanges within a transaction are buffered.
fIf a problem occurs, the transaction is rolled back
to the previous consistent state.
Transfer: $100
From: Acct 1
To: Acct 2
ATM Bank
Transaction Started by Bank
$1000
$500
-$100
$400
1) Withdraw: $100
Account 1
Account 2
Failed
Deposit
$1000
$500
Rollback
Error
message
ATM Bank
Account 1
Account 2
Rollback
Unsuccessful Transfer with Transactions
Theory and Fundamentals-26
2003 BEA Systems, Inc. 981
What Are Distributed Transactions?
fA resource, like a database, is controlled through
software called a resource manager (RM).
fA local transacti on is a transaction that deals with
a single resource manager.
fA di stri buted transacti on is a transaction that
coordinates or spans multiple resource managers.
fCoordination of multiple resource managers is
done by a transacti on manager (TM).
fA transaction is often referred to as a transacti on
context.
Theory and Fundamentals-27
2003 BEA Systems, Inc. 982
The Two-Phase Commit Protocol
fThe 2PC protocol uses two steps to commit
changes within a distributed transaction.
fPhase 1 asks all RMs to prepareto make the
changes and if any of them cannot, the transaction
is aborted.
fPhase 2 asks all RMs to actually commit and make
the changes permanent.
fA global transacti on I D(XID) is used to track all
changes associated with a distributed a
transaction.
Theory and Fundamentals-28
2003 BEA Systems, Inc. 983
XA (Extended Architecture)
fXA:
is the interface used between WLS and Resource
Managers
implements the 2PC protocol
allows programs to control RMs that are involved in
distributed transactions
Client
Transaction
Manager
RM1
RM2
XA
XA
Theory and Fundamentals-29
2003 BEA Systems, Inc. 984
fA Transacti on Manager coordinates several
resource managers.
fThe two-phase commi t (2PC) protocol is used to
coordinate the transaction.
fThe XA protocol implements 2PC.
Transaction and Resource Managers
Transaction Context started by Transaction Manager
Transaction
Manager
Resource
Manager
Resource
Manager
Resource
Manager
Database
Printer
Another
App
Application
XA
XA
XA
Theory and Fundamentals-30
2003 BEA Systems, Inc. 985
A Successful Two-Phase Commit
Application
TLOG
Transaction
Manager
Resource
Manager
Resource
Manager
Resource
Manager
Database
Printer
Another
App
Application
pre-commit
ready
Transaction
Manager
Resource
Manager
Resource
Manager
Resource
Manager
Database
Printer
Another
App
commit
committed
Phase 1
Phase 2
TLOG
Theory and Fundamentals-31
2003 BEA Systems, Inc. 986
An Unsuccessful Two-Phase Commit
Application
TLOG
Transaction
Manager
Resource
Manager
Resource
Manager
Resource
Manager
Database
Printer
Another
App
Application
pre-commit
Transaction
Manager
Resource
Manager
Resource
Manager
Resource
Manager
Database
Printer
Another
App
abort
Phase 1
Phase 2
TLOG
not
ready
ready
prepare/
abort
ready/
committed
not
ready
rolled
back
Theory and Fundamentals-32
2003 BEA Systems, Inc. 987
Java Transaction API (JTA)
fWLS uses J TA 1.0.1 to implement and manage
transactions.
fWLS J TA provides the following support:
creates unique transaction identifier (XID)
supports optional transaction name
tracks objects involved in transaction
notifies databases of transaction
orchestrates 2PC using XA
executes rollbacks
executes automatic recovery procedures when failure
manages time-outs
Theory and Fundamentals-33
2003 BEA Systems, Inc. 988
Section Review
9 What distributed transactions are
9 The XA protocol
9 Transaction and resource managers
9 The two-phase commit
protocol
9 The J ava Transaction API
In this section we discussed:
Theory and Fundamentals-34
2003 BEA Systems, Inc. 989
Road Map
1. EJ B Fundamentals
2. Transaction fundamentals
3. J DBC fundamentals
JDBC Archi tecture
Datasources and Connecti on Pools
T he T wo-phase Commi t Protocol
4. J MS and MOM fundamentals
Theory and Fundamentals-35
2003 BEA Systems, Inc. 990
What Is JDBC?
fJ DBC is an API for accessing databases in a
uniform way.
fJ DBC provides:
platform independent access to databases
location transparency
transparency to proprietary database issues
support for both two-tier and multi-tier models for
database access
Theory and Fundamentals-36
2003 BEA Systems, Inc. 991
fJ DBC Dri vers are implementation classes for
database operations.
fDrivers fall into two categories:
2-tier, where clients talk directly to a database
3-tier, where clients talk to a middle-tier (WLS) which
delegates to a database
JDBC & JDBC Drivers
Java
Application
JDBC
Driver
WLS
JDBC
Driver
Database
Database
Theory and Fundamentals-37
2003 BEA Systems, Inc. 992
JDBC Architecture
Network
Server
(WLS)
Native API
(C, C++)
ODBC
Driver
All Java
JDBC Driver
(Type 4)
JDBC-Net
Bridge
(Type 3)
JDBC-Native
Bridge
(Type 2)
JDBC-ODBC
Bridge
(Type 1)
JDBC API
Java Application
RDBMS
Theory and Fundamentals-38
2003 BEA Systems, Inc. 993
Type 1 Driver
fThe Type 1 driver:
is a J DBC-ODBC bridge & usually runs on Windows
requires ODBC driver to be installed on client machine
ODBC Driver
Client
DBMS
JDBC-ODBC Bridge
JDBC API
Java App
ODBC-supported
Native Driver
Native program
Java program
Theory and Fundamentals-39
2003 BEA Systems, Inc. 994
Type 2 Drivers
fThe Type 2 driver:
requires a native driver to be already installed on the
client machine
the driver converts J DBC calls to native API calls of the
database
JDBC-Native Bridge
Native Driver
Client
DBMS
JDBC API
Java App
Native program
Java program
Theory and Fundamentals-40
2003 BEA Systems, Inc. 995
Type 3 Drivers
Java App
JDBC API
JDBC-Net Driver
Client[b]
DBMS[b]
Network
Server
Network
Protocol
Database
Protocol[b]
DBMS[c]
Database
Protocol[c]
Client[c]
Client[a]
DBMS[a]
Database
Protocol[a]