• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Simple Stateless Session Bean

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a small problem with the bean I have deployed.

My Bean class looks in this way:

public class MyBean implements SessionBean {

public String nam = new String();
int cnt = 0;

public void ejbCreate() {
}

public void ejbActivate() {
}

public void ejbPassivate() {
}

public void ejbRemove() {
}

public void setSessionContext(SessionContext ctx) {
}

public String getMessage() {
return (cnt++)+"";
}
}

I have deployed this bean as a Stateless session bean. Now, my client looks like:

public class MyCall {

public static void main(String ar[]) throws Exception {

Context ctx = new InitialContext();
Object obj = ctx.lookup("MyJndi");
MyHome home = (MyHome)PortableRemoteObject.narrow(obj,MyHome.class);
MyComponent comp = home.create();
System.out.println(comp.getMessage());
System.out.println(comp.getMessage());
System.out.println(comp.getMessage());
}

}

Now, every time I am executing the client, I am getting a sequence of higher numbers. i.e first time I am getting 0 1 2...next time 3 4 5 ...so on.

Is this a correct behaviour for a stateless session bean?

Srini
 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Srinivasan,
There is actually something else which you should be doing in your code. Stateless session beans are kept in pools, correct? When the container places the bean instance in the pool, the values of your instance variables
go with it, UNLESS, you initialize your instance variables to their default values at the end of the business methods or something similar. DO NOT initialize them in the constructor and expect good results (you can guess why).

Hope this helps,
Regards.
[ May 09, 2006: Message edited by: Marcelo Ortega ]
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Srinivasan,
Stateless session bean doesn't hold the conversational states between client calls but it doenst guarantee to re-initialize the bean every time a client calls.

In your case, if you have more than one client programs calling the same bean, it still gives the increasing order numbers for different clients.

For example: you have 2 cleints and
you made a call on client-1: get the value 0
and cleint-2 calls on the same bean and he will get the value as 1 (Since bean was not re initialized)
and again client-1 calls: he will get the value as 2
...so this is the case with Stateless session beans.

the bean developer should be careful to eliminate such cases.


Hope this helps.

Ugender
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your Stateless session bean hold states (if you define instance variable in your bean class) but these states are not the conversational states with the CURRENT client.
The current client may hold a reference and can make multiple calls on the reference. But each call time, it may use a different bean in the bean pool, so it may not get what you want.
You can make mutilple test clients (threads)to see the problem.

The stateless means the bean does not maintain the conversational states.
 
Srinivasan Rengan
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone, for your interesting apt replies.

I have actually tried to call the bean from different clients, i.e tried to execute the program from different windows. But, the sequence of numbers seems to be growing.
1) Now, If at all I have to re-intialize the values, where should I do that. From the console, I find that, the ejbCreate() method is invoked only once, inspite of me calling the business method many times.

2) Now, if this is the behaviour potrayed by the stateless bean, then how does the state maintanance play a part in a stateful session bean. Because, I can use the instance variable in by stateless bean to preserve the state.

Thanks
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1) Now, If at all I have to re-intialize the values, where should I do that. From the console, I find that, the ejbCreate() method is invoked only once, inspite of me calling the business method many times.


For stateless session bean, you may have to re-intialize the values in each method.
Any one has good idea?



2) Now, if this is the behaviour potrayed by the stateless bean, then how does the state maintanance play a part in a stateful session bean. Because, I can use the instance variable in by stateless bean to preserve the state.


The states (values) are not conversational states, the instance variables are shared by different clients.
Stateful session bean keeps the conversational states for ONE client as long as it alive. Think about using a Stateful bean as a shop cart for a customer.
 
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you ever have state(instance variables) in stateless session bean that is going to be mess. Though there is no strict rule, but you should not be depending on instance variables in SLSB. Declare all variables which you need to manipulate inside method and use them.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic