Enhydra 3.1 API

com.lutris.appserver.server.sql.standard
Class StandardConnectionAllocator

java.lang.Object
  |
  +--com.lutris.appserver.server.sql.standard.StandardConnectionAllocator
Direct Known Subclasses:
InformixConnectionAllocator, MsqlConnectionAllocator, OracleConnectionAllocator, SybaseConnectionAllocator

public class StandardConnectionAllocator
extends java.lang.Object
implements ConnectionAllocator

Manages a pool (set) of connections to a database. The pool is named as a logical database. By naming a pool, this allows connection resource control to be on a finer grain that a database and allows for easier migration to multiple databases. One or more pools can map to the same actual database. A connection considered part of the pool, even if its allocated to a thread. These objects are all publicly accessed via the Database Manager, not directly.

If an error occurs in a connection, it is dropped from the pool. The process using the connection has already received an error which aborts the work in progress. By dropping the connection, waiting threads are restarted. If something is wrong with the database (e.g. server is down), they will recieve errors and also be aborted. A generation number is used to close down all connections that were open when the error occured, allowing new connections to be allocated.

The configuration data is specified in the section: DatabaseManager.DB.dbName.Connection

Configuration sub fields are:

It would be nice to add a config parameter that would disable caching of PreparedStatements.

Since:
LBS1.8
Version:
$Revision: 1.10.12.1 $

Field Summary
protected  int generation
          Generation number.
protected  LogicalDatabase logicalDatabase
          Reference to the logical database for easy access to the connection pool.
protected  int maxPreparedStatements
          Maximum number of prepared statements to use; if less-than zero, then JDBC is queried for this value.
protected  long numRequests
          Number of queries or transactions on this logical database.
protected  java.lang.String password
          SQL password..
protected  int queryTimeOut
          Maximum amount of time in seconds to block on a query.
protected  boolean sqlLogging
          Indicates if logging is enabled.
protected  int transactionTimeOut
          Maximum amount of time in seconds to block on a transaction.
protected  java.lang.String url
          JDBC URL of database.
protected  java.lang.String user
          SQL user name.
 
Constructor Summary
protected StandardConnectionAllocator(LogicalDatabase logicalDatabase, Config conConfig)
          Initialize the connection allocator object.
 
Method Summary
 DBConnection allocate()
          Allocate a connection to a thread.
protected  DBConnection createConnection()
          Create a new connection in the pool.
 void drop(DBConnection dbConnection)
          Called when a connection in this pool has an SQL error.
 void dropAllNow()
          Called when the database manager is shutting down: Close all connections immediately.
protected  void finalize()
          Finalizer.
 int getActiveCount()
          Return the number of currently active connections.
 int getMaxCount()
          Return the maximum number of connections active at one time.
 java.util.Date getMaxCountDate()
          Return the time when the maximum connection count occured.
 long getRequestCount()
          Return the number of database requests.
 void release(DBConnection dbConnection)
          Return a connection to the pool.
 void resetMaxCount()
          Reset the maximum connection count and date.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logicalDatabase

protected LogicalDatabase logicalDatabase
Reference to the logical database for easy access to the connection pool.

url

protected java.lang.String url
JDBC URL of database.

user

protected java.lang.String user
SQL user name.

password

protected java.lang.String password
SQL password..

numRequests

protected long numRequests
Number of queries or transactions on this logical database.

sqlLogging

protected boolean sqlLogging
Indicates if logging is enabled.

queryTimeOut

protected int queryTimeOut
Maximum amount of time in seconds to block on a query. The DBQuery object will retrieve this value from the connection.

transactionTimeOut

protected int transactionTimeOut
Maximum amount of time in seconds to block on a transaction. The DBTransaction object will retrieve this value from the connection.

maxPreparedStatements

protected int maxPreparedStatements
Maximum number of prepared statements to use; if less-than zero, then JDBC is queried for this value.

generation

protected int generation
Generation number. When an SQL error occurs, all objects of the same generation or earlier are dropped.
Constructor Detail

StandardConnectionAllocator

protected StandardConnectionAllocator(LogicalDatabase logicalDatabase,
                                      Config conConfig)
                               throws ConfigException
Initialize the connection allocator object. Connections are opened on demand and stored in a pool.
Parameters:
url - JDBC URL of database.
user - SQL user name.
password - SQL password.
maxPoolSize - Maximum number of connections.
sqlLogging - Specifying true enables SQL logging.
timeOut - Set the maximum amount of time in milliseconds to wait for a new connection.
Throws:
ConfigException - if bad configuration information is given in the config file.
Method Detail

createConnection

protected DBConnection createConnection()
                                 throws java.sql.SQLException
Create a new connection in the pool.
Throws:
java.sql.SQLException - If a SQL error occures.

allocate

public DBConnection allocate()
                      throws java.sql.SQLException
Allocate a connection to a thread. If none are available, grow the pool. If the pool is alredy its maximum size, then the thread waits.
Specified by:
allocate in interface ConnectionAllocator
Returns:
The allocated connection object.
Throws:
java.sql.SQLException - If a SQL error occures.

release

public void release(DBConnection dbConnection)
Return a connection to the pool. If it is of an old generation, close and drop.
Specified by:
release in interface ConnectionAllocator
Parameters:
dbConnection - The connection object to return.

drop

public void drop(DBConnection dbConnection)
Called when a connection in this pool has an SQL error. All unallocated connections in the pool are dropped if they have a generation number less than or equal to the error connection. If the current generation number is the same as this generation number, increment it. This way so that all outstanding connections (including the one passed in) of the same generation are dropped when returned.
Specified by:
drop in interface ConnectionAllocator
Parameters:
dbConnection - The connection object to drop. The connection should be returned to the pool after this function returns.

dropAllNow

public void dropAllNow()
Called when the database manager is shutting down: Close all connections immediately.
Specified by:
dropAllNow in interface ConnectionAllocator

getActiveCount

public int getActiveCount()
Return the number of currently active connections.
Specified by:
getActiveCount in interface ConnectionAllocator
Returns:
The number of connections.

getMaxCount

public int getMaxCount()
Return the maximum number of connections active at one time.
Specified by:
getMaxCount in interface ConnectionAllocator
Returns:
The number of connections.

getMaxCountDate

public java.util.Date getMaxCountDate()
Return the time when the maximum connection count occured.
Specified by:
getMaxCountDate in interface ConnectionAllocator
Returns:
The Date when the maximum connection count occured.

resetMaxCount

public void resetMaxCount()
Reset the maximum connection count and date.
Specified by:
resetMaxCount in interface ConnectionAllocator

getRequestCount

public long getRequestCount()
Return the number of database requests.
Specified by:
getRequestCount in interface ConnectionAllocator
Returns:
The number of database requests (queries or transactions).

finalize

protected void finalize()
Finalizer. If any connections allocated by this object have not been closed, this method ensures that garbage collection does so.
Overrides:
finalize in class java.lang.Object

Enhydra 3.1 API