File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Objects In Servlet Context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Objects In Servlet Context" Watch "Objects In Servlet Context" New topic
Author

Objects In Servlet Context

Vikas Aggarwal
Ranch Hand

Joined: Jun 22, 2001
Posts: 140
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.


Vikas Aggarwal

Founder @
Leads and Deals Limited

www.LeadsAndDeals.com
Eric Fletcher
Ranch Hand

Joined: Oct 26, 2000
Posts: 188
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


My theory of evolution is that Darwin was adopted. - Steven Wright
Vikas Aggarwal
Ranch Hand

Joined: Jun 22, 2001
Posts: 140
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

Joined: Oct 26, 2000
Posts: 188
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

Joined: Jun 22, 2001
Posts: 140
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

Joined: Oct 26, 2000
Posts: 188
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

Joined: Jun 22, 2001
Posts: 140
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Objects In Servlet Context