• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Newbie stateful vs stateless session bean question

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what the OP should have done is invoke increment on each bean a few times,instead of reloading the page....
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 10
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic