EJB Cheat Sheet
Quick reminder of EJB types and configurations
EJB Types
Session Bean: stateless, stateful.
Entity: Container-managed persistence (CMP) and bean-managed persistence (BMP).
Message Driven
Interfaces To Extend
EJBHome (or EJBLocalHome) : create, remove, find. (EJB factory)
EJBObject (or EJBLocalObject): business methods.
Local interfaces introduced in EJB 2.0. Client uses these extending interfaces.
Class
Implement one of the interfaces (but not EJBHome or EJBObject):
SessionBean: ejbCreate(). For stateful session beans only, you can use ejbActivate(), ejbPassivate(), ejbRemove().
EntityBean: CMP are abstract.
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>
Transactions
ACID: Atomic, Consistent, Isolated, Durable
Container Managed Tx (CMT): Entity beans can use this only.
Bean Managed Tx (BMT): Only session and MDBs can use.
Local: Uses JDBC.
Global/Distributed: Uses UserTransaction object.
Two-Phase Commit
Manager writes data to all resources.
Manager ask each resource to prepare (promise) to commit.
If all prepare then manager asks resources to commit
If any fail, manager will rollback all others.
Transaction Types
NotSupported: If caller has a tx, container will suspend before calling and resume when returned.
Supports: If caller has a tx, it will be passed.
Required: If caller has a tx, it will be passed, if not, a new tx is created and passed.
RequiresNew: A new tx is created for this method.
Mandatory: The caller must have a tx.
Never: Caller must not have a tx.