12

We have a system where data is partitioned by date.
So, for example, in SqlServer we have one database per month of data. Each month partition uses a Jdbc driver Datasource wrapped in a C3P0 connection pool DataSource.

After a period of time the date range of the partition becomes old enough that we want to offline it. In that case we just remove the relevant month's DataSource from the available list.
However, ideally, when offlining I would like to "close" the DataSource so the pool relinquishes all connections to the DB.

DataSource has no close method for me to call so I'm not sure how to clean this up.

Any suggestions?

Mike Q
  • 22,839
  • 20
  • 87
  • 129

3 Answers3

13

You don't close a DataSource - you close the connection returned by a DataSource. The DataSource itself is never "open" as such.

I would expect the pool to relinquish open connections after a time-out anyway, so I suggest you just don't worry about it :) If you need to forcibly close the connections, you'll need to keep a reference to the connection pool itself and use any facilities supplied by c3p0 directly.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    I think the OP does not only want to close the data source, he wants to remove the whole C3P0 connection pool so no pooled connections to the now obsolete DB are managed anymore. – David Tanzer Jul 05 '11 at 08:38
  • @David: He's going to remove the DataSource from his own list, which will stop any more connections being created... but I thought he then wanted to any existing connections. – Jon Skeet Jul 05 '11 at 08:43
  • @Jon - to clarify I would expect there to be no active connections outside the pool just the idle connections in the pool itself which I want to be closed. I will check to see if I can configure it such that idle connections will drop eventually – Mike Q Jul 05 '11 at 09:05
  • @JonSkeet can you please tell me that if I invoke close method like this: `dataSource().getConnection().close();` would this also close preparedstatements that I opened and Result set along with the connection? – Faraz Nov 22 '19 at 23:16
  • @Faraz: I don't know, to be honest. – Jon Skeet Nov 22 '19 at 23:19
9

Looks like if you use C3PO pooled DataSources, then you can release threads and connections associated with the DataSource using DataSources.destroy(DataSource).

ewan.chalmers
  • 16,145
  • 43
  • 60
2

In my case, @sudocode's answer didn't work, so I've obtained the pooled Datasource and I closed it:

ComboPooledDataSource datasource =
                (ComboPooledDataSource) envContext.lookup("jdbc/oracledb");
datasource.close();
jelies
  • 9,110
  • 5
  • 50
  • 65