com.mchange.v2.c3p0

Class DataSources

public final class DataSources extends Object

A simple factory class for creating DataSources. Generally, users will call DataSources.unpooledDataSource() to get a basic DataSource, and then get a pooled version by calling DataSources.pooledDataSource().

Most users will not need to worry about configuration details. If you want to use a PreparedStatement cache, be sure to call the version of DataSources.pooledDataSource() that accepts a statement_cache_size parameter, and set that to be a number (much) greater than zero. (For maximum performance, you would set this to be several times the number kinds of PreparedStatements you expect your application to use.)

For those interested in detailed configuration, note that c3p0 pools can be configured by explicit method calls on PoolConfig objects, by defining System properties, or by defining a c3p0.properties file in your resource path. See PoolConfig for details.

Method Summary
static voiddestroy(DataSource pooledDataSource)

Immediately releases any unshared resources (Threads and database Connections) that are held uniquely by any DataSource created by the DataSources class.

static voidforceDestroy(DataSource pooledDataSource)

Should be used only with great caution.

static DataSourcepooledDataSource(DataSource unpooledDataSource)

Creates a pooled version of an unpooled DataSource using default configuration information.

NOTE: By default, statement pooling is turned off, because for simple databases that do not pre-parse and optimize PreparedStatements, statement caching is a net performance loss.

static DataSourcepooledDataSource(DataSource unpooledDataSource, int statement_cache_size)

Creates a pooled version of an unpooled DataSource using default configuration information and the specified startement cache size.

static DataSourcepooledDataSource(DataSource unpooledDataSource, PoolConfig pcfg)

Creates a pooled version of an unpooled DataSource using configuration information supplied explicitly by a PoolConfig.

static DataSourcepooledDataSource(DataSource unpooledDataSource, Properties props)

Creates a pooled version of an unpooled DataSource using configuration information supplied explicitly by a Java Properties object.

static DataSourceunpooledDataSource(String jdbcUrl)
Defines an unpooled DataSource on the specified JDBC URL.
static DataSourceunpooledDataSource(String jdbcUrl, String user, String password)
Defines an unpooled DataSource on the specified JDBC URL, authenticating with a username and password.
static DataSourceunpooledDataSource(String jdbcUrl, Properties driverProps)
Defines an unpooled DataSource on the specified JDBC URL.

Method Detail

destroy

public static void destroy(DataSource pooledDataSource)

Immediately releases any unshared resources (Threads and database Connections) that are held uniquely by any DataSource created by the DataSources class. Any resources shared with other, undestroyed DataSources will remain open. If this method is not called, resources will be cleaned up when the DataSource is unreferenced and finalized.

Only DataSources created by the poolingDataSource() method hold any non-memory resources. Calling this method on unpooled DataSources is effectively a no-op.

You can safely presume that destroying a pooled DataSource that is wrapped around another DataSource created by this library destroys both the outer and the wrapped DataSource. There is no reason to hold a reference to a nested DataSource in order to explicitly destroy it.

See Also: PoolConfig

forceDestroy

public static void forceDestroy(DataSource pooledDataSource)

Should be used only with great caution. Immediately destroys any pool and cleans up all resources this DataSource may be using, even if other DataSources are sharing that pool! In general, it is difficult to know whether a pool is being shared by multiple DataSources. It may depend upon whether or not a JNDI implementation returns a single instance or multiple copies upon lookup (which is undefined by the JNDI spec).

In general, this method should be used only when you wish to wind down all c3p0 pools in a ClassLoader. For example, when shutting down and restarting a web application that uses c3p0, you may wish to kill all threads making use of classes loaded by a web-app specific ClassLoader, so that the ClassLoader can be cleanly garbage collected. In this case, you may wish to use force destroy. Otherwise, it is much safer to use the simple destroy() method, which will not shut down pools that may still be in use.

To close a pool normally, use the simple destroy method instead of forceDestroy.

See Also: DataSources

pooledDataSource

public static DataSource pooledDataSource(DataSource unpooledDataSource)

Creates a pooled version of an unpooled DataSource using default configuration information.

NOTE: By default, statement pooling is turned off, because for simple databases that do not pre-parse and optimize PreparedStatements, statement caching is a net performance loss. But if your database does optimize PreparedStatements you'll want to turn StatementCaching on via DataSources.

Returns: a DataSource that can be cast to a PooledDataSource if you are interested in pool statistics

pooledDataSource

public static DataSource pooledDataSource(DataSource unpooledDataSource, int statement_cache_size)

Creates a pooled version of an unpooled DataSource using default configuration information and the specified startement cache size. Use a value greater than zero to turn statement caching on.

Returns: a DataSource that can be cast to a PooledDataSource if you are interested in pool statistics

pooledDataSource

public static DataSource pooledDataSource(DataSource unpooledDataSource, PoolConfig pcfg)

Creates a pooled version of an unpooled DataSource using configuration information supplied explicitly by a PoolConfig.

Returns: a DataSource that can be cast to a PooledDataSource if you are interested in pool statistics

pooledDataSource

public static DataSource pooledDataSource(DataSource unpooledDataSource, Properties props)

Creates a pooled version of an unpooled DataSource using configuration information supplied explicitly by a Java Properties object.

Returns: a DataSource that can be cast to a PooledDataSource if you are interested in pool statistics

See Also: PoolConfig

unpooledDataSource

public static DataSource unpooledDataSource(String jdbcUrl)
Defines an unpooled DataSource on the specified JDBC URL.

unpooledDataSource

public static DataSource unpooledDataSource(String jdbcUrl, String user, String password)
Defines an unpooled DataSource on the specified JDBC URL, authenticating with a username and password.

unpooledDataSource

public static DataSource unpooledDataSource(String jdbcUrl, Properties driverProps)
Defines an unpooled DataSource on the specified JDBC URL.

Parameters: driverProps the usual DriverManager properties for your JDBC driver (e.g. "user" and "password" for all drivers that support authentication)

See Also: java.sql.DriverManager