aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Connection pooling design pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Connection pooling design pattern" Watch "Connection pooling design pattern" New topic
Author

Connection pooling design pattern

Ann Sebastian
Ranch Hand

Joined: Sep 21, 2005
Posts: 37
Which design pattern is generally used for connection pooling?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I'm confused - why do you want to know???


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

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.!


Customer surveys are for companies who didn't pay proper attention to begin with.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Jun 25, 2001
Posts: 16145
    
  21

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

Joined: Oct 25, 2005
Posts: 14
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Connection pooling design pattern