Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 942
Mike Q Avatar asked Jul 05 '11 08:07

Mike Q


People also ask

How do I close a JDBC connection?

Best approach to close JDBC connection objectThe rs. close() closes the ResultSet object and doesn't allow to process the ResultSet object further. The st. close() closes the Statement object that is we can't send further queries to database software using that statement object.

Why should you close a JDBC connection after editing any details?

If you don't close it, it leaks, and ties up server resources. @EJP The connection itself might be thread-safe (required by JDBC), but the applications use of the connection is probably not threadsafe.

Why should we close database connections in Java?

Completely Open and Close A Database Connection If any database connection is open then it takes the resources of the database such as memory, cursors, locks , temporary tables all are are engaged. To release the connection object it is very important to close the database connection after it is used.

In which code block we close the database connection?

It is always better to close the database/resource objects after usage. Better close the connection, resultset and statement objects in the finally block. Until Java 7, all these resources need to be closed using a finally block.


1 Answers

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

like image 96
sudocode Avatar answered Oct 06 '22 22:10

sudocode