How do I close a java.sql.DataSource
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 D开发者_开发百科atasource wrapped in a C3P0 connection poolDataSource
.
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.
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?
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.
Looks like if you use C3PO pooled DataSources, then you can release threads and connections associated with the DataSource using DataSources.destroy(DataSource).
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();
精彩评论