EJB Interview Questions and Answers - Page 4

    1 Votes

38. Explain stateful session bean?

Stateful Session Bean maintains the state of a client. A client starts a conversation with a stateful session bean when it invokes the bean’s method in an EJB object. When the conversation ends, the conversational state is preserved. Stateful session beans are dedicated to a single client resulting in no swapping or pooling of instances.

EJB container controls the life cycle of a stateful session bean. EJB container assigns one stateful session bean instance to serve a single client at a time. It does retain client specific state (session state) from one method invocation to the next. Value of instance variables of a bean instance is preserved between calls from same client.

Bean instances are created on demand for client, and removed afterwards. Bean instances are to be maintained for each client. A client can be in active or inactive state. It is not as scalable as stateless session bean.

39. Give some exmples of stateful session bean?

In an online banking application, a stateful session bean is used for performing banking transactions. A client can deposit and withdraw funds using a stateful session bean method.
  • Shopping cart
  • Client specific state needs to be preserved for each client Items that a user wants to buy.
  • Travel ticket purchasing
  • Client specific state needs to be preserved for each client.
  • Tickets to purchase and then confirm/cancel.
40. what are the life cycle stages of a stateful session bean?

The life cycle of a stateful session bean consists of the following stages:

  • Ready: In this stage, a stateful session bean instance remains in the shared pool and services client requests.
  • Passive: In this stage, the EJB container stores the client state in secondary storage and removes the bean instance from the memory. This is done to conserve resources.
  • Does Not Exist: In this stage, a stateful session bean is permanently removed from the shared pool.
41. In what cases stateful session bean instance enters the ready stage?

A stateful session bean instance enters the Ready stage in the following cases:
  • When EJB container creates a new stateful session bean instance.
  • When EJB container activates a passivated stateful session bean.
42. In what cases stateful session bean instance enters the passive stage?

A stateful session bean enters the Passive stage in its life cycle:
  • When EJB Container releases it from serving an idle client and returns the stateful session bean to the shared pool to service another client’s requests.
  • Before passivation, EJB container saves the client state associated with the stateful session bean instance in a secondary storage device, such as a hard disk in the server.
43. How can we prevent the passivation of certain fields in a stateful session bean?

You can prevent the passivation of certain fields in a stateful session bean by declaring them as transient. The objects that are declared transient are not serialized and are not available when deserialized. EJB container passivates and activates the stateful session bean instances in the shared pool to reduce the overhead of storing a large number of bean instances.

44. What is passivation and activation in EJB?

EJB container maintains a limited number of stateful session bean instances in the shared pool. It saves the bean state of an idle bean instance and stores it in a secondary storage device. This process is called passivation.

When the client of a passivated bean calls another bean method, the bean state is restored. This process is called activation. It is not necessary that a bean state be restored to the same stateful session bean instance, which was servicing the client request before bean passivation.

45. What are the various algorithms used to implement passivation in EJB server?

The implementation of the passivation logic varies from one EJB server to another.Various algorithms used to implement passivation are:

  • Least Recently Used (LRU): Selects the stateful session bean instance, which has been inactive for the longest duration. This is also known as eager passivation.
  • Not Recently Used (NRU): Selects the stateful session bean instance, which has become inactive recently.
  • First In First Out (FIFO): Selects the stateful session bean instance, which had entered the pool first and is inactive. This is also known as lazy passivation.

46. What is just in time algorithm used in EJB server?

Most EJB servers use just-in-time algorithm, also referred to as on demand strategy, for activation of beans. According to this algorithm, a passivated bean is activated when a client decides to continue interaction with the bean instance.
An EJB server maintains a directory for preserving the state of passivated stateful session beans, known as the persistent store directory. The persistent store directory contains one subdirectory for each passivated bean. The implementation details of the persistent store directory may vary from one EJB server to another.

47. In what cases stateful session bean instance enters the ready stage?

At the end of its life cycle, a stateful session bean instance moves into the Does Not Exist stage. A stateful session bean instance enters this stage in the following cases:

  • When EJB container invokes the ejbRemove() method.
  • When the timeout of the bean instance, specified by the bean developer, occurs EJB container destroys stateful session bean instances when the number of instances is more than the number of active client requests.

48. When EJB container fails to call ejbRemove()?

EJB Container fails to call ejbRemove() in the following cases:

  • When EJB container failure occurs.
  • When a stateful session bean method execution throws a system exception.

49. What are the steps to create a stateful session bean?

  • Invokes the newInstance() method, which instantiates a new stateful session bean instance.
  • Invokes the setSessionContext() method to associate the stateful session bean instance with the bean context.
  • Invokes the ejbCreate() method of the stateful session bean class. You can pass arguments to the ejbCreate() method to initialize a new stateful session bean instance. 
  • A stateful session bean can have more than one ejbCreate() method, where each method has a different number and type of arguments.
  • A stateful session bean instance can also move to the Ready stage, when EJB container activates a passivated stateful session bean by invoking the ejbActivate() method.

50. What are the sequence of operations initiated by the client in EJB?

  • Client calls a create() method on the home object.
  • Home object requests a bean instance from a pool, home object returns EJB object to client.
  • Client calls business methods on EJB object.
  • EJB object passes the call to Bean instance.
  • Client calls remove() on the EJB object.
  • EJB object releases bean instance.
Page 4 of 4

Popular Videos

communication

How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

JSP Interview Questions and Answers
Servlet Interview Questions and Answers
Advanced Servlet Interview Questions and Answers
Spring Interview Questions and Answers