Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

query on stateless session bean  RSS feed

 
Suresh Magi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I got a small confusion with stateless session bean. I will show you the code and then will explain you my doubt.

Stateless Bean :

@Stateless(name="HelloBean")
public class SampleBean implements SampleBeanRemote {
@Resource
SessionContext ctxObj;
private String strErrCode = null;
private String strErrMsg = null;

public String searchErrorDescriptor(String strErrCode, String strErrorMsg){
this.strErrCode = strErrCode;
this.strErrMsg = strErrorMsg;

System.out.println("strErrCode == " + this.strErrCode);
System.out.println("strErrMsg== " + this.strErrMsg);

return "I am deployed and running well";
}

public void display(){
System.out.println("I am in display --");
System.out.println("Err Code == " + this.strErrCode);
System.out.println("Err Msg == " + this.strErrMsg);

}
}

EJB Client :
helloUser.searchErrorDescriptor("Error Code"," Error Message");
Thread.sleep(500);
helloUser.display();

If i run the two methods from the ejbclient i got the same output for the instance variable error code and error message. But what i thaught is the dispaly method should display null as the values since it is a stateless bean.

Is there anything wrong in my thaughts ?? or what is that i need to look into statelss session bean when we say it does not hold conversational state.
 
Reza Rahman
author
Ranch Hand
Posts: 580
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suresh,

What you are seeing is happening because the container is handing you the same instance from the pool by pure chance. It is not guaranteed behaviour and will likely not happen in a real world scenario with multiple concurrent clients. It most certainly is not vendor-neutral behaviuor since some implementations create new bean instances on each call.

Best regards,
Reza
 
Suresh Magi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rehman,
Thanks for your reply.

So if the same instance is returned from the pool for the different client then this client has details about other clients. Am i right ??? In our case if the different client try to access the display method directly then he will get the values for those instead variables instead of null.

So what is the takeaway, you mean to say that we should not have instance variables for the stateless session bean.

Thanks
 
Reza Rahman
author
Ranch Hand
Posts: 580
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suresh,

You may have instance variables, but they must be used in a client-agostic manner - for example JMS/data source connections, JPA entity managers, loggers, etc.

Regards,
Reza
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!