如何关闭 java.sql.DataSource

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6579831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:28:06  来源:igfitidea点击:

How do I close a java.sql.DataSource

javadatasourcec3p0

提问by Mike Q

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.

我们有一个系统,其中数据按日期分区。因此,例如在 SqlServer 中,我们每个月有一个数据库的数据。每个月的分区使用一个Jdbc 驱动Datasource 包裹在一个C3P0 连接池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。但是,理想情况下,离线时我想“关闭”数据源,以便池放弃与数据库的所有连接。

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

DataSource 没有我可以调用的 close 方法,所以我不确定如何清理它。

Any suggestions?

有什么建议?

采纳答案by sudocode

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

看起来如果您使用 C3PO池化数据源,那么您可以使用DataSources.destroy(DataSource)释放与数据关联的线程和连接。

回答by Jon Skeet

You don't close a DataSource- you close the connection returnedby a DataSource. The DataSourceitself is never "open" as such.

您没有关闭 a DataSource- 您关闭了a返回的连接DataSource。在DataSource本身是从来没有“开放”本身。

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.

无论如何,我希望池在超时后放弃打开的连接,所以我建议您不要担心:) 如果您需要强行关闭连接,则需要保留对连接池的引用本身并直接使用 c3p0 提供的任何设施。

回答by jelies

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

就我而言,@sudocode 的回答不起作用,所以我获得了汇集的数据源并关闭了它:

ComboPooledDataSource datasource =
                (ComboPooledDataSource) envContext.lookup("jdbc/oracledb");
datasource.close();