aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Newbie stateful vs stateless session bean question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Newbie stateful vs stateless session bean question" Watch "Newbie stateful vs stateless session bean question" New topic
Author

Newbie stateful vs stateless session bean question

Michael MacEachran
Greenhorn

Joined: Dec 15, 2009
Posts: 11
I wrote my first session EJB's from scratch yesterday and deployed them on glassfish. However, they act exactly opposite of what I thought would happen. Here is the scenario:

I have 2 beans StatefulCounter and StatelessCounter, they have the exact same code except for the name. Here are the relevant bits:

private int count = 0;
public void increment() {
count += 1;
}
public String getCount() {
return String.valueOf(count);
}

Verry simple.

I then have a JSP that increments then displays the count of each bean. Here are the relevant bits:

<% InitialContext ic = new InitialContext();

Object oRef = ic.lookup( "StatelessCounter" );
StatelessCounterHome statelessHome = (StatelessCounterHome)PortableRemoteObject.narrow( oRef, StatelessCounterHome.class);
less = statelessHome.create();

Object oRef2 = ic.lookup("StatefulCounter");
StatefulCounterHome statefulHome = (StatefulCounterHome)PortableRemoteObject.narrow(oRef2, StatefulCounterHome.class);
ful = statefulHome.create();

less.increment();
ful.increment();
%>
<table border=1>
<tr>
<td>
Stateless counter: <%=less.getCount()%>
</td>
<td>
Stateful counter: <%=ful.getCount()%>
</td>
</tr></table>

However, when I keep hitting the reload button on FF, the stateless counter goes up, and the stateful counter stays at 1. This is exactly the opposite of what I thought would happen.

Also, here is the relevant ejb-jar.xml:
<session>
<ejb-name>StatelessCounterEJB</ejb-name>
<mapped-name>StatelessCounter</mapped-name>
<description>Simple session bean example</description>
<home>com.pos.ejb.test.home.StatelessCounterHome</home>
<remote>com.pos.ejb.test.remote.StatelessCounterRemote</remote>
<ejb-class>com.pos.ejb.test.StatelessCounter</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
</session>
<session>
<ejb-name>StatefulCounterEJB</ejb-name>
<mapped-name>StatefulCounter</mapped-name>
<description>Simple session bean example</description>
<home>com.pos.ejb.test.home.StatefulCounterHome</home>
<remote>com.pos.ejb.test.remote.StatefulCounterRemote</remote>
<ejb-class>com.pos.ejb.test.StatefulCounter</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Bean</transaction-type>
</session>

What concept am I missing?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Concerning the stateless beans, these beans are pooled, so the container may return you the same bean. That's why the counter keeps growing. Stateful beans are not pooled, so create will return you a new instance, which explains why you'll always get "1".


[My Blog]
All roads lead to JavaRanch
Alex Fluegels
Greenhorn

Joined: Dec 16, 2009
Posts: 3
I agree with Christophe and think that you are using stateful/stateless beans in an incorrect manner.

Stateless Beans (and their members) are not meant to be shared across clients. They should not save a state (in this case the counter variable).
For Stateful Beans it's ok to save the state (counter) which is bound to the client.

I suggest using JSF 2.0 and EJB 3.1. Live is so much easier with it


Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

I think what the OP should have done is invoke increment on each bean a few times,instead of reloading the page....


===>SCJP 1.5(72%)<===
==>SCWCD1.5(76%)<===
Mustafa Garhi
Ranch Hand

Joined: Nov 05, 2008
Posts: 111
Okay one doubt here guys.

We say the state of a client is maintained across calls in Stateful Session Beans.

Now how does the container know that for a particular Stateful bean the same client has called for.

Are cookies/browsers relevant here.

So in case, the code above wanted the incremented count from the Stateful bean in some other JSP, how do we achieve it and how would the container know that okay this is the same client.
Sujeet sharma
Greenhorn

Joined: Mar 01, 2011
Posts: 6
Hi All,

I am also new to ejb and facing same behavoiur. I am hitting ejb from servlet client.

For Stateless counter example, my counter is increasing by one, every time i hit ejb but in case of statefull counter example its showing same value "1".

I couldn't understand it. Can some one help me ?

Regards,
Sujeet
Tom mmeke
Greenhorn

Joined: Jul 19, 2011
Posts: 10

You're missing the basic concept of SF and SL.

If you inject (or look up) an SFSB into your backing bean (JSP page), you 'link' these 2 together.
But since your JSP page is requestscoped, you will recieve a 'clean' JSP page everytime you reload your page, ergo it has lost its link, and creates a new SFSB instance.
I have no experience on JSP & EJB2 technology, so i have no right answer to fix this, but i guess you have to store your SFSB reference in your HttpSession ?

As for the SLSB, in EJB3.x SLSB's are pooled, and you'd have to be extremely lucky to hit the same SLSB every single time.
Not sure why it keeps injecting the same SLSB in your example.

PS: As alex stated, please, for the love of god, use JSF & EJB3.x
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Newbie stateful vs stateless session bean question