Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Read Java Certification Certification SCBCD Study Guide - 5. Session Bean Compon

JavaFAQ Home » Java Certification Books and Tests Go to all tips in Java Certification Books and Tests


Bookmark and Share

Chapter 3. Session Bean Component Contract

Identify correct and incorrect statements or examples about session beans, including conversational state, the SessionBean interface, and create methods.

Session bean is specified at deployment as having one of the following state management modes:
  • STATELESS — the session bean instances contain no conversational state between methods; any instance can be used for any client.
  • STATEFUL — the session bean instances contain conversational state which must be retained across methods and transactions.
The conversational state of a STATEFUL session object is defined as the session bean instance’s field values.

In advanced cases, a session object’s conversational state may contain open resources, such as open sockets and open database cursors. A container cannot retain such open resources when a session bean instance is passivated. A developer of such a session bean must close and open the resources in the ejbPassivate and ejbActivate notifications.

All session beans must implement the SessionBean interface.
		public interface SessionBean extends EnterpriseBean {
		void ejbActivate() throws EJBException, RemoteException;
		void ejbPassivate() throws EJBException, RemoteException;
		void ejbRemove() throws EJBException, RemoteException;
		void setSessionContext(SessionContext ctx) throws EJBException, RemoteException;
		}

		public interface EnterpriseBean extends Serializable {
		}
	
A client creates a session bean instance using one of the create<METHOD> methods defined in the session bean’s home interface:
		public interface CartHome extends javax.ejb.EJBHome {
		CartRemote create(String customerName, String account)
		throws RemoteException, BadAccountException,
		CreateException;
		CartRemote createLargeCart(String customerName, String account)
		throws RemoteException, BadAccountException,
		CreateException;
		}
	
		.....
		CartRemote cart = cartHome.create(“John”, “7506”);
	
or for LOCAL view:
		public interface CartHome extends javax.ejb.EJBLocalHome {
		CartLocal create(String customerName, String account)
		throws BadAccountException, CreateException;
		CartLocal createLargeCart(String customerName, String account)
		throws BadAccountException, CreateException;
		}
	
		.....
		CartLocal cart = cartHome.create(“John”, “7506”);
	

The container creates an instance of a session bean in three steps:

  1. The container calls the bean class’ newInstance() method to create a new session bean instance.
  2. The container calls the setSessionContext(context) method to pass the context object to the instance.
  3. The container calls the instance’s ejbCreate<METHOD>() method whose signature matches the signature of the create<METHOD>() method invoked by the client.

Each session bean class must have at LEAST one ejbCreate<METHOD>() method (NOTE: for Entity Beans create methods are optional).

Invoking create() method by client on home interface of STATELESS session bean will NOT invoke ejbCreate() method on bean object. It only creates new EJB(Local)Object. ejbCreate() on STATELESS session bean is invoked by EJB container when it decides to put object in method-ready pool. For STATELESS session beans container handles creating of instances.

Invoking remove() method by client on home interface of STATELESS session bean will NOT invoke ejbRemove() method on bean object. It does nothing. ejbRemove() on STATELESS session bean is invoked by EJB container when it decides to remove object from method-ready pool. For STATELESS session beans container handles removing of instances.

Invoking create<METHOD>() method by client on home interface of STATEFUL session bean will invoke ejbCreate<METHOD>() method on bean object. More precisely: newInstance(), setSessionContext(context), ejbCreate<METHOD>([ARGS]).

Invoking remove() method by client on home interface of STATEFUL session bean will invoke ejbRemove() method on bean object.

The home interface of a STATELESS session bean must have one (and ONLY one) create() method that takes NO arguments.

The create() method of the remote home interface must return the session bean’s remote interface. The create method of the local home interface must return the session bean’s local interface.

STATELESS session bean CAN have instance variables, but there is no guarantee, that these variables will be accessed by the same client. However, the instance variables of the instance can contain the state across client-invoked method calls. Examples of such states include an open database connection and an object reference to an EJB object.

A STATELESS session bean MUST NOT implement the javax.ejb.SessionSynchronization interface.

IBA JV

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy