Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connection pooling design pattern

 
Ann Sebastian
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which design pattern is generally used for connection pooling?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused - why do you want to know???
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18276
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean to use a connection pool, or to implement a connection pool?

From a user's point of view, a connection pool is just a resource factory. If you're implementing on, it's pretty much based on what mechanisms you want/need to employ. Provided that to the consumer you're a connection factory.!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's just say "design" rather than "pattern". Pools may be common enough to be patterns, but I'm not sure I've seen them in full-blown pattern language.

I've seen a couple approaches to connection pools. One makes all users aware they are using a pool. I like this because it's simple even though it impacts the client. You can use a generic off-the-shelf object pool:

The other approach hides the pooling. I'm not sure how they get connections; I used snipped ConnectionManager from a toy project.

This is a lot more work as you have to extend or wrap Connection to return to the pool instead of really closing and much more.

Does that much track to your line of questioning? There's more to talk about ... policies for growing and shrinking the pool, detecting stale connections, etc.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
The other approach hides the pooling. I'm not sure how they get connections; I used snipped ConnectionManager from a toy project.

This is a lot more work as you have to extend or wrap Connection to return to the pool instead of really closing and much more.


That's the Proxy design pattern. It's how the Jakarta Commons connection pool is implemented, for example. And it's not even that hard, if you know how to use Java's dynamic proxies...
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18276
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically, the connection manager is, like I said, a factory. The "connection" you get back is, however a proxy for the real connection. Done that way so that the proxy connection close() method will simply return the proxied connection back to the pool instead of destroying it.
 
Raj Murthy
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Connection pooling (in the context of 'connection' referring to a connection to a service type such as database etc.) is specialization of object-pool pattern, a creational design pattern

An Object Pool manages the reuse of objects for a type of object that is expensive to create or only a limited number of a kind of object can be created. These instances of a class can be used interchangeably.

This can be used inconjunction with Factory-Method, Singleton and cache-management patterns.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic