Applications: DataSource

PostgreSQL™ includes two implementations of DataSource for JDBC 2 and two for JDBC 3, as shown in Table 11.3, “DataSource Implementations”. The pooling implementations do not actually close connections when the client calls the close method, but instead return the connections to a pool of available connections for other clients to use. This avoids any overhead of repeatedly opening and closing connections, and allows a large number of clients to share a small number of database connections.

The pooling data-source implementation provided here is not the most feature-rich in the world. Among other things, connections are never closed until the pool itself is closed; there is no way to shrink the pool. As well, connections requested for users other than the default configured user are not pooled. Many application servers provide more advanced pooling features and use the ConnectionPoolDataSource implementation instead.

All the implementations use the same configuration scheme. JDBC requires that a DataSource be configured via JavaBean properties, shown in Table 11.4, “DataSource Configuration Properties”, so there are get and set methods for each of these properties.

The pooling implementations require some additional configuration properties, which are shown in Table 11.5, “Additional Pooling DataSource Configuration Properties”.

Example 11.1, “DataSource Code Example” shows an example of typical application code using a pooling DataSource.