org.postgresql.ds.jdbc23
Class AbstractJdbc23PoolingDataSource

java.lang.Object
  extended by org.postgresql.ds.common.BaseDataSource
      extended by org.postgresql.ds.jdbc23.AbstractJdbc23PoolingDataSource
All Implemented Interfaces:
Referenceable
Direct Known Subclasses:
AbstractJdbc4PoolingDataSource

public abstract class AbstractJdbc23PoolingDataSource
extends BaseDataSource

DataSource which uses connection pooling. Don't use this if your server/middleware vendor provides a connection pooling implementation which interfaces with the PostgreSQL ConnectionPoolDataSource implementation! This class is provided as a convenience, but the JDBC Driver is really not supposed to handle the connection pooling algorithm. Instead, the server or middleware product is supposed to handle the mechanics of connection pooling, and use the PostgreSQL implementation of ConnectionPoolDataSource to provide the connections to pool.

If you're sure you want to use this, then you must set the properties dataSourceName, databaseName, user, and password (if required for the user). The settings for serverName, portNumber, initialConnections, and maxConnections are optional. Note that only connections for the default user will be pooled! Connections for other users will be normal non-pooled connections, and will not count against the maximum pool size limit.

If you put this DataSource in JNDI, and access it from different JVMs (or otherwise load this class from different ClassLoaders), you'll end up with one pool per ClassLoader or VM. This is another area where a server-specific implementation may provide advanced features, such as using a single pool across all VMs in a cluster.

This implementation supports JDK 1.3 and higher.

Author:
Aaron Mulder (ammulder@chariotsolutions.com)

Field Summary
protected  String dataSourceName
           
protected static Map dataSources
           
 
Constructor Summary
AbstractJdbc23PoolingDataSource()
           
 
Method Summary
protected abstract  void addDataSource(String dataSourceName)
           
 void close()
          Closes this DataSource, and all the pooled connections, whether in use or not.
protected  PGConnectionPoolDataSource createConnectionPool()
          Creates the appropriate ConnectionPool to use for this DataSource.
 Connection getConnection()
          Gets a connection from the connection pool.
 Connection getConnection(String user, String password)
          Gets a non-pooled connection, unless the user and password are the same as the default values for this connection pool.
static PGPoolingDataSource getDataSource(String name)
           
 String getDataSourceName()
          Gets the name of this DataSource.
 String getDescription()
          Gets a description of this DataSource.
 int getInitialConnections()
          Gets the number of connections that will be created when this DataSource is initialized.
 int getMaxConnections()
          Gets the maximum number of connections that the pool will allow.
 Reference getReference()
          Adds custom properties for this DataSource to the properties defined in the superclass.
 void initialize()
          Initializes this DataSource.
protected  boolean isInitialized()
           
protected  void removeStoredDataSource()
           
 void setDatabaseName(String databaseName)
          Ensures the DataSource properties are not changed after the DataSource has been used.
 void setDataSourceName(String dataSourceName)
          Sets the name of this DataSource.
 void setInitialConnections(int initialConnections)
          Sets the number of connections that will be created when this DataSource is initialized.
 void setMaxConnections(int maxConnections)
          Sets the maximum number of connections that the pool will allow.
 void setPassword(String password)
          Ensures the DataSource properties are not changed after the DataSource has been used.
 void setPortNumber(int portNumber)
          Ensures the DataSource properties are not changed after the DataSource has been used.
 void setServerName(String serverName)
          Ensures the DataSource properties are not changed after the DataSource has been used.
 void setUser(String user)
          Ensures the DataSource properties are not changed after the DataSource has been used.
 
Methods inherited from class org.postgresql.ds.common.BaseDataSource
createReference, getApplicationName, getCompatible, getDatabaseName, getLoginTimeout, getLogLevel, getLogWriter, getPassword, getPortNumber, getPrepareThreshold, getProtocolVersion, getServerName, getSocketTimeout, getSsl, getSslfactory, getTcpKeepAlive, getUnknownLength, getUser, initializeFrom, readBaseObject, setApplicationName, setCompatible, setLoginTimeout, setLogLevel, setLogWriter, setPrepareThreshold, setProtocolVersion, setSocketTimeout, setSsl, setSslfactory, setTcpKeepAlive, setUnknownLength, writeBaseObject
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dataSources

protected static Map dataSources

dataSourceName

protected String dataSourceName
Constructor Detail

AbstractJdbc23PoolingDataSource

public AbstractJdbc23PoolingDataSource()
Method Detail

getDataSource

public static PGPoolingDataSource getDataSource(String name)

getDescription

public String getDescription()
Gets a description of this DataSource.

Specified by:
getDescription in class BaseDataSource

setServerName

public void setServerName(String serverName)
Ensures the DataSource properties are not changed after the DataSource has been used.

Overrides:
setServerName in class BaseDataSource
Throws:
IllegalStateException - The Server Name cannot be changed after the DataSource has been used.

setDatabaseName

public void setDatabaseName(String databaseName)
Ensures the DataSource properties are not changed after the DataSource has been used.

Overrides:
setDatabaseName in class BaseDataSource
Throws:
IllegalStateException - The Database Name cannot be changed after the DataSource has been used.

setUser

public void setUser(String user)
Ensures the DataSource properties are not changed after the DataSource has been used.

Overrides:
setUser in class BaseDataSource
Throws:
IllegalStateException - The User cannot be changed after the DataSource has been used.

setPassword

public void setPassword(String password)
Ensures the DataSource properties are not changed after the DataSource has been used.

Overrides:
setPassword in class BaseDataSource
Throws:
IllegalStateException - The Password cannot be changed after the DataSource has been used.

setPortNumber

public void setPortNumber(int portNumber)
Ensures the DataSource properties are not changed after the DataSource has been used.

Overrides:
setPortNumber in class BaseDataSource
Throws:
IllegalStateException - The Port Number cannot be changed after the DataSource has been used.

getInitialConnections

public int getInitialConnections()
Gets the number of connections that will be created when this DataSource is initialized. If you do not call initialize explicitly, it will be initialized the first time a connection is drawn from it.


setInitialConnections

public void setInitialConnections(int initialConnections)
Sets the number of connections that will be created when this DataSource is initialized. If you do not call initialize explicitly, it will be initialized the first time a connection is drawn from it.

Throws:
IllegalStateException - The Initial Connections cannot be changed after the DataSource has been used.

getMaxConnections

public int getMaxConnections()
Gets the maximum number of connections that the pool will allow. If a request comes in and this many connections are in use, the request will block until a connection is available. Note that connections for a user other than the default user will not be pooled and don't count against this limit.

Returns:
The maximum number of pooled connection allowed, or 0 for no maximum.

setMaxConnections

public void setMaxConnections(int maxConnections)
Sets the maximum number of connections that the pool will allow. If a request comes in and this many connections are in use, the request will block until a connection is available. Note that connections for a user other than the default user will not be pooled and don't count against this limit.

Parameters:
maxConnections - The maximum number of pooled connection to allow, or 0 for no maximum.
Throws:
IllegalStateException - The Maximum Connections cannot be changed after the DataSource has been used.

getDataSourceName

public String getDataSourceName()
Gets the name of this DataSource. This uniquely identifies the DataSource. You cannot use more than one DataSource in the same VM with the same name.


setDataSourceName

public void setDataSourceName(String dataSourceName)
Sets the name of this DataSource. This is required, and uniquely identifies the DataSource. You cannot create or use more than one DataSource in the same VM with the same name.

Throws:
IllegalStateException - The Data Source Name cannot be changed after the DataSource has been used.
IllegalArgumentException - Another PoolingDataSource with the same dataSourceName already exists.

initialize

public void initialize()
                throws SQLException
Initializes this DataSource. If the initialConnections is greater than zero, that number of connections will be created. After this method is called, the DataSource properties cannot be changed. If you do not call this explicitly, it will be called the first time you get a connection from the DataSource.

Throws:
SQLException - Occurs when the initialConnections is greater than zero, but the DataSource is not able to create enough physical connections.

isInitialized

protected boolean isInitialized()

createConnectionPool

protected PGConnectionPoolDataSource createConnectionPool()
Creates the appropriate ConnectionPool to use for this DataSource.


getConnection

public Connection getConnection(String user,
                                String password)
                         throws SQLException
Gets a non-pooled connection, unless the user and password are the same as the default values for this connection pool.

Overrides:
getConnection in class BaseDataSource
Returns:
A pooled connection.
Throws:
SQLException - Occurs when no pooled connection is available, and a new physical connection cannot be created.

getConnection

public Connection getConnection()
                         throws SQLException
Gets a connection from the connection pool.

Overrides:
getConnection in class BaseDataSource
Returns:
A pooled connection.
Throws:
SQLException - Occurs when no pooled connection is available, and a new physical connection cannot be created.

close

public void close()
Closes this DataSource, and all the pooled connections, whether in use or not.


removeStoredDataSource

protected void removeStoredDataSource()

addDataSource

protected abstract void addDataSource(String dataSourceName)

getReference

public Reference getReference()
                       throws NamingException
Adds custom properties for this DataSource to the properties defined in the superclass.

Specified by:
getReference in interface Referenceable
Overrides:
getReference in class BaseDataSource
Throws:
NamingException


Copyright © 2013. All Rights Reserved.