• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do you write a DB Connection Pool class

 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic