• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connection Pool Design Issue

 
Tiger Scott
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tiger Scott
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic