Stateless Session Bean Design Question.


EJB design: Stateless Session Bean Design Question.

  1. Stateless Session Bean Design Question. (1 messages)

    Hello, I am implementing a stateless session bean EJB 2.0 on WL 8.1. I am new to EJB, so my understanding is limited. This is more of a design question. I have a session bean, encapsulating a service. public class MySessionBean extends GenericSessionBean{ private MyService service = new MyService(); public Collection getRows(){ return service.getDao().getRows(); } } The service in this case MyService, initializes the DAO and queries the underlying table. The side effects that I am seeing here is that UI which initializes the Session Bean, is getting more rows than it should. By that I mean the first call to the method returns 1 row, 2nd call 2 rows and so on. The DAO was defined in the following fashion.. public class MyDao{ private List rows; public MyDao(){ rows = new ArrayList() } public List getRows(){ //Get connection. //Query. while(rs.hasNext()){ //populate the rows. } } } I have resolved the issue (or I think I did) by moving the "rows" attribute to the getRows() method. public List getRows(){ List rows = new ArrayList(); //Get connection. //Query. while(rs.hasNext()){ //populate the rows. } } However, I feel that the culprit here is the service declaration in the Stateless Session Bean. It looks like EJB is caching the service reference by the virtue of it's initialization. The service inturn is caching the DAO. Ideally this should be behaving stateless, i.e. every call should not know of the previous call. My question is - Is the service declaration correct? Should it be more an part of the EJB init? Or EJB constructor? Are there other alternative design approaches to this one? Thank you in advance for your response. RJ
  2. The most important thing about a stateless session bean is that it should not have state :) So don't declare any members. Instead handle everything within the method calls. The EJB container caches stateless session bean instances and re-uses them for subsequent requests. It is up to you to guarantee their stateless behavior. Johnny Webworks JEExplorer - file explorer for your Java web applications