Академический Документы
Профессиональный Документы
Культура Документы
Deployment
EJB Clients
April 27, 2008 at 2:19 pm (EJB, Unit 3) (EJB)
EJB Clients
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.
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.
Primary Keys
April 22, 2008 at 3:46 am (EJB, Unit 3)
Primary Keys
Example:
• 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
Entity Beans
• 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.
• 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.
• 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.
• 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
Solutions:
• 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.
Constraints on Beans
April 22, 2008 at 3:08 am (EJB, Unit 3)
Common Constraints for all beans:
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]-->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]-->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.
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.