Enterprise beans

An enterprise bean is a Java component that can be combined with other resources to create Java applications. There are three types of enterprise beans, entity beans, session beans, and message-driven beans.

All beans reside in Enterprise JavaBeans (EJB) containers, which provide an interface between the beans and the application server on which they reside.

EJB 2.1 and earlier versions of the specification define entity beans as a means to store permanent data, so they require connections to a form of persistent storage. This storage might be a database, an existing legacy application, a file, or another type of persistent storage.

New or updated for this feature pack The EJB 3.0 specification deprecates EJB 1.1-style entity beans. The Java Persistence API (JPA) specification is intended to replace the deprecated enterprise beans. While the JPA replacement is called an entity class, it should not be confused with entity enterprise beans. A JPA entity is not an enterprise bean and is not required to run in an EJB container.

Session beans typically contain the high-level and mid-level business logic for an application. Each method on a session bean typically performs a particular high-level operation. For example, submitting an order or transferring money between accounts. Session beans often invoke methods on entity beans in the course of their business logic.

Session beans can be either stateful or stateless. A stateful bean instance is intended for use by a single client during its lifetime, where the client performs a series of method calls that are related to each other in time for that client. One example is a shopping cart where the client adds items to the cart over the course of an online shopping session. In contrast, a stateless bean instance is typically used by many clients during its lifetime, so stateless beans are appropriate for business logic operations that can be completed in the span of a single method invocation. Stateful beans should be used only where absolutely necessary. Using stateless beans improves the ability to debug, maintain, and scale the application.

New or updated for this feature pack The Feature Pack for EJB 3.0 supports stateless and stateful session beans. They follow a simple pattern such as: The end result of a simple EJB 3.0 stateful session bean looks like the following:
package ejb3demo;

@Stateful
public class Cart3Bean implements ShoppingCart {
    private ArrayList contents = new ArrayList();
    
public void addToCart (Object o) {
		contents.add(o);
    }

public Collection getContents() {
    return contents;

    }
}
EJB components can use annotations such as @EJB and other injectable @Resource references if the module is an EJB 3.0 module.
Avoid trouble New or updated for this feature pack Avoid trouble: If you define a class-level persistence unit or extended persistence context for a stateful session bean or any associated interceptor class, then the stateful session bean cannot be serialized. Passivation for that stateful session bean fails. If you have defined an extended persistence context (container-managed) or a persistence unit (application-managed), which is always considered to be extended, then stateful session bean holds a reference to an entity manager. If the entity manager is in the open state, then the stateful session bean cannot be serialized and passivation fails.

To resolve this problem, you can add a pre-passivate interceptor method to the stateful session bean that closes an open entity manager and add a null value to the entity manager field prior to any passivation attempt. This approach allows the stateful session bean to be passivated without failure because it is now serialized. Allowing the activation policy for the enterprise bean to default to the Once value is not a complete solution to this problem. An activation policy of Once causes the enterprise bean instance to not be passivated on transaction boundaries. However, it does not prevent passivation if the stateful session bean is evicted from the enterprise bean cache when the cache becomes full. When the enterprise bean is evicted from the cache, it is passivated. This problem only occurs when the application is under load. Therefore, the solution of using the pre-passivated interceptor to close the entity manager and set the entity manager field to null is the only complete solution to the problem.

gotcha

New or updated for this feature pack Web application clients and application clients can use deployment descriptor defined EJB references. If the reference is for an EJB 3.0 session bean without a home interface, the reference should be defined with a null <home> or <local-home> setting in the deployment descriptor.

New or updated for this feature pack Web application clients and application clients can also use @EJB injections for references to EJB session beans within the same enterprise archive (EAR) file, but the binding must either use the AutoLink support within the container or the annotation must use the name of the reference that is defined by the deployment descriptor and bound when the application is installed. For more information about AutoLink, see the topic, "EJB 3.0 application bindings support."

Message-driven beans enable asynchronous message servicing.

Beans that require data access use data sources, which are administrative resources that define pools of connections to persistent storage mechanisms.




Related concepts
Feature Pack for EJB 3.0
EJB 3.0 application bindings overview
Data sources
Message-driven beans - JCA components
Message-driven beans - listener port components
Related tasks
Task overview: Using enterprise beans in applications
Related reference
Enterprise beans: Resources for learning
Options for the AdminApp object install, installInteractive, edit, editInteractive, update, and updateInteractive commands
Concept topic Concept topic    

Terms and conditions for information centers | Feedback

Last updatedLast updated: Aug 31, 2013 2:56:59 AM CDT
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=pix&product=was-nd-dist&topic=cejbmain
File name: cejb_main.html