aspose file tools*
The moose likes JDBC and the fly likes How do you write a DB Connection Pool class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How do you write a DB Connection Pool class" Watch "How do you write a DB Connection Pool class" New topic
Author

How do you write a DB Connection Pool class

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4639
    
    5

I know, if you use servlets, you can rely upon the container (Tomcat, etc.) to do connection pooling. But I want to write library objects that can be
used both in servlets and in stand alone utility programs. So far, that means I have to write a connection pool for when there is no container.

At first, I thought that I'd keep a Map of Connections that I hand out, and return them to the available pool when the Connection is returned. But I can't figure out what to use as the key to the Map, as the Connection is not immutable and I can't be sure that the hashCode() of the Connection is constant.

Any ideas?
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3456
    
  47

There is IdentityHashMap, it seems to fit your needs quite well.

I'd consider creating an interface with methods to obtain and close the connection. Users of your library would then provide implementation through which your library would manipulate the connections (I'm sure there is some design pattern which corresponds to this setup). Or better yet, let every method in your library which needs it accept a connection as a parameter, that way the caller can decide in which context (eg. as part of a wider transaction, or with specific isolation level) does he want your method to be executed.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18168
    
    8

Why not use a Set instead of a Map? I don't see the need for a key at all. If you make sure that no two Connections are equal, by not implementing the equals() method, then you shouldn't run into any problems. Or am I missing something?
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4639
    
    5

A set is probably sufficient. I'm keeping the Date last used as the value. If its "too stale" I want to discard the Connection.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4639
    
    5

Of course, many (most?) Set implementations are simply wrappers on Map, with no values stored. It "wastes" one reference per entry, which is nearly always trivial, and it allows one set of code to do two functions.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19557
    
  16

Pat Farrell wrote:Of course, many (most?) Set implementations are simply wrappers on Map, with no values stored.

I know two exceptions: EnumSet (uses an Enum[] + long (RegularEnumSet) / long[] (JumboEnumSet)) and CopyOnWriteArraySet (uses an internal CopyOnWriteArrayList instead of a Map).

However, most Set implementations do use a value, so they can use get / remove and use null to indicate the value was not present. This value is mostly Boolean.TRUE or a shared Object.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do you write a DB Connection Pool class
 
Similar Threads
What is Connection pool?
how to use jts in weblogic 6.0
Hibernate dialect
best way to handle database connection
Re: Closing database connection in destroy metod()?