aspose file tools*
The moose likes Servlets and the fly likes How are Servlets and objects in it instantiated? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How are Servlets and objects in it instantiated?" Watch "How are Servlets and objects in it instantiated?" New topic
Author

How are Servlets and objects in it instantiated?

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Is a servlet instantiated one per user? Or one for the whole application to use? How about objects inside the servlet? Suppose I have a static object in a servlet, is it static for one user or static for the whole application? Thanks!


SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by Timothy Sam:
Is a servlet instantiated one per user? Or one for the whole application to use? How about objects inside the servlet? Suppose I have a static object in a servlet, is it static for one user or static for the whole application? Thanks!


One servlet per application but one thread per user.The variables in servlet both static and non static are not thread safe and there for one instance is shared by the complete servlet.


Rahul Bhattacharjee
LinkedIn - Blog
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
So if I made a single connection object to be used by the whole application would that be a problem?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41508
    
  53
Yes, it would mean that all acesses to the database are serialized through that object. Also, if any of the threads used transactions, and failed to handle them properly, there would be big-time trouble ahead.


Ping & DNS - my free Android networking tools app
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hmmm... This is bad... I've been doing this for almost all my applications. I'm doing this to an app that is to be published over the internet. So what could ba a good solution then? I'm thinking of the following...


1. Create a single connection for each thread, determine if that thread already has a connection object and if not instantiate one...

2. Just instantiate a connection for every DB transaction.


Aaaaaak! I didn't imagine this to be such a problem!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

With regards to connections, it's best to rely on the container-managed connection pool. Are you using Tomcat?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Yes, I am using Tomcat. I saw a sample connection Pool class over this link

http://archive.coreservlets.com/Chapter18.html


Could you please refer me to links about container-managed connection pool? I happen to have a class which I plan to twist and follow the DAO pattern. Will container-managed connection pool adapt to this? Thanks!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

It's extensively covered in the Tomcat documentation. See previous discussions in the Tomcat forum for more details.
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Thanks Bear! I'll take a look at it right away.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How are Servlets and objects in it instantiated?