• Post Reply Bookmark Topic Watch Topic
  • New Topic

2 frames/synchronization?

 
Matt Horton
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have two frames, which both access and iterate two different hashsets off of a parent class.
for example, a parent includes, parent.getDaughters() and parent.getSons()... and each frame draws the respective children of the given gender.
I have the following problem however, my parent bean, stored in HttpSession, may not have been initialized. Both Servlets can check for this state, but if the Daughter Servlet trys to construct the parent bean, the Son Servlet should not try to initiate the object (and different browsers appear to load the servlets in different order). How will I inform the Son Servlet to delay its render until the object is in the proper state?
Thanks in advance...
 
Matt Horton
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Update: the code appears to function if the second servlet just sleeps until the item is created (it appears to be ready within half a second though I have accomodated for 2 seconds, after which the user merely sees an error).
Is this appropriate?

currently these two servlets are being called by a servlet which could ensure the integrity of the bean itself, and then render the frame calls to the associated servlets. That seems as if that design would be cleaner. Does anyone have any suggestions? Should I merely have the 'frameset' servlet control the object instantiation, or is there a more 'proper' way to address this problem.
Thanks again.
[ November 12, 2002: Message edited by: Matt Horton ]
 
Vin Kris
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matt, 2 seconds seems a bit long to me just get an object intialized. The total response will take a beating if in some cases your response consumes the whole of these 2 seconds.
Why didn't you go for synchronization? Since you posted that sleep works, I am inferring that you have only one instance of your application server (servlet container). Therefore, you could try the something like this -

I am also assuming that your JDOManager returns a reference to the same object to both your servlets - because a normal call would create 2 instances of your ref object.
Therefore, whichever servlet gets the control first, obtains a lock accessible by both the servlet threads belonging to the same user and your object is created. The backdrop of this solution is having to possibly call intern() (if they are not already interned) on the session-id string. That will cause a lot of strings to be in-eligible for GC. BUT, If the string is anyway interned (which i strongly suspect is the case), then you will not be under any loss of resources.
Alternatively, you could synchronize on a common string literal, say synchronized("parent_object_init") {}. The dis-advantage of this approach is that all the unrelated threads, belonging to different users, will wait on the synchronized block if their ref is not initialized. This idea sucks, doesn't it? just typed it anyway.
 
Matt Horton
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your time Vin!
I believe that I miswrote earlier... one of the cruxes of my problem is the fact that mySession doesn't actually instantiate 'ref'. Instead, one of the servlets currently handles that task, and then puts it in mySession's jdoManager.
..ie, daughter servlet:

ie, son servlet

I could not come up w/ a way in which synchronize would work in this scenario. Am I just completely missing your point?
If I synchronized the getObject() from the JDOManager, as well as the block that instantiates the mother, would the 'lock of the lock' (sorry if this is a dumb question, I am 90% sure that's not how it works) delay accesses to getObject() long enough for mom to be in the proper state?
Thanks for your patience.
[ November 12, 2002: Message edited by: Matt Horton ]
 
Jesse Beaumont
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would it be possible for the first frame to call load the second frame once it is loaded.
So basically the daughter frame is loaded directly, whereas the son frame contains (initially) only a blank page, or a 'please wait...' message, the response page output from the daughter processing includes a body.onLoad event which redirects the bottom frame to the son servlet for processing. that way you will be sure to have things firing in the right order. The only drawback is the user's perception of the page rendering, but that will probably still be faster than your 2 second timeout.
Hope that helps,
Jesse
 
Vin Kris
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt, this is what i understood from your posts.
- JDOManager is some sort of cache where you store objects - most probably a singleton
- You have 2 servlets that have similar code to instantiate the bean if it isn't already.
- therefore, whichever servlet (daughter or son) comes first starts the instantiation process
- the second servlet should therefore wait and NOT instantiate the same bean.
If you just synchronize your complete "someInitLogic", it should solve the problem unless there is something that i missed.
or.. you could write a base class and both the servlets can extend the base class to invoke a common inherited method. This method does all the work of checking if bean is instantiated or not etc., etc.,. So, the servlet code would just have a call to get mom bean.
Otherwise, the solution given by Jesse seems pretty staright-forward - if you are ok with the user perceiving the rendering process. (does it take time?)
 
Matt Horton
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Vin, Jesse. Sorry that I took so long in responding to your help, but I had an exam earlier that I had to prepare for...
or.. you could write a base class and both the servlets can extend the base class to invoke a common inherited method. This method does all the work of checking if bean is instantiated or not etc., etc.,. So, the servlet code would just have a call to get mom bean.

Just for giggles, I actually wrote both solutions (a synch solution and the javascript solution). First, I extrapolated the servlet initiation logic to a parent class, and synchronized it. This worked fine.
I rolled back these changes and then implemented Jesse's solution. This is actually incredible elegant, just b/c of its simplicity. After seeing concerns elsewhere on this site about synchronized threads in servlets (dated concerns?? does the newer API or the newer containers make synchronized a valid option?), I think that I'll stick to the javascript load of the second servlet in the second frame.
Thanks guys. This website rocks. I build a wealth of knowledge even resolving the most rudimentary issue.
Thanks again...
 
Matt Horton
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by the way, the second page's load is transparent to the user...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!