0

I am new to EJB. I am trying to understand the functionality of stateless enterprise bean .

Have a look at the following example:

 @Stateless
 @LocalBean
 public class FlightService implements Serializable {

/**
 * Default constructor. 
 */
public FlightService() {
    // TODO Auto-generated constructor stub
}
/**
 * @return the id
 */
public Integer getId() {
    return id;
}
/**
 * @param id the id to set
 */
public void setId(Integer id) {
    this.id = id;
}
/**
 * @return the from
 */
public String getFrom() {
    return from;
}
/**
 * @param from the from to set
 */
public void setFrom(String from) {
    this.from = from;
}
/**
 * @return the to
 */
    public String getTo() {
        return to;
   }
   public void setTo(String to) {
    this.to = to;
    }
/**
 * @return the price
 */
   public Integer getPrice() {
    return price;
    }
/**
 * @param price the price to set
 */
    public void setPrice(Integer price) {
        this.price = price;
   }
/**
 * @return the numOfSeats
 */
    public Integer getNumOfSeats() {
        return numOfSeats;
   }
/**
 * @param numOfSeats the numOfSeats to set
 */
   public void setNumOfSeats(Integer numOfSeats) {
       this.numOfSeats = numOfSeats;
    }
  /**
   * @return the airplaneModel
 */
   public String getAirplaneModel() {
        return airplaneModel;
   }
/* (non-Javadoc)
 * @see java.lang.Object#toString()
 */
@Override
public String toString() {
        return "FlightService [id=" + id + ", from=" + from + ", to=" + to + ", price=" + price + ", numOfSeats="
            + numOfSeats + ", airplaneModel=" + airplaneModel + "]";
}
/**
 * @param airplaneModel the airplaneModel to set
 */
public void setAirplaneModel(String airplaneModel) {
    this.airplaneModel = airplaneModel;
}
private Integer id =23467;
private String from="Los Angles";
private String to="London";
private Integer price=400;
private Integer numOfSeats=250;
private String airplaneModel="Boeing 787";

}

Here is the class where @EJB dependency injection is being used.

@EJB
private FlightService flightService;
@EJB
private FlightService flightService1;
@EJB
private FlightService flightService2;
@EJB
private FlightService flightService3;
@EJB
private FlightService flightService4;
@EJB
private FlightService flightService5;
@EJB
private FlightService flightService6;
public FlightDetails() {
    super();
    System.out.println(flightService);

}
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
  PrintWriter out = response.getWriter();
  response.setContentType("text/html");
  flightService.setAirplaneModel("Rishanth");
  flightService.setFrom("vijaywada");
  flightService.setId(1);
  flightService.setNumOfSeats(4);
  flightService.setPrice(4);
  flightService.setTo("hyderabad");      
  flightService4.setAirplaneModel("cannadian Airlines");
  out.println(flightService.getAirplaneModel());  

}

I want to understand when i print

 "flightService.getAirplaneModel()" 

why am i getting the value set on

 "flightService4.setAirplaneModel("cannadian Airlines");"

Any Help would be appreciated.

mohan babu
  • 1,388
  • 2
  • 17
  • 35
  • Because the bean is supposed to be **Stateless**, i.e. it doesn't have any conversational state. And BTW, even if it had one, you're calling setAirplaneModel on flightService4, and calling getAirplaneModel on flightService. – JB Nizet Feb 13 '17 at 20:05
  • @JBNizet I Apologise for the Typo. Please revisit the question again. – mohan babu Feb 13 '17 at 20:14
  • Again, the bean is supposed to be **stateless**. So it can't have any conversational state. The container is free to reuse any instance for any request, provided no two requests are handled concurrently by the same instance. – JB Nizet Feb 13 '17 at 20:23

1 Answers1

2

That is mainly because the J2EE server will have a pool for each @Statelss EJB, and for each single client a new instance will be created. In your case, It happens that only one client is using the injected EJBs.

Reference: http://docs.oracle.com/cd/E13222_01/wls/docs81/ejb/session.html

Similar question: Difference between @Stateless and @Singleton

Community
  • 1
  • 1