EJB Cheat Sheet

Quick reminder of EJB types and configurations

EJB Types

    1. Session Bean: stateless, stateful.

    2. Entity: Container-managed persistence (CMP) and bean-managed persistence (BMP).

    3. Message Driven

Interfaces To Extend

    1. EJBHome (or EJBLocalHome) : create, remove, find. (EJB factory)

    2. EJBObject (or EJBLocalObject): business methods.

Local interfaces introduced in EJB 2.0. Client uses these extending interfaces.


Implement one of the interfaces (but not EJBHome or EJBObject):

    1. SessionBean: ejbCreate(). For stateful session beans only, you can use ejbActivate(), ejbPassivate(), ejbRemove().

    2. EntityBean: CMP are abstract.

    3. MessageDrivenBean and MessageListener: onMessage()

The class will implement the business methods defined in EJBObject.

ejb-jar.xml Deployment Descriptor

<ejb-jar> <enterprise-beans> <session> <ejb-name>MySessionBean</ejb-name> <home>InterfaceThatExtendsEJBHome</home> <remote>InterfaceThatExtendsEJBObject</remote> <ejb-class>ClassThatExtendsSessionBean</ejb-class> <session-type>Stateful | Stateless</session-type> <!-- this is how the container knows the type --> <transaction-type>Container | Bean</transaction-type> </session> <entity> <ejb-name>MyEntityBean</ejb-name> <home>InterfaceThatExtendsEJBHome</home> <remote>InterfaceThatExtendsEJBObject</remote> <ejb-class>ClassThatExtendsEntityBean</ejb-class> <persistence-type>Bean | Container</persistence-type> <reentrant>False</reentrant> </entity> <message-driven> <ejb-name>MyMDB</ejb-name> <ejb-class>ClassThatImplementsMessageDrivenBeanAndMessageListener</ejb-class> <transaction-type>Container | Bean</transaction-type> <message-driven-destination> <destination-type>javax.jms.Topic</destination-type> <subscription-durability>NonDurable</subscription-durability> <!-- msg will be lost if server go down --> </message-driven-destination> </message-driven> ... </enterprise-beans> <assembly-descriptor> <container-transaction> <method> <ejb-name>MyEntityBean</ejb-name> <method-intf>Remote</method-intf> <method-name>myMethod*</method-name> </method> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor> </ejb-jar>


ACID: Atomic, Consistent, Isolated, Durable

    1. Container Managed Tx (CMT): Entity beans can use this only.

    2. Bean Managed Tx (BMT): Only session and MDBs can use.

        • Local: Uses JDBC.

        • Global/Distributed: Uses UserTransaction object.

Two-Phase Commit

    1. Manager writes data to all resources.

    2. Manager ask each resource to prepare (promise) to commit.

    3. If all prepare then manager asks resources to commit

    4. If any fail, manager will rollback all others.

Transaction Types

    1. NotSupported: If caller has a tx, container will suspend before calling and resume when returned.

    2. Supports: If caller has a tx, it will be passed.

    3. Required: If caller has a tx, it will be passed, if not, a new tx is created and passed.

    4. RequiresNew: A new tx is created for this method.

    5. Mandatory: The caller must have a tx.

    6. Never: Caller must not have a tx.