0
votes

i'm new in JavaEE and I try to figure out the difference between stateless and stateful session beans. What I have understood so far:

1.) in stateful session beans the state of the bean is bound with the client; therefore as long as we are in the same session with the same user there should be the same state of the bean instance

2.) in stateless session beans there is no state bound to the session and the client; in fact, the bean instances can interchange on every invocation or request of the user

To try this I wrote a short servlet which just print out the number of hits with every request on a stateless bean. This is the servlet:

package com.java.ee.ejb.stateless;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet
public class StateLessServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @EJB
    private StateLessBean stateLessBean;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        System.out.println(stateLessBean);
        stateLessBean.increaseHits();

        resp.getWriter().write("<h1>Hits: "+stateLessBean.getHits()+"<h1>");

    }

}

And this is the stateless bean:

package com.java.ee.ejb.stateless;

import javax.ejb.Stateless;

@Stateless
public class StateLessBean {

    private int hits;


    public void increaseHits() {
        hits++;
    }


    public int getHits() {
        return hits;
    }



}

But it seems to be, that I invoked the methods every time on the same object - should it not be exact the opposite, that means invocation each time on different instances when I use stateless session beans? Have I forgot something?

enter image description here

1
Just because your test happens to always hit the same instance doesn't mean it will always be the case. Start sending many requests concurrently, and add a Thread.sleep(5000L) in the increaseHits() method, and you'll start seeing differences.JB Nizet
No ive tried it and it never changesHakan Kiyar
What never changes? Because if you only look at the toString() of the stateless bean reference in the servlet, that will never change: the servlet is a singleton. Print this.toString() in the EJB methods.JB Nizet

1 Answers

2
votes

Statefull session beans keep state per client. The state you are seeing is state that is kept on the instance of the Stateless Session Bean.

When you create a Stateless Session Bean, the application server will typically create a pool of these beans. I would suggest that your implementation is creating a pool of 1. As they are stateless (your's isnt!), this means that multiple threads can operate on the same instance without having any impact.

It is up to the application server to determine if it want's to create multiple instances of Stateless Session beans or not.