• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How are Servlets and objects in it instantiated?

 
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So if I made a single connection object to be used by the whole application would that be a problem?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With regards to connections, it's best to rely on the container-managed connection pool. Are you using Tomcat?
 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's extensively covered in the Tomcat documentation. See previous discussions in the Tomcat forum for more details.
 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bear! I'll take a look at it right away.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic