org.opensubsystems.core.persist.db.connectionpool
Class PooledDatabaseConnectionFactoryImpl

java.lang.Object
  extended by org.opensubsystems.core.persist.db.DatabaseConnectionFactoryImpl
      extended by org.opensubsystems.core.persist.db.connectionpool.PooledDatabaseConnectionFactoryImpl
All Implemented Interfaces:
DatabaseConnectionFactory
Direct Known Subclasses:
C3P0DatabaseConnectionFactoryImpl, DBCPDatabaseConnectionFactoryImpl, J2EEDatabaseConnectionFactoryImpl, ProxoolDatabaseConnectionFactoryImpl, XAPoolDatabaseConnectionFactoryImpl

public abstract class PooledDatabaseConnectionFactoryImpl
extends DatabaseConnectionFactoryImpl

Base class for implementation of factories for retrieving and returning of database connections, which are maintained in a pool of always ready connections.

Version:
$Id: PooledDatabaseConnectionFactoryImpl.java,v 1.8 2007/01/07 06:14:58 bastafidli Exp $
Author:
Miro Halas
Code reviewer:
Miro Halas
Code reviewed:
1.6 2006/05/21 03:43:47 bastafidli

Field Summary
static java.lang.String DEFAULT_DATASOURCE_NAME
          Name that will be used for default datasource when the connection factory is started and no default datasource is defined.
protected  org.opensubsystems.core.persist.db.connectionpool.PooledDatabaseConnectionFactoryImpl.ConnectionPoolDefinition m_defaultConnectionPool
          Default connection pool used for all connections.
protected  java.util.Map m_mpConnectionPoolCrossRef
          Map used to cross reference connection to their pool so that we can correctly manage counts of requested connections per pool
protected  java.util.Map m_mpConnectionPools
          Registered connection pools.
protected  java.util.Map m_mpNotPooledConnections
          Map of not pooled connections which needs to be closed explicitely.
 
Fields inherited from class org.opensubsystems.core.persist.db.DatabaseConnectionFactoryImpl
ADMIN_DATASOURCE_NAME, DATABASE_ADMIN_PASSWORD, DATABASE_ADMIN_USER, DATABASE_DRIVER, DATABASE_PASSWORD, DATABASE_URL, DATABASE_USE_ADMIN_DATAROURCE, DATABASE_USE_ADMIN_DATAROURCE_DEFAULT, DATABASE_USER, m_bUseAdminDataSource, m_iRequestedConnectionCount, m_strDatabaseAdminPassword, m_strDatabaseAdminUser, m_strDatabaseDriver, m_strDatabasePassword, m_strDatabaseURL, m_strDatabaseUser, m_strRealDatabaseDriver, m_transactionFactory, SPY_CONFIG_FILE_NAME, SPY_DRIVER_IDENTIFICATION, SPY_REALDRIVER
 
Constructor Summary
PooledDatabaseConnectionFactoryImpl()
          Default constructor.
PooledDatabaseConnectionFactoryImpl(DatabaseTransactionFactoryImpl transactionFactory)
          Default constructor.
PooledDatabaseConnectionFactoryImpl(java.lang.String strDriver, java.lang.String strURL, java.lang.String strUser, java.lang.String strPassword, DatabaseTransactionFactoryImpl transactionFactory)
          Constructor with explicitely specified JDBC driver.
 
Method Summary
 void addDataSource(java.lang.String strDataSourceName, java.lang.String strDriverName, java.lang.String strUrl, java.lang.String strUser, java.lang.String strPassword)
          Create new data source.
protected abstract  void closeConnectionPool(org.opensubsystems.core.persist.db.connectionpool.PooledDatabaseConnectionFactoryImpl.ConnectionPoolDefinition connectionpool)
          Close the specified connection pool and all connections maintained to database by this connection pool.
protected abstract  java.lang.Object createConnectionPool(java.lang.String strConnectionPoolName, java.lang.String strDriverName, java.lang.String strUrl, java.lang.String strUser, java.lang.String strPassword)
          Create new connection pool with specified parameters.
 java.lang.String debug()
          Create string representing state of the factory so we can user it to debug connection related problems.
 java.lang.String getDefaultDataSourceName()
          Get the name of the default data source.
protected abstract  java.sql.Connection getPooledConnection(org.opensubsystems.core.persist.db.connectionpool.PooledDatabaseConnectionFactoryImpl.ConnectionPoolDefinition connectionpool)
          Get connection for given connection pool.
protected abstract  java.sql.Connection getPooledConnection(org.opensubsystems.core.persist.db.connectionpool.PooledDatabaseConnectionFactoryImpl.ConnectionPoolDefinition connectionpool, java.lang.String strUser, java.lang.String strPassword)
          Get connection using the same settings as given connection pool but get the connection under different name and password.
 int getRequestedConnectionCount()
          Get number for connections which are currently requested and were not returned for default data source.
 int getRequestedConnectionCount(java.lang.String strDataSourceName)
          Get number for connections which are currently requested and were not returned.
 boolean isDataSourceDefined(java.lang.String strDataSourceName)
          Check if the specified data has been already defined
protected  java.sql.Connection requestNonTransactionalConnection(boolean bAutoCommit)
          This method is a delegation method for now final org.opensubsystems.core.persist.db.DatabaseConnectionFactory#requestConnection() to get connection which is not tied to the transactional manager.
protected  java.sql.Connection requestNonTransactionalConnection(boolean bAutoCommit, java.lang.String strDataSourceName)
          This method is a delegation method for now final org.opensubsystems.core.persist.db.DatabaseConnectionFactory #requestConnection(String) to get connection which is not tied to the transactional manager.
protected  java.sql.Connection requestNonTransactionalConnection(boolean bAutoCommit, java.lang.String strUser, java.lang.String strPassword)
          This method is a delegation method for now final org.opensubsystems.core.persist.db.DatabaseConnectionFactory #requestConnection(String, String) to get connection which is not tied to the transactional manager.
protected  java.sql.Connection requestNonTransactionalConnection(boolean bAutoCommit, java.lang.String strDataSourceName, java.lang.String strUser, java.lang.String strPassword)
          This method is a delegation method for now final org.opensubsystems.core.persist.db.DatabaseConnectionFactory #requestConnection(String, String, String) to get connection which is not tied to the transactional manager.
 void returnNonTransactionalConnection(java.sql.Connection cntDBConnection)
          This method is a delegation method for now final org.opensubsystems.core.persist.db.DatabaseConnectionFactory #returnConnection(java.sql.Connection) to return connection which is not tied to the transactional manager.
protected  void returnPooledConnection(java.sql.Connection cntDBConnection)
          Return connection which was taken from the pool.
 void setDefaultDataSourceName(java.lang.String strDataSourceName)
          Set the default data source.
protected  void start()
          Start the connection factory.
 void stop()
          Stop the connection factory.
 
Methods inherited from class org.opensubsystems.core.persist.db.DatabaseConnectionFactoryImpl
getDatabaseAdminPassword, getDatabaseAdminUser, getDatabaseDriver, getDatabasePassword, getDatabaseURL, getDatabaseUser, getInstance, getRealDatabaseDriver, getTotalRequestedConnectionCount, getUseAdminDataSource, initializeConnection, initializeDriver, loadDefaultDatabaseProperties, requestConnection, requestConnection, requestConnection, requestConnection, returnConnection, setDatabaseAdminPassword, setDatabaseAdminUser, setDatabaseDriver, setDatabasePassword, setDatabaseURL, setDatabaseUser, setInstance, setTransactionFactory, setUseAdminDataSource
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_DATASOURCE_NAME

public static final java.lang.String DEFAULT_DATASOURCE_NAME
Name that will be used for default datasource when the connection factory is started and no default datasource is defined. Make this name somehow meaningful because for example for J2EE CF this name has to be configured in external configuration file.

See Also:
Constant Field Values

m_defaultConnectionPool

protected org.opensubsystems.core.persist.db.connectionpool.PooledDatabaseConnectionFactoryImpl.ConnectionPoolDefinition m_defaultConnectionPool
Default connection pool used for all connections.


m_mpConnectionPools

protected java.util.Map m_mpConnectionPools
Registered connection pools. Key is String, connection pool name and value is a ConnectionPoolDefinition.


m_mpNotPooledConnections

protected java.util.Map m_mpNotPooledConnections
Map of not pooled connections which needs to be closed explicitely. This allows the implementing connection factory also have some connections which are not pooled and by putting them into this map the connection factory can make sure that they are closed when the factory is stopped (if they were not returned earlier, since then they are closed immidiately). Key is connection and value is the same connection.


m_mpConnectionPoolCrossRef

protected java.util.Map m_mpConnectionPoolCrossRef
Map used to cross reference connection to their pool so that we can correctly manage counts of requested connections per pool

Constructor Detail

PooledDatabaseConnectionFactoryImpl

public PooledDatabaseConnectionFactoryImpl()
Default constructor.


PooledDatabaseConnectionFactoryImpl

public PooledDatabaseConnectionFactoryImpl(DatabaseTransactionFactoryImpl transactionFactory)
Default constructor.

Parameters:
transactionFactory - - transaction factory to use for this connection factory, can be null

PooledDatabaseConnectionFactoryImpl

public PooledDatabaseConnectionFactoryImpl(java.lang.String strDriver,
                                           java.lang.String strURL,
                                           java.lang.String strUser,
                                           java.lang.String strPassword,
                                           DatabaseTransactionFactoryImpl transactionFactory)
                                    throws OSSConfigException,
                                           OSSDatabaseAccessException
Constructor with explicitely specified JDBC driver.

Parameters:
strDriver - - JDBC driver to connect to the database
strURL - - URL of database to connect to
strUser - - user name to connect to the database
strPassword - - password to the database
transactionFactory - - transaction factory to use for this connection factory, can be null
Throws:
OSSConfigException - - problem accessing or locating the config file.
OSSDatabaseAccessException - - an error has occured accessing the database
Method Detail

getDefaultDataSourceName

public final java.lang.String getDefaultDataSourceName()
Get the name of the default data source.

Returns:
String

addDataSource

public final void addDataSource(java.lang.String strDataSourceName,
                                java.lang.String strDriverName,
                                java.lang.String strUrl,
                                java.lang.String strUser,
                                java.lang.String strPassword)
                         throws OSSDatabaseAccessException
Create new data source. If it doesn't exist, it will be created.

Parameters:
strDataSourceName - - data source name
strDriverName - - name of the JDBC driver
strUrl - - url by which data source connects to the database
strUser - - user name to connects to the database
strPassword - - password to connects to the database
Throws:
OSSDatabaseAccessException - - data source with the same name already exists

setDefaultDataSourceName

public final void setDefaultDataSourceName(java.lang.String strDataSourceName)
Set the default data source.

Parameters:
strDataSourceName - - data source which will be used to get connections

isDataSourceDefined

public boolean isDataSourceDefined(java.lang.String strDataSourceName)
Check if the specified data has been already defined

Parameters:
strDataSourceName - - name of the data source to check for
Returns:
boolean - true if the data source is already defined, false otherwise

stop

public final void stop()
                throws OSSException
Stop the connection factory. After the connection factory is stopped all the connections should be released and they cannot be retrieved unless the connection factory is reinitialized in some way.

Throws:
OSSException - - problem stopping connection factory.

getRequestedConnectionCount

public int getRequestedConnectionCount()
Get number for connections which are currently requested and were not returned for default data source.

Returns:
int - how many connections were currently requested from pool and were not returned yet.

getRequestedConnectionCount

public int getRequestedConnectionCount(java.lang.String strDataSourceName)
Get number for connections which are currently requested and were not returned.

Parameters:
strDataSourceName - - data source which will be used to get connections
Returns:
int - how many connections were currently requested from pool and were not returned yet.

debug

public java.lang.String debug()
Create string representing state of the factory so we can user it to debug connection related problems.

Returns:
String - state of the connection factory.

requestNonTransactionalConnection

protected java.sql.Connection requestNonTransactionalConnection(boolean bAutoCommit)
                                                         throws OSSDatabaseAccessException
This method is a delegation method for now final org.opensubsystems.core.persist.db.DatabaseConnectionFactory#requestConnection() to get connection which is not tied to the transactional manager. This method is protected so that only classes from this package can access it.

Specified by:
requestNonTransactionalConnection in class DatabaseConnectionFactoryImpl
Parameters:
bAutoCommit - - The desired autocommit state of the connection. If this connection is invoked in global (JTA) transaction then the autocommit is false regardless of what value is specified here. Use true here if the client only reads the data and false if the client also modifies the data. Use DatabaseTransactionFactory.commitTransaction to commit the transaction.
Returns:
Connection - connection to a database, never null
Throws:
OSSDatabaseAccessException - - if connection cannot be established
See Also:
DatabaseConnectionFactoryImpl.returnConnection(java.sql.Connection)

requestNonTransactionalConnection

protected final java.sql.Connection requestNonTransactionalConnection(boolean bAutoCommit,
                                                                      java.lang.String strUser,
                                                                      java.lang.String strPassword)
                                                               throws OSSDatabaseAccessException
This method is a delegation method for now final org.opensubsystems.core.persist.db.DatabaseConnectionFactory #requestConnection(String, String) to get connection which is not tied to the transactional manager. This method is protected so that only classes from this package can access it.

Specified by:
requestNonTransactionalConnection in class DatabaseConnectionFactoryImpl
Parameters:
bAutoCommit - - The desired autocommit state of the connection. If this connection is invoked in global (JTA) transaction then the autocommit is false regardless of what value is specified here. Use true here if the client only reads the data and false if the client also modifies the data. Use DatabaseTransactionFactory.commitTransaction to commit the transaction.
strUser - - user name to connect to the database
strPassword - - password to the database
Returns:
Connection - connection to a database, never null
Throws:
OSSDatabaseAccessException - - if connection cannot be established
See Also:
DatabaseConnectionFactoryImpl.returnConnection(java.sql.Connection)

requestNonTransactionalConnection

protected final java.sql.Connection requestNonTransactionalConnection(boolean bAutoCommit,
                                                                      java.lang.String strDataSourceName)
                                                               throws OSSDatabaseAccessException
This method is a delegation method for now final org.opensubsystems.core.persist.db.DatabaseConnectionFactory #requestConnection(String) to get connection which is not tied to the transactional manager. This method is protected so that only classes from this package can access it.

Specified by:
requestNonTransactionalConnection in class DatabaseConnectionFactoryImpl
Parameters:
bAutoCommit - - The desired autocommit state of the connection. If this connection is invoked in global (JTA) transaction then the autocommit is false regardless of what value is specified here. Use true here if the client only reads the data and false if the client also modifies the data. Use DatabaseTransactionFactory.commitTransaction to commit the transaction.
strDataSourceName - - data source which will be used to get connections
Returns:
Connection - connection to a database, never null
Throws:
OSSDatabaseAccessException - - if connection cannot be established
See Also:
DatabaseConnectionFactoryImpl.returnConnection(java.sql.Connection)

requestNonTransactionalConnection

protected final java.sql.Connection requestNonTransactionalConnection(boolean bAutoCommit,
                                                                      java.lang.String strDataSourceName,
                                                                      java.lang.String strUser,
                                                                      java.lang.String strPassword)
                                                               throws OSSDatabaseAccessException
This method is a delegation method for now final org.opensubsystems.core.persist.db.DatabaseConnectionFactory #requestConnection(String, String, String) to get connection which is not tied to the transactional manager. This method is protected so that only classes from this package can access it.

Specified by:
requestNonTransactionalConnection in class DatabaseConnectionFactoryImpl
Parameters:
bAutoCommit - - The desired autocommit state of the connection. If this connection is invoked in global (JTA) transaction then the autocommit is false regardless of what value is specified here. Use true here if the client only reads the data and false if the client also modifies the data. Use DatabaseTransactionFactory.commitTransaction to commit the transaction.
strDataSourceName - - data source which will be used to get connections
strUser - - user name to connect to the database
strPassword - - password to the database
Returns:
Connection - connection to a database, never null
Throws:
OSSDatabaseAccessException - - if connection cannot be established
See Also:
DatabaseConnectionFactoryImpl.returnConnection(java.sql.Connection)

returnNonTransactionalConnection

public final void returnNonTransactionalConnection(java.sql.Connection cntDBConnection)
This method is a delegation method for now final org.opensubsystems.core.persist.db.DatabaseConnectionFactory #returnConnection(java.sql.Connection) to return connection which is not tied to the transactional manager. This method is protected so that only classes from this package can access it.

Specified by:
returnNonTransactionalConnection in class DatabaseConnectionFactoryImpl
Parameters:
cntDBConnection - - connection to return, can be null

start

protected void start()
              throws OSSDatabaseAccessException
Start the connection factory.

Throws:
OSSDatabaseAccessException - - an error has occured

getPooledConnection

protected abstract java.sql.Connection getPooledConnection(org.opensubsystems.core.persist.db.connectionpool.PooledDatabaseConnectionFactoryImpl.ConnectionPoolDefinition connectionpool)
                                                    throws OSSDatabaseAccessException
Get connection for given connection pool.

Parameters:
connectionpool - - connection pool to get connection for
Returns:
Connection - this can be null if connection is not available
Throws:
OSSDatabaseAccessException - - an error getting connection

getPooledConnection

protected abstract java.sql.Connection getPooledConnection(org.opensubsystems.core.persist.db.connectionpool.PooledDatabaseConnectionFactoryImpl.ConnectionPoolDefinition connectionpool,
                                                           java.lang.String strUser,
                                                           java.lang.String strPassword)
                                                    throws OSSDatabaseAccessException
Get connection using the same settings as given connection pool but get the connection under different name and password.

Parameters:
connectionpool - - connection pool defining settings to get connection for
strUser - - user name to get the connection for
strPassword - - password to get the connection for
Returns:
Connection - this can be null if connection is not available
Throws:
OSSDatabaseAccessException - - an error getting connection

returnPooledConnection

protected void returnPooledConnection(java.sql.Connection cntDBConnection)
Return connection which was taken from the pool. Since most pool implementation will return connection to the pool as soon as it is closed the default implementation is provided to just close the connection. If the derived class needs to do something specific, it can do so by overriding this method.

Parameters:
cntDBConnection - - connection taked from the pool

createConnectionPool

protected abstract java.lang.Object createConnectionPool(java.lang.String strConnectionPoolName,
                                                         java.lang.String strDriverName,
                                                         java.lang.String strUrl,
                                                         java.lang.String strUser,
                                                         java.lang.String strPassword)
                                                  throws OSSException
Create new connection pool with specified parameters.

Parameters:
strConnectionPoolName - - connection pool name
strDriverName - - name of the JDBC driver
strUrl - - url by which data source connects to the database
strUser - - user name to connects to the database
strPassword - - password to connects to the database
Returns:
Object - connection pool
Throws:
OSSException - - an error has occured during creation of connection pool

closeConnectionPool

protected abstract void closeConnectionPool(org.opensubsystems.core.persist.db.connectionpool.PooledDatabaseConnectionFactoryImpl.ConnectionPoolDefinition connectionpool)
                                     throws OSSException
Close the specified connection pool and all connections maintained to database by this connection pool.

Parameters:
connectionpool - - connection pool to close.
Throws:
OSSException - - an error has occured during closing of the connection pool


Copyright © 2003 - 2006 OpenSubsystems s.r.o.