- Session Beans
In the
Java Platform, Enterprise Edition specifications, a Session Bean is a type of Enterprise Beans. In theJ2EE architecture, the other two types areEntity Bean s andMessage-driven bean s. However, in Java EE 5 entity beans have been replaced byJava Persistence API entities.Contrary to Entity Beans which represent persistent data maintained in a database, a Session Bean implements a business task and is hosted by an
EJB container .A Session Bean is created by a client and usually exists only for the duration of a single client-server session. A session bean performs operations, such as calculations or database access, for the client. Although a session bean can be transactional, it is not recoverable should a system crash occur. Session bean objects either can be stateless or can maintain conversational state across methods and transactions. If a session bean maintains state, then the EJB container manages this state if the object must be removed from memory. However, the session bean object itself must manage its own persistent data.
tateless Session Beans
A stateless session bean is a distributed object that does not have an associated conversational state, thus allowing concurrent access to the bean. The contents of instance variables are not guaranteed to be preserved across method calls. All instances of a stateless session bean are identical.
Remote Stateless SessionBean Hello World example:
Java EE 5
J2EE 1.4
Remote interface, declares methods clients can invoke on the EJB:
Home interface, declares create, destroy and finder methods for the EJB depending on type:
The implementing EJB class:
A simple client:
tateful Session Beans
Stateful session beans are distributed objects having a conversational state. The state could be persisted, but access to the bean is limited to only one client.
External links
* [http://java.sun.com/j2ee/1.4/docs/tutorial/doc/EJBConcepts3.html What is a Session Bean ? (Sun's J2EE Tutorial)]
* [http://java.sun.com/j2ee/1.4/docs/tutorial/doc/EJBConcepts9.html Entreprise Beans lifecycle]
* [http://www.unix.org.ua/orelly/java-ent/ebeans/appb_02.htm Life cycle state diagram of session beans]
Wikimedia Foundation. 2010.