While using the BoneCP connection pooling, I came across the following confusions and would like to hear some advise on this:
- Is the
getConnection
method of the BoneCP thread-safe? What is the best way to use this when there are many threads asking for a connection in parallel? - Is it required to call
connection.close()
every time after using the connection? - if it is required to call
connection.close()
, will it really disconnect the connection with the DB or just sends the connection to the pool?
Thanks in advance for the support.
The
getConnection()
of BoneCP is thread-safe; so you don't have to do anything yourself. And yes, you need to callconnection.close()
if you are done with the connection (this is not specific to BoneCP, but applies to any JDBC connection).As with all connection pools, calling
connection.close()
will return the connection to the connection pool or in some cases also close the physical connection (but really: this is an implementation detail you should not be concerned about).In general connection pools maintain a pool of physical connections to a database. When you call
getConnection()
, the connection pool looks for an available physical connection and wrap it in a logical connection. This logical connection is returned. When youclose()
the logical connection, the connection pool knows the physical connection is available again for reuse.