• Post Reply Bookmark Topic Watch Topic
  • New Topic

InitialContext Lookup returns the same Stateful Session Bean Reference everytime ...  RSS feed

 
De La Hai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

In my current project I am facing a strange problem: whenever I want to get my Stateful Session Bean remote object , I get always the same reference. So all clients get the same remote object on server side ... which is of course not the incentive by the Stateful Session Beans model...

I have written a small test application which behaves exactly the same as my projects application ...

here the code :
the SFSB : testBean


the Remote Interface: testRemote



the test JSP



Also I am using Eclipse Europe Winter IDE & Glassfish v2

Everytime it returns the same reference for the SFSB Object , ... I don't know why this happens ... is it a fault by me or is it a Bug on Server Side ??

I just don't know ... ... I also tried it with Servlet Injection but the result is the same , I guess it's my fault but I have no clue how to solve it ...

thanks in advance ...

LHD
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dang,

welcome to the JavaRanch

As far as I'm familiar with Java servlet technology the problem in your code isn't in the EJB layer but in your JSP/servlet! Servlets (and JSPs which get compiled to servlets) are shared among multiple threads in a servlet container. So you'll probably only be using the same SFB because it's always the same servlet you are using

I personally have never experienced any problem with SFBs...

Marco
[ June 04, 2008: Message edited by: Marco Ehrentreich ]
 
De La Hai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So by this you mean that I am only accessing the same servlet everytime ? I am running my ejb-application and my web-application on localhost so it is difficult for me to test it with multible clients, I have just created request via different browser client etc... But anyways, how can I test it properly ?

Also to my little experience with Servlet/JSP , if I understand you correctly . Each time I access my Servlet the Servlet will be processed in a thread on my server, and even with my multiple browsers the same servlet is being called ? May this be the reason why everytime the same SFB-Reference is assigned to my client ? ...

I will test it right away... but any more hints are still appreciated

Thank you

LHD
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly this is the point (if I'm not totally wrong) The scriptlet code in your JSP ends up in a servlet the servlet container automatically compiles. For performance reasons it's absolutely reasonable that the servlet container reuses this servlet for all clients. So servlets are a very bad place for client-specific data like the reference to your SFB. A servlet should always be thread-safe which is not the case here because you share data among multiple threads that should not be shared.

The easiest way to test this is probably to move the SFB reference into session scope and don't maintain it in the servlet!

Marco
 
De La Hai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again,

just one thing I am wondering about : How should you design your WEB Client ? If it shouldn't be the way I have done ... ?

I have followed Suns EJB Tutorial and then came to this problem ... how do you do it ? As with my understanding , I have created an EAR project then bind a EJB and dynamic web application to it , with dynamic web application being dependant on EJB's Jar (J2EE Dependencies)...
 
De La Hai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The easiest way to test this is probably to move the SFB reference into session scope and don't maintain it in the servlet!


I get the point but I don't know how I should implement this . What do you mean by session scope ? which session scope ? Can you give me some example code for my problem ?

thank you

LHD
 
De La Hai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example this code :



If I am trying this I would always get the same reference of the EJB "foo" ... . Isn't it the correct way how to get an EJB ?
the Code is from : http://javahowto.blogspot.com/2007/07/simple-ejb-3-servlet-application.html

Or hasn't the author just not thought of this problem ?

thanks
 
De La Hai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

ok now I have figured it out , since those examples you find on the internet are such misleading , I was kinda stucked, since I believed that these are the suggested ways how to call a SFB ...

I have understood the Servlet problem now and had implemented my test application in this way :

Since I only want to have a WEB APP for accessing my EJB APP

I had declared a Stateless Session Bean , which creates on Server Side the SFB and returns me the Reference of it ...

In my Servlet class I just called the Stateless Bean and get the Reference for my SFB and work with it ... passing it with the Http Session etc...


Is this an appropriate solution ??? what do you think is better ??


Thank you very much for your help

LHD
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dang,

this sounds a lot better than with your first try What I don't really understand is what you're trying to achieve with this stateless session bean As far as I can see from your description this only adds one additional layer between the servlet and stateful session bean. This just makes the architecture unnecessarily more complicated without gaining any benefit. Do you have any special reason to use this intermediate stateless bean?

The problem with your first approach wasn't to get the reference to you SFB in the servlet. It was just a problem that you stored the reference in the servlet by defining a member variable for it. Just get your SFB like before and put it immediately as an attribute in HttpSession scope instead. Perhaps there's a more elegant way to do this but at least it should work then.

Marco
 
De La Hai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marco ,

I am using this Stateless Bean as a workaround I guess , because I just want to acces my EJB Layer by a WEB Client. By this I mean that my application will start with a startpage, from which the user invokes some action . A Stateful Session Bean should be created for him/her and then he can work with it. .. Since the client requests a SFB through a Servlet , hence the Servlet is processed on the serverside in may be the same thread/object, which leads to the problem that I have described. My workaround is that you use the Stateless Bean as an entry point for invocation ... . In the EJB Container , the Stateless Bean creates a new Instance of a Stateful Bean and returns it ...

When I get it by an Servlet I have to lookup/ or inject the EJB Object somewhere ... either as an class variable or in the doGet or doPost methods (lookup) ... in both cases the Servlet is the same in the WEB COntainer right ? so I will always get the same reference...

I don't see how I can get a new Reference from a servlet/jsp when the only way is to call lookup or injection ...

passing the referenced SFB in the Session is not the issue here i guess because the first reference I get is already the same reference shared by the clients ....

Thats why I'm using this workaround ... but honestly this method is not good i can see it by myself ... But just a question by curiosity , how do you normaly get your SF Session ???

Or am I missing something from your post above ??? Sry if I have missed it though

Thanks

LHD

PS: Isn't it a basic problem ? Shouldn't everybody have used SFSB already ??? how is it done correctly ?
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dang,

I think we're not (yet) talking about exactly the same solution to your problem But I'm sure we'll get things working...

So let me explain in more detail what I think is the problem. It doesn't matter or depends on your application server and its supported J2EE version if you'll be able to use dependency inject to get a reference to the SFB or if you have to do the JNDI lookup yourself. But that isn't the point and it's particularly not the cause of your problem. I think it doesn't really matter which one of these two you are using:

or

The real problem is this here:

As you can see you're not only doing the lookup/injection for the SFSB in your servlet. You even save the reference to the bean IN YOUR SERVLET CLASS. And because there's only one instance of each servlet in a web application (or maybe a pool of identical instances) it should be obvious that all clients which access this servlet are using the same reference to the same session bean.

You should save the remote reference to the HTTP session context in order to get have a distinct SFSB reference for each web client accessing your web application simply with something like:

This way you can use the session attribute later to get the specific SFSB for each client.

Unfortunately I'm not an expert in JSPs and servlets but besides the JNDI lookup for your bean this problem has absolutely nothing to do with EJB. You would encounter problems of this sort with really every resource you put directly in a servlet but actually don't want to share with other clients/threads. To go one step further it's not even a problem of web applications. This is an ordinary multi-threading issues because servlets have to be thread-safe which for your servlet isn't the case. You unintentionally share a resource among other threads - that's all.

Usually you have to use correct synchronization to avoid such problems but this also prevents good concurrency. So a better approach would be to use the servlet only as an entry point to your application and do all further client-specific processing in separate classes.

But now it's really going too far. Unfortunately it's not possible to explain everything about good design and architecture of (web) applications here. You should read some books if you're not familiar with common design patterns for good web applications or you may simply use a good web framework once you are familiar with the basics of servlets and JSPs.
If you really want to understand it you should perhaps first try without a framework. Have a look at this excellent article by Bear Bibeault. It explains how to achieve a very good architecture and design for a web application without using a framework.

So, I hope after reading all this you'll manage to solve your problem! Good Luck!

Marco
 
Juggy Obhi
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All, Thanks for the valuable information. I would like to know one thing that i read in EJB 3.0 in Action that for SFSB you should use JNDI for lookup in order to get different SFSB instance for each user. Using Dependency Injection will result in the return of same SFSB each time. Is this true? Is there any difference between DI and JNDI Lookup as far as Singleton/protoype design pattern is concerned?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!