3

I would like to use a PoolingDataSource as my connection pool (API at: http://commons.apache.org/dbcp/apidocs/org/apache/commons/dbcp/PoolingDataSource.html), but I don't know what to do with the pool when I no longer need it. What if I want to connect to a new database and don't need the connections in the old pool anymore? There is no close method on the pool.

Buttons840
  • 9,239
  • 15
  • 58
  • 85

3 Answers3

3

You don't necessarily need to kill this pool to create a new one.

You can manage the connections in it using the maxIdle, timeBetweenEvictionRunsMillis and minEvictableIdleTimeMillis parameters (see here) to ensure idle connections get closed in a reasonable time.

Or you can configure a GenericObjectPool with those parameters programatically and use when creating your PoolingDataSource. That has a close() method if you want to force it.

Brian Smith
  • 3,383
  • 30
  • 41
2

Sorry for not answering your question directly, but may I recommend not using DBCP? It has had a number of serious problems, which other libraries learned from and improved upon.

There are much better pools out there.

Community
  • 1
  • 1
erickson
  • 265,237
  • 58
  • 395
  • 493
  • 1
    While fairly accurate; it should be noted DBCP has been actively maintained since this answer was provided. – Brett Ryan May 09 '16 at 07:27
1

What kind of pool are you using? If you're using the AbandonedObjectPool, then that's a subclass of Commons Pool's GenericObjectPool, which has a close() method.

Tom Anderson
  • 46,189
  • 17
  • 92
  • 133