ProgramixGenericLib v5.0.1

com.programix.sql
Class DriverManagerConnectionSource

java.lang.Object
  extended by com.programix.sql.DriverManagerConnectionSource
All Implemented Interfaces:
ConnectionSource

public class DriverManagerConnectionSource
extends Object
implements ConnectionSource

An implementation of ConnectionSource that uses DriverManager to create database Connection's.

Configuration settings are specified by a ValueMap with settings such as:

 
 sql.connection.driver=org.postgresql.Driver
 sql.connection.url=jdbc:postgresql://localhost:5432/andrewdb
 sql.connection.username=someone
 sql.connection.password=something
 sql.connection.extras.ssl=true
 sql.connection.extras.loginTimeout=90
 

The required and additional settings are retrieved by code equivalent to this:

 ValueMap config = // the configuration settings initially passed in
 
 Class.forName(config.getString(DRIVER_KEY));
 
 String url = config.getString(URL_KEY);
  
 ValueMap conInfo = config.getNestedValueMap(EXTRAS_PREFIX);
 
 if ( config.containsKey(USERNAME_KEY) ) {
     conInfo.put("user", config.getString(USERNAME_KEY); 
 }
 
 if ( config.containsKey(PASSWORD_KEY) ) {
     conInfo.put("password", config.getString(PASSWORD_KEY); 
 }
 
 Connection con = DriverManager.getConnection(url, conInfo.getProperties());
 

Author:
Paul Hyde

Field Summary
static String DRIVER_KEY
          Required configuration key for the JDBC driver's classname.
static String EXTRAS_PREFIX
          Optional configuration key prefix for additional settings to use to connect to the database.
static String PASSWORD_KEY
          Optional configuration key for the password to use to connect to the database.
static String URL_KEY
          Required configuration key for the URL to use to connect to the database.
static String USERNAME_KEY
          Optional configuration key for the username to use to connect to the database.
 
Constructor Summary
DriverManagerConnectionSource(ValueMap config)
           
 
Method Summary
 Connection getConnection()
          Returns a Connection (potentially from a shared resource pool).
 Connection getConnection(long msTimeout)
          Returns a Connection (potentially from a shared resource pool).
 void shutdown()
          Call this when done with the ConnectionSource to allow any underlying allocated resources to be released.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DRIVER_KEY

public static final String DRIVER_KEY
Required configuration key for the JDBC driver's classname. The value of this constant is always:
 sql.connection.driver
 

See Also:
Constant Field Values

URL_KEY

public static final String URL_KEY
Required configuration key for the URL to use to connect to the database. The value of this constant is always:
 sql.connection.url
 

See Also:
Constant Field Values

USERNAME_KEY

public static final String USERNAME_KEY
Optional configuration key for the username to use to connect to the database. Although this key is optional, most connections require it. The value of this constant is always:
 sql.connection.username
 

See Also:
Constant Field Values

PASSWORD_KEY

public static final String PASSWORD_KEY
Optional configuration key for the password to use to connect to the database. Although this key is optional, most connections require it. The value of this constant is always:
 sql.connection.password
 

See Also:
Constant Field Values

EXTRAS_PREFIX

public static final String EXTRAS_PREFIX
Optional configuration key prefix for additional settings to use to connect to the database. These settings are retrieved as a nested map using code like this:
 ValueMap conInfo = config.getNestedValueMap(EXTRAS_PREFIX);
 
The value of this constant is always (note the trailing period):
 sql.connection.extras.
 

See Also:
Constant Field Values
Constructor Detail

DriverManagerConnectionSource

public DriverManagerConnectionSource(ValueMap config)
                              throws SQLException
Throws:
SQLException
Method Detail

getConnection

public Connection getConnection(long msTimeout)
                         throws SQLTimedOutException,
                                SQLInterruptedException,
                                SQLShutdownException,
                                SQLException
Description copied from interface: ConnectionSource
Returns a Connection (potentially from a shared resource pool). This connection may be checked out from a shared pool of connections or may be from some other source. When done with a Connection, be sure to close it to potentially return it to a pool. Due to the fact that this Connection might be in a shared pool, you must ensure that after you have called close, your code does not do anything more with this Connection or anything it created (like a Statement, or a ResultSet).

Calls to this method may block while waiting for a Connection if there is a maximum number of connections allowed in an underlying resource pool.

Specified by:
getConnection in interface ConnectionSource
Parameters:
msTimeout - the maximum amount of time to wait for a connection to become available.
Throws:
SQLTimedOutException - if the specific maximum waiting time is exceeded and still no connection is available. Only some implementations (ones that support timeout detection) throw this specific SQLException. This is a subclass of SQLException so callers can optionally ignore this specific kind of exception by simply catching SQLException.
SQLInterruptedException - if calling thread is interrupted while waiting for a connection to become available. Only some implementations (ones that support interrupt detection) throw this specific SQLException. This is a subclass of SQLException so callers can optionally ignore this specific kind of exception by simple catching SQLException.
SQLException - if there is trouble getting a connection.
SQLShutdownException

getConnection

public Connection getConnection()
                         throws SQLException
Description copied from interface: ConnectionSource
Returns a Connection (potentially from a shared resource pool). This connection may be checked out from a shared pool of connections or may be from some other source. When done with a Connection, be sure to close it to potentially return it to a pool. Due to the fact that this Connection might be in a shared pool, you must ensure that after you have called close, your code does not do anything more with this Connection or anything it created (like a Statement, or a ResultSet).

Calls to this method may block while waiting for a Connection if there is a maximum number of connections allowed in an underlying resource pool.

Specified by:
getConnection in interface ConnectionSource
Throws:
SQLInterruptedException - if calling thread is interrupted while waiting for a connection to become available. Only some implementations (ones that support interrupt detection) throw this specific SQLException. This is a subclass of SQLException so callers can optionally ignore this specific kind of exception by simple catching SQLException.
SQLException - if there is trouble getting a connection.

shutdown

public void shutdown()
Description copied from interface: ConnectionSource
Call this when done with the ConnectionSource to allow any underlying allocated resources to be released.

Specified by:
shutdown in interface ConnectionSource

ProgramixGenericLib v5.0.1

Copyright © 2001-2009 Programix Incorporated. All rights reserved. ProgramixGenericLib is free and is OSI Certified Open Source Software under the BSD license.