aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Simple Stateless Session Bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Simple Stateless Session Bean" Watch "Simple Stateless Session Bean" New topic
Author

Simple Stateless Session Bean

Srinivasan Rengan
Ranch Hand

Joined: Nov 07, 2004
Posts: 122
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


SCJP, SCWCD, SCBCD (EJB 2.x and EJB 3), OCA, SCDJWS
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 528

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 ]

SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA

Live life to an interface, not an implementation!
Ugender Rekulampally
Ranch Hand

Joined: Nov 14, 2005
Posts: 130
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
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
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

Joined: Nov 07, 2004
Posts: 122
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

Joined: Feb 02, 2006
Posts: 2023
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.
Rizwan Mohammad
Ranch Hand

Joined: Sep 02, 2005
Posts: 445
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.


Rizwan
SCJA, SCJP, SCWCD, SCBCD, SCDJWS.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple Stateless Session Bean