Deployment
Deployment
-
Must include the names of the home interface, remote interface, and implementation class.
-
Set the isolation level and transaction attribute for the bean.
-
Deployment Descriptor must be of type javax.ejb.deloyment.EntityDescriptor
-
Include the name of the primary key class.
-
Provide the list of container-managed fields
-
Must specify whether the bean is reentrant or not.
-
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.
Entity Bean Life Cycle
-
An entity bean can have only three states:
-
Non-existence
-
Pooled
-
Ready
-
-
Non-existence
-
In this state, the bean doesn’t exist.
-
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.
-
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
-
In this state, entity beans have no identity. So all are identical.
-
When an ejbFind() method is invoked, the container uses an EJB instance from the pooled state to handle the request.
-
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.
-
-
The ejbPostCreate() method takes the same arguments as the ejbCreate() method. It is invoked only after ejbCreate() has reached completion successfully.
-
ejbPostCreate() coded to perform any special processing needed after the beanis created, but before it becomes available to the client.
-
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.
-
-
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:
-
In this state, beans accept invocations of its business methods. It can also process invocations of its ejbLoad() and ejbStore() methods.
-
The container keeps the state of the bean with its representation in the database.
-
ejbLoad() reads data from the database and uses it to popular instance variables, and ejbStore() writes data from the instance variables to the database.
-
When a bean is activated, the container automatically invokes its ejbLoad() method.
-
Similarly, before the bean becomes passivated, its ejbStore() method is invoked.
-
So, no logic needed in the ejbActivate() and ejbPassivate() to handle state synchronization between the objectand the database.
-
-
Reentrant Instances
-
It is possible, in narrow circumstances, to make an entity EJB reentrant.
-
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
-
Serialize all arguments on the client side.
-
Transmit the arguments to the server
-
Deserialize the arguments on the server
-
Invoke the remote method
-
Serialize the return value on the server
-
Transmit the return value to the client
-
Deserialize the return value on the client side.
-
Primary Keys
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.
BMP and CMP
Bean Managed and Container-Managed Persistence
-
There are two types of entity beans:
-
Entity beans with bean-managed persistence
-
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.
When to use Entity Beans
|
Entity Beans When to use Entity Beans: |
|
Transaction Isolation Levels
|
Transaction Isolation Levels: |
|
Transactions and EJB
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 |
|
TX_NOT_SUPPORTED |
None |
None |
|
T1 |
None |
|
|
TX_REQUIRED |
None |
T2 |
|
T1 |
T1 |
|
|
TX_SUPPORTS |
None |
None |
|
T1 |
T1 |
|
|
TX_REQUIRES_NEW |
None |
T2 |
|
T1 |
T2 |
|
|
TX_MANDATORY |
None |
Error |
|
T1 |
T1 |
-
TX_NOT_SUPPORTED means that the bean should never be called within a transaction
-
If a client calls the bean without a transaction in effect, the bean method is invoked.
-
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.
-
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.
-
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.
-
If the client does not supply a transaction context, it invokes the bean’s method with no transaction context.
-
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.
-
If the client has not supplied a transaction context, the EJB container creates a new one.
-
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
-
If the client attempts to invoke a TX_MANDATORY bean without a transaction context, the container will throw a TransactionRequiredException to the client.
-
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:
-
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.
-
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.
-
Session Bean Life Cycles
Session Bean Life Cycle
-
Stateless session bean has only two states:
-
Non-existence
-
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.
-
The bean can enter a transaction
-
It can be passivated
-
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.