Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes Connection Pool Design Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Connection Pool Design Issue" Watch "Connection Pool Design Issue" New topic
Author

Connection Pool Design Issue

Tiger Scott
Ranch Hand

Joined: Mar 01, 2001
Posts: 223
I have a search Servlet. The init method creates a search class which is an insatnce on the servlet. If a method is invoked on the servlet it will invoke it on the search class. The Search class gets a connection from the pool and returns it back at the end of the method completion.
Is this an appropriate design? My question is if the web server creates several insatnces of the servlet(will it?)- will the init method be invoked each time- I would want that- as I want each insatnce of the servlet to have its own search class?
If this is OK. How and when do I close the connections or my connection pool?
Thanks a lot guys,
Sanjay
George Pavamani
Ranch Hand

Joined: Jan 31, 2002
Posts: 31
Hey Sanjay,
The Servlet container will create several instances of the Servlet only when you implement SingleThreadModel, otherwise only one instance of the servlet is created to handle all the requests.
And also when you are not implementing the SingleThreadModel, the init() is called only once before the first request is handled.
I hope this message is of some help to you.
George
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
More importantly -- why are you reinventing the wheel? Most application servers already have JDBC 2.0-compliant connection pools, and even if you're using something like Tomcat you can download Apache Struts and get a connection pool implementation for free...
Kyle


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Tiger Scott
Ranch Hand

Joined: Mar 01, 2001
Posts: 223
I will look at the struts- for the connection pool.
In any case- should I implement the Single Thread Model- if that is the only way get several instances of servlet?
Thanks a lot,
Sanjay
George Pavamani
Ranch Hand

Joined: Jan 31, 2002
Posts: 31
I don't think it is a good idea to implement SingleThreadModel , if your sole purpose is to obtain a seperate Connection for each request. The single thread model might slow down your process.Instead, why don't you have a helper class that would fetch you a connection for each request and use the instance of this class in the doget or dopost method. Please correct me if i am wrong
Thanks
George
Tiger Scott
Ranch Hand

Joined: Mar 01, 2001
Posts: 223
My idea is to use a helper class and that is what I am trying to do. if you could go thru my first mail and see if am achieving that or not.
Thanks
Sanjay
Tiger Scott
Ranch Hand

Joined: Mar 01, 2001
Posts: 223
Anybody has further ideas on this. I think by not using the SingleThreadModel I am not using the pool- as I have only one instance of the helper class- that gets only one connection from the pool.
Thanks again,
Sanjay
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
There may be only one servlet object but each Thread handling a request will have its own local variables - including a connection from a connection pool if so desired.
Bill
Tiger Scott
Ranch Hand

Joined: Mar 01, 2001
Posts: 223
Thanks.
The way I was implementing it was to create my helper class in the init() method- if that be the case I should have only one instance of the helper class for one instance of the Servlet?
TIA
Sanjy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Connection Pool Design Issue