D
David McDivitt
Subject: Re: pooled connection myth
To repeat what I said in another post just sent, I appreciate the time you,
John, Lee, and Chris took to answer my questions.
Date: Fri, 18 Mar 2005 13:42:44 -0500
Do not confuse a Driver object (which acts as a factory for Connections)
with a whole JDBC driver, which encompasses at least one implementation
of Driver, Connection, Statement, ResultSet, and several other
interfaces, along with supporting classes. Once a Driver has created a
Connection, it need not have anything to do with communication via that
Connection, so a Driver object's thread safety is unrelated to the
thread safety of the Connections it hands out. If you want to talk
about thread safety of the whole driver (lowercase 'd'), however, then
of course you have to include the Connection implementation(s) in the
discussion.
A key difference here between a Driver and a Connection is that Drivers
are stateless, whereas Connections are explicitly stateful. (The
Connection interface defines methods for manipulation of persistent
state.) Connections' statefulness is what makes them unsuitable for
concurrent use by multiple threads, even if they internally serialize
individual operations. This is much the same reason that the
synchronized collections (Vector, Hashtable,
Collections.synchronizedXXX(foo)) are not safe for concurrent use: the
synchronization / serialization ensures that the objects' internal state
remains consistent, but that's not sufficient to shield one thread from
the effects of other threads' manipulation of the objects.
If you know what you can and cannot safely do, then yes, you can write
code that shares Connections among multiple threads and never have a
problem. There may or may not be performance consequences relative to
using a connection pool (in either direction, but probably not too
significant). The fact that you must know and abide by nontrivial
restrictions on the use of the shared Connections makes that approach
anything but transparent to Connection users, however, and the fact that
the approach provides fertile ground for concurrency bugs should strike
terror into your heart.
Alternatively, your JDBC driver likely comes with a connection pool
implementation that you can use out of the box. If it doesn't then you
can still get one from a third party and thereby not have to maintain
your own connection manager. Users of the Connections provided by such
a pool do not need to worry about any restrictions on their normal use
of the connections, and there are no special concurrency concerns
involved. A connection pool satisfies exactly the same performance
considerations that you shared connection mechanism does, and moreover
can provide resource management hooks that the shared connection
mechanism does not provide. Tell me again, then, what were the relative
advantages of shared connections?
You seem to be very determined to believe that your shared connection
plan is good, so I suspect that none of this has persuaded you. You are
free to believe whatever you want to believe on the topic, and I am
finished trying to tell you otherwise.
To repeat what I said in another post just sent, I appreciate the time you,
John, Lee, and Chris took to answer my questions.