Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regarding connection pooling

 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As we all know the concept of connection pooling.I have a doubt for that.
If connection pooling would not have been done by container,how would we have implemented ,i mean the ways in which we can implement this thing and how.This was asked in an interview.
 
Muth Rama
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way I would say, create a n(say 50 or something) number of connection objects and keep it in some queue(I mean some collection) objects. Then whenever the database connection needed by application, get the available connection object from that queue and use it. Once the need over just give back the object to the queue and make it free. So that next time anyone can use that. This is way connection pooling also working.
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rama,

Your reply seems good.But,can there be some more efficient way than this.This question is for all but specially for ranchers who have not told anything till now.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "more efficient"? Getting an object from a pool has very little overhead. The pool itself generally includes a low-priority background thread that checks connections for liveness, and opens or closes them as needed. That hardly seems worth worrying about.
 
Andy Grove
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are many open source connection pool libraries that you can use rather than implementing your own connection pool. Apache DBCP is a popular one. You can use this in your code regardless of whether your code is deployed in a container or not.

Hope that helps.
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One approach makes clients aware of the pool. Instead of doing the normal get connection and close connection they have to do pool.get() and pool.return(). You can use a very simple object pool made out of a blocking queue. Load it up with connections at startup and let people get and return them all day long. Wrap it up with more code if you need to grow and shrink the pool.

A much more elaborate approach hides the pool behind the normal methods. When you ask the DriverManager for a new connection, you actually get one from the pool. When call close, it actually puts itself back in the pool. You can write one of these, too, but it's a lot of work to get right.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic