Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lookup returns new instance of Stateful session bean

I am using Java EE 5, EJB 3.0, Jboss AS 4.3.2.
I have simplest Stateful bean

@Local
public interface IStateBean 
{
}
@Stateful
public class StateBean implements IStateBean
{  
   private static int number = 0;

   @PostConstruct
   void init()
   {
      number++;
      System.out.println("@PostConstruct: " + number);
   }

   @PreDestroy
   void destroy()
   {
      number--;
      System.out.println("@PreDestroy: " + number);  
   }
}

I do lookup in servlet for this bean

public class MyServlet extends HttpServlet
{  
   @Override
   public void doGet(final HttpServletRequest aRequest, final HttpServletResponse aResponse) throws ServletException, IOException
   {  
      IStateBean bean = new InitialContext().lookup("app-ear/StateBean/local");
      // ...
   }
}  

But each time new instance of StateBean is created.
I can call lookup twice but new instance of StateBean is created again

   @Override
   public void doGet(final HttpServletRequest aRequest, final HttpServletResponse aResponse) throws ServletException, IOException
   {  
      IStateBean bean1 = new InitialContext().lookup("app-ear/StateBean/local"); 
      IStateBean bean2 = new InitialContext().lookup("app-ear/StateBean/local"); // new instance is created
      // ...
   }  

I expect the same instance in the same http-session

Servlet mapping in web.xml

   <servlet>
      <servlet-name>MyServlet</servlet-name>
      <servlet-class>com.package.MyServlet</servlet-class>
   </servlet>  
   <servlet-mapping>
      <servlet-name>MyServlet</servlet-name>
      <url-pattern>*.html</url-pattern>
   </servlet-mapping>
like image 927
Ilya Avatar asked Nov 11 '13 10:11

Ilya


People also ask

How is a state of a stateful session bean achieved?

A stateful session bean is a type of enterprise bean, which preserve the conversational state with client. A stateful session bean as per its name keeps associated client state in its instance variables. EJB Container creates a separate stateful session bean to process client's each request.

Which are the states of stateful session bean?

The life cycle of a stateful session bean has three states: Does Not Exist, Method-Ready, and Passivated. This sounds a lot like a stateless session bean, but the Method-Ready state is significantly different from the Method-Ready Pool of stateless beans.

What is difference between stateful and stateless session bean?

An instance of a stateful session bean has a unique identity that is assigned by the container at create time. Stateless: A stateless session bean does not maintain conversational state. Instances of a stateless session bean have no conversational state.

Which stage of stateless session bean life cycle bean has instance in the memory of the EJB container and it is ready to serve clients?

The Lifecycle of a Singleton Session Bean The singleton session bean is now ready to have its business methods invoked by the client. At the end of the lifecycle, the EJB container calls the method annotated @PreDestroy, if it exists. The singleton session bean is now ready for garbage collection.


1 Answers

The EJB spec does not say, that multiple lookups will return the same instance of a stateful session bean. In opposite: It is even required for the server to create two different instances, to guarantee that every client gets his own instance on the server.

The EJB spec only says that while you're referencing a stateful session bean, it retains its internal state across multiple method invocations:

IStateBean bean = new InitialContext().lookup("app-ear/StateBean/local");
bean.myMethod1();
bean.myMethod2(); // affects the same EJB instance on the server

Note that this might NOT be the case when using stateless session beans. Here, the two method calls shown above might go to different instances on the server!

like image 66
isnot2bad Avatar answered Sep 28 '22 03:09

isnot2bad