org.apache.ojb.broker.accesslayer
Class ConnectionFactoryDBCPImpl

java.lang.Object
  extended by org.apache.ojb.broker.accesslayer.ConnectionFactoryAbstractImpl
      extended by org.apache.ojb.broker.accesslayer.ConnectionFactoryDBCPImpl
All Implemented Interfaces:
ConnectionFactory

public class ConnectionFactoryDBCPImpl
extends ConnectionFactoryAbstractImpl

ConnectionFactory implementation using Commons DBCP and Commons Pool API to pool connections. Based on a proposal of Dirk Verbeek - Thanks.

Version:
$Id: ConnectionFactoryDBCPImpl.java,v 1.1 2007-08-24 22:17:30 ewestfal Exp $
Author:
Armin Waibel
See Also:
Commons Pool Website, Commons DBCP Website

Field Summary
static String PARAM_NAME_POOL_STATEMENTS
           
static String PARAM_NAME_STATEMENT_POOL_MAX_TOTAL
           
static String PARAM_NAME_UNWRAP_ALLOWED
           
 
Constructor Summary
ConnectionFactoryDBCPImpl()
           
 
Method Summary
 Connection checkOutJdbcConnection(JdbcConnectionDescriptor jcd)
          Returns a valid JDBC Connection.
protected  org.apache.commons.dbcp.ConnectionFactory createConnectionFactory(JdbcConnectionDescriptor jcd)
          Creates a DriverManager-based ConnectionFactory for creating the Connection instances to feed into the object pool of the specified jcd-alias.
protected  org.apache.commons.pool.ObjectPool createConnectionPool(org.apache.commons.pool.impl.GenericObjectPool.Config config, org.apache.commons.dbcp.AbandonedConfig ac)
           
protected  org.apache.commons.pool.ObjectPool createObjectPool(org.apache.commons.pool.impl.GenericObjectPool.Config config)
          Deprecated. since OJB 1.0.4, please use createConnectionPool(org.apache.commons.pool.impl.GenericObjectPool.Config, org.apache.commons.dbcp.AbandonedConfig)
protected  org.apache.commons.dbcp.PoolingDataSource createPoolingDataSource(org.apache.commons.pool.ObjectPool connectionPool)
          Deprecated. since OJB 1.0.4, please use wrapAsDataSource(org.apache.ojb.broker.metadata.JdbcConnectionDescriptor, org.apache.commons.pool.ObjectPool)
protected  org.apache.commons.pool.KeyedObjectPoolFactory createStatementPoolFactory(JdbcConnectionDescriptor jcd)
           
protected  DataSource getDataSource(JdbcConnectionDescriptor jcd)
          Returns the DBCP DataSource for the specified connection descriptor, after creating a new DataSource if needed.
 void releaseAllResources()
          Closes all managed pools.
 void releaseJdbcConnection(JdbcConnectionDescriptor jcd, Connection con)
          Releases a Connection after use.
protected  org.apache.commons.pool.ObjectPool setupPool(JdbcConnectionDescriptor jcd)
          Returns a new ObjectPool for the specified connection descriptor.
protected  DataSource wrapAsDataSource(JdbcConnectionDescriptor jcd, org.apache.commons.pool.ObjectPool connectionPool)
          Wraps the specified object pool for connections as a DataSource.
 
Methods inherited from class org.apache.ojb.broker.accesslayer.ConnectionFactoryAbstractImpl
getDbURL, getJcdDescription, getJdbcProperties, getJdbcProperties, initializeJdbcConnection, lookupConnection, newConnectionFromDataSource, newConnectionFromDriverManager, releaseConnection
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PARAM_NAME_UNWRAP_ALLOWED

public static final String PARAM_NAME_UNWRAP_ALLOWED
See Also:
Constant Field Values

PARAM_NAME_POOL_STATEMENTS

public static final String PARAM_NAME_POOL_STATEMENTS
See Also:
Constant Field Values

PARAM_NAME_STATEMENT_POOL_MAX_TOTAL

public static final String PARAM_NAME_STATEMENT_POOL_MAX_TOTAL
See Also:
Constant Field Values
Constructor Detail

ConnectionFactoryDBCPImpl

public ConnectionFactoryDBCPImpl()
Method Detail

checkOutJdbcConnection

public Connection checkOutJdbcConnection(JdbcConnectionDescriptor jcd)
                                  throws LookupException
Description copied from class: ConnectionFactoryAbstractImpl
Returns a valid JDBC Connection. Implement this method in concrete subclasses. Concrete implementations using Connection pooling are responsible for any validation and pool removal management.

Note: This method is never called for a jdbc-connection-descriptor that uses datasources, OJB only manages connections from DriverManager.

Note: If the concrete implementation does not callback to ConnectionFactoryAbstractImpl.newConnectionFromDriverManager(org.apache.ojb.broker.metadata.JdbcConnectionDescriptor) when creating a new Connection, it must call ConnectionFactoryAbstractImpl.initializeJdbcConnection(java.sql.Connection, org.apache.ojb.broker.metadata.JdbcConnectionDescriptor) so that the platform implementation can peform any RDBMS-specific init tasks for newly created Connection objetcs.

Specified by:
checkOutJdbcConnection in class ConnectionFactoryAbstractImpl
Parameters:
jcd - the connection descriptor for which to return a validated Connection
Returns:
a valid Connection, never null. Specific implementations must guarantee that the connection is not null and that it is valid.
Throws:
LookupException - if a valid Connection could not be obtained

releaseJdbcConnection

public void releaseJdbcConnection(JdbcConnectionDescriptor jcd,
                                  Connection con)
                           throws LookupException
Description copied from class: ConnectionFactoryAbstractImpl
Releases a Connection after use. Implement this method in concrete subclasses. Concrete implementations using Connection pooling are responsible for any validation and pool removal management.

Note: This method is never called for a jdbc-connection-descriptor that uses datasources, OJB only manages connections from DriverManager.

Specified by:
releaseJdbcConnection in class ConnectionFactoryAbstractImpl
Parameters:
jcd - the connection descriptor for which the connection was created
con - the connection to release. Callers must guarantee that the passed connection was obtained by calling ConnectionFactoryAbstractImpl.checkOutJdbcConnection(org.apache.ojb.broker.metadata.JdbcConnectionDescriptor).
Throws:
LookupException - if errors occured during release of object. Typically happens if return of object to pool fails in a pooled implementation.

releaseAllResources

public void releaseAllResources()
Closes all managed pools.

Specified by:
releaseAllResources in interface ConnectionFactory
Overrides:
releaseAllResources in class ConnectionFactoryAbstractImpl

getDataSource

protected DataSource getDataSource(JdbcConnectionDescriptor jcd)
                            throws LookupException
Returns the DBCP DataSource for the specified connection descriptor, after creating a new DataSource if needed.

Parameters:
jcd - the descriptor for which to return a DataSource
Returns:
a DataSource, after creating a new pool if needed. Guaranteed to never be null.
Throws:
LookupException - if pool is not in cache and cannot be created

setupPool

protected org.apache.commons.pool.ObjectPool setupPool(JdbcConnectionDescriptor jcd)
Returns a new ObjectPool for the specified connection descriptor. Override this method to setup your own pool.

Parameters:
jcd - the connection descriptor for which to set up the pool
Returns:
a newly created object pool

createConnectionPool

protected org.apache.commons.pool.ObjectPool createConnectionPool(org.apache.commons.pool.impl.GenericObjectPool.Config config,
                                                                  org.apache.commons.dbcp.AbandonedConfig ac)

createStatementPoolFactory

protected org.apache.commons.pool.KeyedObjectPoolFactory createStatementPoolFactory(JdbcConnectionDescriptor jcd)

wrapAsDataSource

protected DataSource wrapAsDataSource(JdbcConnectionDescriptor jcd,
                                      org.apache.commons.pool.ObjectPool connectionPool)
Wraps the specified object pool for connections as a DataSource.

Parameters:
jcd - the OJB connection descriptor for the pool to be wrapped
connectionPool - the connection pool to be wrapped
Returns:
a DataSource attached to the connection pool. Connections will be wrapped using DBCP PoolGuard, that will not allow unwrapping unless the "accessToUnderlyingConnectionAllowed=true" configuration is specified.

createConnectionFactory

protected org.apache.commons.dbcp.ConnectionFactory createConnectionFactory(JdbcConnectionDescriptor jcd)
Creates a DriverManager-based ConnectionFactory for creating the Connection instances to feed into the object pool of the specified jcd-alias.

NB! If you override this method to specify your own ConnectionFactory you must make sure that you follow OJB's lifecycle contract defined in the Platform API - ie that you call initializeJdbcConnection when a new Connection is created. For convenience, use ConnectionFactoryAbstractImpl.initializeJdbcConnection(java.sql.Connection, org.apache.ojb.broker.metadata.JdbcConnectionDescriptor) instead of Platform call.

The above is automatically true if you re-use the inner class ConPoolFactory below and just override this method for additional user-defined "tweaks".

Parameters:
jcd - the jdbc-connection-alias for which we are creating a ConnectionFactory
Returns:
a DriverManager-based ConnectionFactory that creates Connection instances using DriverManager, and that follows the lifecycle contract defined in OJB Platform API.

createObjectPool

protected org.apache.commons.pool.ObjectPool createObjectPool(org.apache.commons.pool.impl.GenericObjectPool.Config config)
Deprecated. since OJB 1.0.4, please use createConnectionPool(org.apache.commons.pool.impl.GenericObjectPool.Config, org.apache.commons.dbcp.AbandonedConfig)

mkalen: Left for binary API-compatibility with OJB 1.0.3 (don't break users' factories)


createPoolingDataSource

protected org.apache.commons.dbcp.PoolingDataSource createPoolingDataSource(org.apache.commons.pool.ObjectPool connectionPool)
Deprecated. since OJB 1.0.4, please use wrapAsDataSource(org.apache.ojb.broker.metadata.JdbcConnectionDescriptor, org.apache.commons.pool.ObjectPool)

mkalen: Left for binary API-compatibility with OJB 1.0.3 (don't break users' factories)



Copyright © 2004-2012 The Kuali Foundation. All Rights Reserved.