Вы находитесь на странице: 1из 16

Deployment

April 27, 2008 at 2:21 pm (EJB, Unit 3) (EJB)

Deployment

Permalink Leave a Comment

EJB Clients
April 27, 2008 at 2:19 pm (EJB, Unit 3) (EJB)

EJB Clients

Permalink Leave a Comment

Deployment
April 22, 2008 at 3:52 am (EJB, Unit 3)

Deployment

1. Must include the names of the home interface, remote interface, and implementation
class.
2. Set the isolation level and transaction attribute for the bean.
3. Deployment Descriptor must be of type javax.ejb.deloyment.EntityDescriptor
4. Include the name of the primary key class.
5. Provide the list of container-managed fields
6. Must specify whether the bean is reentrant or not.
7. Provide a description for the finder methods, so that the deployer can use the container to
generate them.

• BEA Weblogic uses findByCustID(), custom finder method, as follows:

“findByCustID(int cusidnum)” “(=custid $custidnum)”

• This method takes one integer argument called custidnum.


• The expression “(=custid $custidnum)” is a prefix-notation expression that indicates that
the method should return all instances where the database column that maps to the field
custid matches the argument of the finder method exactly.

Permalink Leave a Comment


Entity Bean Life Cycle
April 22, 2008 at 3:49 am (EJB, Unit 3)

• An entity bean can have only three states:


o Non-existence
o Pooled
o Ready
• Non-existence
o In this state, the bean doesn’t exist.
o A bean leaves this state and enters the polled state when it is added into the pool
by the container, at which point newInstance() and setEntityContext() are
invoked on it.
o It enters the state of non existence when the container removes it from the pool, at
which point unsetEntityContext() and finalize() are called on it.
• Pooled state
o In this state, entity beans have no identity. So all are identical.
o When an ejbFind() method is invoked, the container uses an EJB instance from
the pooled state to handle the request.
o A bean transitions from the poled state to the ready state when one of two things
happen:
 The container selects the instance to handle an ejbCreate() request, at
which point ejbCreate() and ejbPostCreate() are invoked on the object.
 The container selects the instance to be activated, at which point
ejbActivate() is invoked on the instance.
o The ejbPostCreate() method takes the same arguments as the ejbCreate() method.
It is invoked only after ejbCreate() has reached completion successfully.
o ejbPostCreate() coded to perform any special processing needed after the beanis
created, but before it becomes available to the client.
o A bean transitions from the ready state to the pooled state when one of two things
happens:
 The container selects the instance for passivation at which point
ejbPassivate() is called on the object.
 The client invokes a remove() method on the instance, at which point the
instance’s ejbRemove() method is invoked.
o When a bean returns to pooled state, it once again loses its identity and becomes
one of several available instances in the pool.
• Ready State:
o In this state, beans accept invocations of its business methods. It can also process
invocations of its ejbLoad() and ejbStore() methods.
o The container keeps the state of the bean with its representation in the database.
o ejbLoad() reads data from the database and uses it to popular instance variables,
and ejbStore() writes data from the instance variables to the database.
o When a bean is activated, the container automatically invokes its ejbLoad()
method.
o Similarly, before the bean becomes passivated, its ejbStore() method is invoked.
o So, no logic needed in the ejbActivate() and ejbPassivate() to handle state
synchronization between the objectand the database.
• Reentrant Instances
o It is possible, in narrow circumstances, to make an entity EJB reentrant.
o This situation occurs when the entity bean uses another entity bean as a client, and
the remote entity bean must perform a callback parent bean.

Key points:

• All entity beans must provide a findByPrimaryKey() method in their home interface.
Clients use this finder method to locate existing entity beans based on their primary key.
• The implementation details of the finder methods also vary depending on whether the
bean has bean-managed or container-managed persistence.
• If the bean has bean-managed persistence the developer must implement a finder method
that is declared in the home interface.
• If the bean has container-managed persistence, the container will provide
implementations of the finder methods; developers need not to implement them.
• The container can easily generate an implementation of the findByPrimaryKey() method,
because it know exactly what it needs to do and receives the primary key class.
• getEJBObject() returns a reference to the Entity bean’s EJBObject.
• getPrimaryKey() returns an object that contains the primary key for this Entity Object.
• In the client side, each method invocation must perform the following tasks
o Serialize all arguments on the client side.
o Transmit the arguments to the server
o Deserialize the arguments on the server
o Invoke the remote method
o Serialize the return value on the server
o Transmit the return value to the client
o Deserialize the return value on the client side.

Permalink Leave a Comment

Primary Keys
April 22, 2008 at 3:46 am (EJB, Unit 3)

Primary Keys

• Each entity bean instance has an associated primary key.


• Logically, a primary key is a value or combination of values that allows you to uniquely
specify a row of data.
• In EJB, the primary key is represented by a Java class that contains whatever unique data
are necessary to lookup a particular entity EJBs.

Example:

pubic class CustomerPK implements java.io.Serializable

public int custid;

• According to the EJB specification, a primary key class must implement the
java.io.Serializable interface.
• This class simply carries the value for the primary key from the client to the server.
• The server inspects the class and uses the values it contains to perform a database lookup
for the object of the requested primary key.
• If two entity beans have the same home interface and the same primary key, they are
considered to be identical.
Permalink Leave a Comment

BMP and CMP


April 22, 2008 at 3:44 am (EJB, Unit 3)

Bean Managed and Container-Managed Persistence

• There are two types of entity beans:


o Entity beans with bean-managed persistence
o Entity beans with container-managed persistence
• An entity bean with bean-managed persistence contains code that updates the underlying
database.
• An entity bean with container-managed persistence contains no such code; instead, it
relies on the container to update the database as necessary.
• This is simpler to implement but require a high degree of functionality from their
container.
• Beans with bean-managed persistence are slightly more complex to implement, but do
not require extra support from the container.

Permalink Leave a Comment

When to use Entity Beans


April 22, 2008 at 3:38 am (EJB, Unit 3)

Entity Beans

When to use Entity Beans:


• To understand entity beans, a brief recap of session beans is helpful. Session beans
are:
o Intended to be used by a single client.
o Relatively short-lived – they are intended to last for the duration of the
client’s session with the server.
o Not able to survive a server crash
o Capable of interacting with shared data in a database.
• Entity beans by contrast,
o Can be used concurrently by several clients
o Are relatively long-lived – they are intended to exist beyond the lifetime of
any single client
o Will survive a server crash.
o Directly represent data in a database.
• Concurrent use by Several clients
o More than one client will have access to the bean concurrently.
o Although the clients all think that they’re accessing the bean freely, in reality
this interaction is interposed by the container so as to guarantee the integrity
of the database.
o A container may interpose on client interactions with an entity bean in two
ways to guarantee database integrity.
 The guarantee can directly interpose, queuing client request and
allowing only one request at a time to be executed.
 The container can create an instance of the bean for each client that
wishes to use it, and then allow the underlying database to deal with
synchronization issues.
• Long lifetime:
o Entity beans represent data in an underlying database. Therefore, their
lifetime matches the lifetime of the underlying data itself.
o Entity EJBs may continue to exist for days, months or even years.
o No upper limit exists on the lifetime of entity EJBs and they can be removed
in only two ways:
 A client explicitly invokes the remove() method on the bean’s remote
interface or home interface.
 Someone deletes the bean’s data from the underlying database.
o Another interesting fact about an entity bean’s lifetime is that an Entity bean
does not necessarily need to provide a create() method, a client will bet be
able to create new entity EJB via the bean’s home interface.
o Nevertheless, one can create a new entity EJB by directly inserting a row of
data into its underlying database.
o Even without a create() method, a client can still obtain a reference to an
existing Entity EJB by using one of its finder methods.
• Survival of Server Crashes:
o The session bean could be irrevocably destroyed in three ways:
 The client connection to the bean can timeout
 The server can be restarted
 The server can crash
o None of these three problem applies to entity beans:
 There is no timeout period associated with entity beans.
 Entity beans are guaranteed to survive an orderly server restart.
 Entity beans are guaranteed to survive a server crash.
• Direct representation of data in an underlying database:
o Typically an entity bean maps directly to a row in an underlying database.
o For example, a table called CUSTOMERS with the following columns:
 An integer called CUST_ID that represents a unique ID for each
customer.
 A string called CUST_NAME that contains the customer’s name.
 A string called CUST_ADDR that contains the customer’s address.
o Now an entity bean called “Customer” can be created that contains three
instance variables:
 Cust_id
 Cust_name
 Cust_addr
o These variables map directly to the columns in the underlying CUSTOMERS
table.
o A client application can then interact with the customer EJBs to modify these
fields, update the underlying database, or perform any other business logic
necessary on the customer object.

Permalink Leave a Comment

Transaction Isolation Levels


April 22, 2008 at 3:32 am (EJB, Unit 3)

Transaction Isolation Levels:


• This allows to specify a bean how much or how little of other transactions can be
viewed during the course of its own transaction execution.
• An inverse relationship exists between higher levels of isolation and the level of
concurrency. That is, an application that requires a high degree of isolation must
maintain more locks for a longer period of time than an application for which a
lower degree of isolation is acceptable.
• Two programs attempting to simultaneously access a database can have essentially
three types of problem interactions.
o Transaction A modifies a row; transaction B reads it; transaction A does a
rollback and the row disappears. This situation is called “dirty read”.
o Transaction A reads a row; transaction B modifies the row; transaction A
reads the row again and sees a different value. This case is called a “non-
repeatable read”.
o Transaction A selects a set of rows that satisfy a given search condition;
Transaction B inserts rows that satisfy the same condition. If transaction A
executes the same query again, it will receive additional data. This situation
is called a “phantom read”.
• Four levels of transaction isolations are possible:
o TRANSACTION_READ_UNCOMMITTED – Permits dirty reads, non-
repeatable read and phantom reads.
o TRANSACTION_READ_COMMITTED – Permits only non-repeatable
reads and phantom reads.
o TRANSACTION_REPEATABLE_READ – Permits only phantom reads.
o TRANSACTION_SERIALIZABLE – does not permit any of these situations
to occur.

Permalink Leave a Comment


Transactions and EJB
April 22, 2008 at 3:29 am (EJB, Unit 3)

Transactions and EJB

• Table shows the various transaction specifiers, illustrating which transaction is in effect
in various situations.
• The table includes two rows for each type of transaction support.
• The first row indicates what occurs if the client calls the bean without a transaction
context; the second row indicates what happens if the client calls the bean with a
transaction in effect.

Transaction Attribute Client’s TX Bean’s TX


None None
TX_NOT_SUPPORTED
T1 None
None T2
TX_REQUIRED
T1 T1
None None
TX_SUPPORTS
T1 T1
None T2
TX_REQUIRES_NEW
T1 T2
None Error
TX_MANDATORY
T1 T1

• TX_NOT_SUPPORTED means that the bean should never be called within a transaction
o If a client calls the bean without a transaction in effect, the bean method is
invoked.
o If the client calls the bean while the client has a transaction in effect, the container
suspends the client’s transaction temporarily, invokes the bean method, and after
the method has finished executing resumes the client’s transaction.
• TX_REQUIRED means that the bean’s methods must always be invoked within a
transaction.
o If the client does not currently have a transaction in effect, the container creates a
new transaction, invokes the method within the transaction, and then terminates
the transaction prior to returning to the client.
o If the client does have a transaction in effect, it serves as the transaction context
for the bean’s method invocation.
• TX_SUPPORTS means that the bean can be invoked in a client supplied transaction
context.
o If the client does not supply a transaction context, it invokes the bean’s method
with no transaction context.
o If the client supplies a transaction context, this context is used as the transaction
context for the bean’s method.
• TX_REQUIRES_NEW means that the bean’s method must always be invoked in a new
transaction context.
o If the client has not supplied a transaction context, the EJB container creates a
new one.
o If the client has supplied a transaction context, the EJB container creates a new
one anyway, suspending the client’s transaction context until the bean’s
transaction has been completed.
• TX_MANDATORY means that the client is required to supply a transaction context
o If the client attempts to invoke a TX_MANDATORY bean without a transaction
context, the container will throw a TransactionRequiredException to the client.
o If the client supplies a transaction context, the bean will use this context to invoke
the method.

• TX_BEAN_MANAGED is a bit of a special case:

Client’s TX Instance’s Current TX TX Used by method


None None None
T1 None None
None T3 T3
T1 T3 T3

• In the first case, neither the client nor the bean has a transaction context open. The
bean’s method is therefore invoked without a transaction context.
• In the second case, the client has a transaction context open, but the bean does
not. The client’s transaction is suspended, and the bean is invoked with no
transaction context.
• In the third case, the client has no transaction context open, but the bean does. The
bean’s method is invoked in its own transaction context.
• In the fourth case, the client has a transaction context open, as does the bean. The
client’s transaction is suspended, the method is invoked in the bean’s transaction
context, and the client’s transaction context is resumed after the method
completes.

• TX_BEAN_MANAGED transaction also differs depending on whether the session bean


is either stateful or stateless:
o A stateful bean can maintain an open transaction across method invocations. That
is, it can open a transaction in one method, respond to several method invocations
within that transaction, and then close the transaction in yet another method. In
this situation, the container suspends the bean’s transaction context after each
invoked method completes, then resumes it when the next method is invoked.
o A stateless bean cannot maintain an open transaction across method invocations.
These can potentially be invoked by many different clients during the course of
their life, so it would be erroneous to attempt to maintain an open transaction
context across different method invocations. If a stateless bean uses transactions,
each transaction must either commit or rollback before the method returns.

Permalink Leave a Comment

Session Bean Life Cycles


April 22, 2008 at 3:26 am (EJB, Unit 3) (EJB)

Session Bean Life Cycle


• Stateless session bean has only two states:
o Non-existence
o Method-ready state

• newInstance() – Allocates space for the bean and brings it into existence state.
• setSessionContext() – Gives the bean a Session Context.
• ejbCreate() – Allows the bean to perform any necessary initializations
• SessionContext points to a dynamic object that the container updates each time a stateless
session bean’s methods are invoked.
• So, even though the bean is stateless, the access to the useful method of SessionContext
interface is retained.
• A stateless session bean can have only one ejbCreate() method, and it must take no
arguments.
• This initializes the instance’s internal state variables. Because a stateless session bean has
no specific state variables, it does not require any arguments.
• Even though the client invokes create() and remove(), these methods do not necessarily
result in an actual instance being created or removed in the container.
• The container will maintain a pool of stateless beans. This pool will be large enough to
ensure that any client that needs to invoke a method receives an instance.
• The system administrator will normally set the number of stateless session beans in the
instance pool, adjusting this number as necessary to provide the required level of service.
• A container might also manage the pool of instances itself, creating and removing
instances as necessary to serve the current client load.

• newInstance() – Allocates space for the bean and brings it – into existence.
• setSessionContext() – Gives the bean a SessionContext
• ejbCreate() – Allows the bean to perform any necessary initializations.
• Because stateless beans have no state, they can be reused by many clients in the course of
their existence.
• A stateful session bean can implement any of several ejbCreate() methods that take
different sets of arguments.
• Only one of the available ejbCreate() methods will be invoked at creation time, however.
• Once the ejbCreate() method executes the bean enters the “method ready” state.
• It remains in this state as long as only non-transactional methods are invoked on it.
• Three transitions out of this state are possible.
o The bean can enter a transaction
o It can be passivated
o It can be removed.
• The bean enters a transaction if one of its transactional methods is invoked. (should
contain transaction declaratory TX_BEAN_MANAGED, TX_REQUIRED,
TX_REQUIRES_NEW, TX_MANDATORY, for TX_SUPPORTS caller should have a
transaction)
• If the bean implements the SessionSynchronization interface, the container invokes its
afterBegin() method to notify it that it is entering a transaction.
• The bean then moves to the “method-ready in transaction” state, where it remains until
the transaction concludes.
• If the transaction was created specifically for the method invocation, the bean will exit
the “method-ready in transaction” state immediately.
• For the TX_BEAN_MANAGED, the bean remains in this state until a commit or
rollback occurs.
• If a commit occurs while the bean is in the “method-ready in transaction” state, the
beforeCompletion() method of the SessionSynchronization interface is invoked with a
boolean value of true as the argument.
• This sequence of invocations allows the bean to perform any database updates, before the
transaction commits, and provides it with definitive notification that the transaction has
successfully committed. After these methods finish the bean returns to the “method-
ready” state.
• On the other hand, if a rollback occurs while the bean is in the “method-ready in
transaction” state, beforeCompletion() is not invoked; rather afterCompletion is invoked
with a boolean argument of false.
• If the bean is in the “method-ready in transaction” state, don’t attempt to call the bean in
a different transaction context, this will lead to an error state.

• When the bean is passivated, the container invokes its ejbPassivate() method and then
writes it out to some type of persistent storage; it also removes the bean from memory.
• A bean in the “method-ready in transaction” state cannot be passivated; only beans in the
“method-ready” state are subject to passivation.
• If the bean have opened any database or socket connections from the ejbCreate() method,
the code should included in the ejbPassivate() method to close these connections.
• A bean cannot maintain open database or socket connections when it is passivated.
• These connections are reestablished again when the beans are activated.
• The final transaction is out of the “method-ready” state is caused by the removal of the
bean.
• Unlike stateless session beans, stateful session beans are actually removed by the
container in response to a client’s call to the remove() method on the bean’s remote
interface.
• Before removing the bean, the container calls the beans ejbRemove method, which gives
it one last opportunity to write out database updates before it passes out of existence. This
will close all the opened database and socket connections.
• After ejbRemove() has executed, the bean is destroyed and cannot be resurrected

Session Beans Constraints


April 22, 2008 at 3:13 am (EJB, Unit 3)

Session Beans Constraints


• Session beans disappear from the server, if
o The session bean’s timeout value expires
o The server crashes and is restarted
o The server is shutdown and restarted
• If any of these event occurs, state information of the bean is lost.

Solutions:

1. Session timeout value is configurable so the timeout value can be increased.


2. Server crashes and restarts are fairly rare events.
1. The user can be notified that non-recoverable error has occurred and ask the
user to try again later.
2. A copy of the state of the bean can be cached at the client side and a new bean
can be recreated in the event of original bean dies.
3. Use a database on the server side to hold the state of the client’s conversation. If
the session bean dies during the conversation, the client can reconnect and pass
in a unique customer identifier to query the database to retrieve whatever state
information was saved prior to the failure of the session beans.

• A session bean’s state is not transactional. If the internal state of the bean is
modified, the rollback will not affect the session bean’s internal state.

Solution:

• The session bean’s state should be reset manually to whatever it was before the
transaction began.

• A stateless session bean cannot implement the SessionSynchronization interface.


Because stateless session beans lack a conversational state and cannot hold a
transaction across method invocations.
• The final constraint on session beans is that they are not reentrant. If one thread is
currently using a session bean, the container will throw a java.rmi.RemoteException
in response to any other requests to connect to the session bean.

Permalink Leave a Comment

Constraints on Beans
April 22, 2008 at 3:08 am (EJB, Unit 3)
Common Constraints for all beans:

• EJB cannot start new threads or use thread synchronization primitives.


o Container is responsible for creating and managing new bean instances
o EJB beans are not allowed to spawn threads themselves, but the container
can run multiple beans concurrently.
o This prevention is needed not to interfere the container’s management of its
beans
• EJB cannot directly access the underlying transaction manager
o Because the container has the responsibility for managing transactions.
o If bean declares its transaction management as TX_BEAN_MANAGED, is
allowed to use a javax.jts.UserTransaction to manage the state of its own
transaction.
• The bean cannot use the JDBC commit and rollback primitives.
o This constraints exists because the container is responsible for issuing the
commit and rollback instructions for transaction-enabled beans.
o A bean with a transaction attribute of TX_NOT_SUPPORTED, however,
can use JDBC commit or rollback, because no external transaction manager
exists.
• Beans are not allowed to change their java.security.Identity at runtime.
o The identity is typically set up at deployment time by the system
administrator
o Runtime changes are disallowed to prevent malicious beans from acquiring
unintended privileges.
• EJB cannot have read/write static variables. But “static final” variables are allowed.

Permalink Leave a Comment

When to Use Session Beans?


April 22, 2008 at 3:04 am (EJB, Unit 3)

• The EJB 1.0 specification requires support for session beans.


• EJB 2.0 specification requires support for Entity Beans
• Few EJB containers will not implement support for entity beans.

When to use Session Beans?


• Session beans are intended to allow the application author to easily implement portions of
application code in middleware and to simply access to this code.
• Entity beans are essentially a transactional object persistence mechanism; when you need
to save the object’s state to persistent storage.
Permalink Leave a Comment

EJB Development Considerations and Steps


March 5, 2008 at 3:48 pm (EJB, Unit 3) (EJB)

The steps to developing EJBs can be partitioned along two main lines: server-side and client-side
development. Although server-side development of distributed communications servers, such as CORBA and
RMI, takes on a somewhat-complicated nature at times, server-side development of EJBs is simplified because
much of the communications, state management, resource allocation, and thread management infrastructure
coding is provided by the container. These are the main steps employed for server-side EJB development:

<!--[if !supportLists]-->1. <!--[endif]-->Implement EJB standard interfaces—Any interfaces required


by the standard EJB component model to enable container-based management of the EJB should be
implemented.

<!--[if !supportLists]-->2. <!--[endif]-->Implement EJB business-specific interfaces—Any business-


specific interfaces provided by your EJB and any supporting helper and utility classes should be
implemented.

<!--[if !supportLists]-->3. <!--[endif]-->Create client remote interfaces—The remote interface for


your EJB that defines all business-specific interfaces to the EJB should be created.

<!--[if !supportLists]-->4. <!--[endif]-->Create client home interfaces—The home interface for your
EJB that defines the application-specific methods for creating your EJB, as well as application-specific
methods for finding your EJB (if it is an entity bean), should be created.

<!--[if !supportLists]-->5. <!--[endif]-->Compile EJB code—The EJB implementation, home interface,


and remote interface should be compiled.

<!--[if !supportLists]-->6. <!--[endif]-->Configure module deployment descriptors—The standard


EJB deployment descriptor should be configured to define the specific structural characteristics and
dependencies of your EJB. Any deployment descriptors needed by your container/server provider should
also be configured.

<!--[if !supportLists]-->7. <!--[endif]-->Package EJB into an EJB JAR module—The standard EJB
deployment descriptor, any vendor-specific deployment descriptors, and one or more of your compiled EJB
class files should be packaged into an EJB JAR module.

<!--[if !supportLists]-->8. <!--[endif]-->Configure application deployment descriptor—A standard


J2EE deployment descriptor should be configured for a cohesive collection of J2EE modules.

<!--[if !supportLists]-->9. <!--[endif]-->Package EJB Modules into a J2EE EAR Application—The


standard J2EE deployment descriptor and one or more EJB JAR files should be packaged into a J2EE
EAR application.

<!--[if !supportLists]-->10. <!--[endif]-->Deploy the J2EE application—The J2EE EAR application


should be deployed to a J2EE-compliant application container/server environment.

On the client side, clients must simply be designed to utilize the proper EJB client interfaces and
libraries. EJB client development proceeds along the following lines:
<!--[if !supportLists]-->1. <!--[endif]-->Standard client library verification—The proper EJB client
libraries must be established.

<!--[if !supportLists]-->2. <!--[endif]-->EJB client interface generation—The properly compiled


interfaces and stubs specific to a particular EJB must also be provided to an EJB client.

<!--[if !supportLists]-->3. <!--[endif]-->Client implementation—The EJB client may be implemented


to utilize any interfaces as appropriate.

<!--[if !supportLists]-->4. <!--[endif]-->Client code compilation—The EJB client code should be


compiled.

<!--[if !supportLists]-->5. <!--[endif]-->Configure application client deployment descriptors


(optional)—Any standard J2EE application client deployment descriptor should be configured to define the
specific configuration properties of your EJB client.

<!--[if !supportLists]-->6. <!--[endif]-->Package client into application client JAR (optional)—The


standard J2EE application client deployment descriptor and compiled EJB client class files should be
packaged into a J2EE application client JAR.

<!--[if !supportLists]-->7. <!--[endif]-->Launch the J2EE application client (optional)—The J2EE


application client may be launched within a special J2EE application client container environment.

Вам также может понравиться