Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Objects In Servlet Context

 
Vikas Aggarwal
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am using an approach where I have kept the Objects on all of my beans, Static ResultSets in the servlet context at the server startup.
So any servlet can access them and make use of them.
Is that a valid approach and what can be possible errors or limitation of that?

Thanks.
 
Eric Fletcher
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's likely to cause some concurrent access isuses, for the beans anyway. If all your beans are application scope, then you should probably synchronize all the public methods.
Does the app neccessitate that all your beans are application scope? In my experience, session scope is generally more useful(although not without its own issues!) :roll:
Cheers
E
 
Vikas Aggarwal
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Eric,
That's what I have been suspecting. I can do 2 things now.Either make the shared objects Synchronized or make the public methods in the coprresponding beans Synchronized. Which one is better?
Thanks.
 
Eric Fletcher
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmmm... Good question. Personally I generally synchronized the public methods to shared resources, but your question of which is better prompted me to do a little digging. Here is a good link I found on the differences(not neccesarily the relative merits) of synchronizing method vs. objects.
http://www-106.ibm.com/developerworks/java/library/praxis/pr46.html
Take a look.
Either way, synchronizing can affect performance, which is whay I asked earlier if you could get away with using a less broad scope.
Hope this helps.
Cheers,
E
 
Vikas Aggarwal
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I think synchronizing the public method will do good. What do you say? Actually I have read some where that if you have to do the synchronization do at the lowest possible level. What's your opinion?
 
Eric Fletcher
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's true, if you have to synchronize its best to do it at the lowest level.
And since you are truly sharing objects as opposed to creating a new instance with each thread, I agree that synch'ing the methods is the better way to go.
Good luck, let me know how it turns out! irwinm@telerama.com
E
 
Vikas Aggarwal
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have just gone through this line on the URL you sent to . This is very good article. I have run into some problem and will try to synchronize the class objects using a method in the class itself.
public static Header getInstance() {
Header _instance =null;
if (_instance == null) {
synchronized (Header.class) {
if (_instance == null) {
_instance = new Header();
}
}
}
return _instance;
}
I will sure let you know about it.
vikinsa@vikinsa.com
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic