|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--net.sourceforge.esw.util.database.DBConnectionPool
A pool of java.sql.Connection
instances to optimize connection
to a JDBC data source.
A database connection pool greatly simplifies many of the common tasks
associated with JDBC database access. It lets you use the connections
without worrying about the maintainance or scalability of your database
access. The DBConnectionPool
instance lets you interact with
your JDBC connections as standard java.sql.Connection
instances,
instead of making you use some non-standard api for interaction. This allows
the developer to have greater flexibility for implemention by reducing the
overhead for learning and integrating.
A DBConnectionPool
instance has a URL and, if required, a UserID
that can be used to uniquely identify the source data that the connection
represents. Some databases allow the UserID and/or Password to be specified
in the URL. Some do not require secure access, so neither the UserID or
Password is required to use a DBConnectionPool
instance.
A DBConnectionPool
instance does not initialize the database
driver directly, but the DBConnectionPoolManager
has a method to
initialize the driver for you called initializeDriver( aDriver )
.
You can also follow the JDBC standard method for registering your JDBC driver
in the Java VM using the DriverManager
. You should only need to
register a unique driver once during a Java VM session.
To use a DBConnectionPool
instance, see the getPool
method of the DBConnectionPoolManager
singleton.
Examples:
DBConnectionPoolManager.initializeDriver( aDriver ); DBConnectionPool pl = DBConnectionPoolManager.getInstance().getPool( getURL(), getUserID() ); pl.setDatabasePassword( getPassword() ); connection = pl.getDBConnection(); PreparedStatement ps = connection.prepareStatement( getSQLStatement );
Field Summary | |
protected java.util.ArrayList |
availDBConnections
The ArrayList instance that holds the availibleConnections. |
protected boolean |
bHoldUntilConnectionAvailable
Specifies whether the getDBConnection method should wait
when the maximum number of allowed connections is reached. |
protected boolean |
bLogging
Specifies if database calls are logged to DriverManager's LogWriter. |
protected boolean |
bWaitingForConnection
Specifies if a client is waiting for an availiable connection. |
protected java.lang.String |
databaseURL
The URL used for database access. |
protected java.lang.String |
defaultDatabasePassword
The password used for database access. |
protected java.util.Properties |
defaultDatabaseProperties
The properties used for database access. |
protected java.lang.String |
defaultDatabaseUserID
The UserID used for database access. |
protected int |
maxSize
The maximum number of database connections to allow opened at once. |
protected int |
preferredSize
The optimal number of database connections to be opened at once. |
protected java.util.ArrayList |
usedDBConnections
The ArrayList instance that holds the usedConnections. |
Constructor Summary | |
DBConnectionPool(java.lang.String dbURL)
Creates a new DBConnectionPool instance. |
|
DBConnectionPool(java.lang.String dbURL,
java.util.Properties dbProp)
Creates a new DBConnectionPool instance. |
|
DBConnectionPool(java.lang.String dbURL,
java.util.Properties dbProp,
boolean startup)
Creates a new DBConnectionPool instance. |
|
DBConnectionPool(java.lang.String dbURL,
java.lang.String userid,
java.lang.String passwd)
Creates a new DBConnectionPool instance. |
|
DBConnectionPool(java.lang.String dbURL,
java.lang.String userid,
java.lang.String passwd,
boolean startup)
Creates a new DBConnectionPool instance. |
|
DBConnectionPool(java.lang.String dbURL,
java.lang.String userid,
java.lang.String passwd,
java.util.Properties dbProp,
boolean startup)
Creates a new DBConnectionPool instance. |
Method Summary | |
protected void |
closeConnection(java.sql.Connection conn)
Closes a Connection instance. |
void |
closeCurrentConnections()
Closes all current available and used Database Connections. |
protected java.sql.Connection |
createDatabaseConnection()
Creates a new Connection instance, wrapping the connection in
an adapter to provide all the pooled connection abilites. |
int |
getAvailablePoolSize()
Returns the number of available Database Connections. |
boolean |
getDatabaseLogging()
Returns if database logging is enabled. |
java.lang.String |
getDatabasePassword()
Returns the JDBC Database Connection Password Value. |
java.util.Properties |
getDatabaseProperties()
Returns the JDBC Database Connection Properties Value. |
java.lang.String |
getDatabaseURL()
Returns the JDBC Database Connection URL value. |
java.lang.String |
getDatabaseUserID()
Returns the JDBC Database Connection User ID Value. |
java.sql.Connection |
getDBConnection()
Returns a java.sql.Connection instance from the
DBConnectionPool instance. |
protected java.sql.Connection |
getDirectDatabaseConnection()
Creates a new Connection instance, directly connected to the
database. |
boolean |
getHoldUntilConnectionAvailable()
Returns whether this DBConnectionPool will cause the client
to wait when a request is made for a new database connection, but there
are none available to return and the maximum size has been reached. |
int |
getMaxSize()
Returns the Maximum Size for this DBConnectionPool . |
int |
getPreferredSize()
Returns the preferred number of connections in the pool for this DBConnectionPool . |
int |
getUsedPoolSize()
Returns the number of used Database Connections. |
void |
releaseDBConnection(java.sql.Connection theConnection)
Releases a Connection instance back into the pool. |
void |
setDatabaseLogging(boolean value)
Turning on Database Logging causes all calls by Statements against the database to log to DriverManager's LogWriter |
void |
setDatabasePassword(java.lang.String value)
Sets the value for the Password used for database access. |
void |
setDatabaseProperties(java.util.Properties value)
Sets the value for the Properties used for database access. |
void |
setDatabaseURL(java.lang.String value)
Sets the value for the URL used for database access. |
void |
setDatabaseUserID(java.lang.String value)
Sets the value for the UserID used for database access. |
void |
setHoldUntilConnectionAvailable(boolean value)
Sets a value for checks to see if the DBConnectionPool
instance should always wait to return a available connection from a call to
getDBConnection() . |
void |
setMaxSize(int value)
Sets the maximum number of concurrent connections to the database from this DBConnectionPool intstance. |
void |
setPreferredSize(int value)
Sets the optimal number of concurrent connections to the database from this DBConnectionPool instance. |
void |
startupDatabaseConnections()
Initializes all the database connections, up to the preferred size size setting. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected java.util.ArrayList availDBConnections
ArrayList
instance that holds the availibleConnections.protected java.util.ArrayList usedDBConnections
ArrayList
instance that holds the usedConnections.protected java.lang.String databaseURL
protected java.lang.String defaultDatabaseUserID
protected java.lang.String defaultDatabasePassword
protected java.util.Properties defaultDatabaseProperties
protected int maxSize
Integer.MAX_VALUE
.protected int preferredSize
protected boolean bHoldUntilConnectionAvailable
getDBConnection
method should wait
when the maximum number of allowed connections is reached.protected boolean bWaitingForConnection
protected boolean bLogging
Constructor Detail |
public DBConnectionPool(java.lang.String dbURL) throws java.sql.SQLException
DBConnectionPool
instance.dbURL
- The URL used for database access.java.sql.SQLException
- If a connection to the database can't be made.public DBConnectionPool(java.lang.String dbURL, java.util.Properties dbProp) throws java.sql.SQLException
DBConnectionPool
instance.dbURL
- The URL used for database access.dbProp
- The properties used for database access.java.sql.SQLException
- If a connection to the database can't be made.public DBConnectionPool(java.lang.String dbURL, java.util.Properties dbProp, boolean startup) throws java.sql.SQLException
DBConnectionPool
instance.dbURL
- The URL used for database access.dbProp
- The properties used for database access.startup
- If the connections should be initialized.java.sql.SQLException
- If a connection to the database can't be made.public DBConnectionPool(java.lang.String dbURL, java.lang.String userid, java.lang.String passwd) throws java.sql.SQLException
DBConnectionPool
instance.dbURL
- The URL used for database access.userid
- The UserID used for database access.passwd
- The password used for database access.java.sql.SQLException
- If a connection to the database can't be made.public DBConnectionPool(java.lang.String dbURL, java.lang.String userid, java.lang.String passwd, boolean startup) throws java.sql.SQLException
DBConnectionPool
instance.dbURL
- The URL used for database access.userid
- The UserID used for database access.passwd
- The password used for database access.startup
- If the connections should be initialized.java.sql.SQLException
- If a connection to the database can't be made.public DBConnectionPool(java.lang.String dbURL, java.lang.String userid, java.lang.String passwd, java.util.Properties dbProp, boolean startup) throws java.sql.SQLException
DBConnectionPool
instance.dbURL
- The URL used for database access.userid
- The UserID used for database access.passwd
- The password used for database access.dbProp
- The properties used for database access.startup
- If the connections should be initialized.java.sql.SQLException
- If a connection to the database can't be made.Method Detail |
public void setDatabaseURL(java.lang.String value)
value
- The new database URL value.public void setDatabaseUserID(java.lang.String value)
value
- The new Default Database UserID value to use.public void setDatabasePassword(java.lang.String value)
value
- The new defaultDatabasePassword value to use.public void setDatabaseProperties(java.util.Properties value)
value
- The new defaultDatabaseProperties value to use.public void setMaxSize(int value)
DBConnectionPool
intstance. If maximum size is lower than
preferred size, then the preferred size will be reduced to match maximum
size.value
- The new maximum size value to use.public void setPreferredSize(int value)
DBConnectionPool
instance. If the number of connections
exceeds this value, then returned connections will be closed. The maximum
size of the Pool overrides this value, if maximum size is lower than the
preferred size, then the preferred size will be reduced to match maximum
size.value
- The new preferred size value to use.public void setHoldUntilConnectionAvailable(boolean value)
DBConnectionPool
instance should always wait to return a available connection from a call to
getDBConnection()
.value
- The new hold until a connection is available value.public void setDatabaseLogging(boolean value)
value
- The new logging value.public java.lang.String getDatabaseURL()
public java.lang.String getDatabaseUserID()
public java.lang.String getDatabasePassword()
public java.util.Properties getDatabaseProperties()
public int getMaxSize()
DBConnectionPool
. When new
connections are created, the total number of connections in the pool will
never exceed the Maximum Size.DBConnectionPool
.public int getPreferredSize()
DBConnectionPool
. When the connections are initialized, the
number of connections created will equal the preferred size. When
connections are returned to the pool, the pool will close any connections
returned when the pool size has already reached the preferred size.DBConnectionPool
.public boolean getHoldUntilConnectionAvailable()
DBConnectionPool
will cause the client
to wait when a request is made for a new database connection, but there
are none available to return and the maximum size has been reached.DBConnectionPool
will block the client
thread until a connection is available.public boolean getDatabaseLogging()
DriverManager
LogWriter.public java.sql.Connection getDBConnection() throws java.sql.SQLException
java.sql.Connection
instance from the
DBConnectionPool
instance. If there are available database
connections, then a connection from the available pool is returned. If
there are no available connections, then a connection is created, until
the database reaches the maximum size. Once the maxSize is reached, then
the behavior depends on if HoldUntilConnectionAvailable is set to
true
or false
. If true
, it will
hold until a connection is ready, then a call to this method won't return
until there is an available connection. Otherwide, it will return a
null
value.Connection
instance, or null if the number of
connections is at maxSize and HoldUntilConnectionAvailable is set to
false
.java.sql.SQLException
- If unable to make a connection if a new
Connection
instance needs to be created
or if the number of connections is at maxSize and
HoldUntilConnectionAvailable is set to
false
.public int getAvailablePoolSize()
public int getUsedPoolSize()
public void releaseDBConnection(java.sql.Connection theConnection)
Connection
instance back into the pool.theConnection
- The Connection
instance to be returned.public void closeCurrentConnections()
public void startupDatabaseConnections() throws java.sql.SQLException
java.sql.SQLException
- Indicates there is a problem creating the
connection.protected void closeConnection(java.sql.Connection conn) throws java.sql.SQLException
Connection
instance. If it is a pooled connection,
closes the contained connection.java.sql.SQLException
- Indicates there is a problem closing the
connection.protected java.sql.Connection createDatabaseConnection() throws java.sql.SQLException
Connection
instance, wrapping the connection in
an adapter to provide all the pooled connection abilites.java.sql.SQLException
- Indicates there is a problem creating the
connection.protected java.sql.Connection getDirectDatabaseConnection() throws java.sql.SQLException
Connection
instance, directly connected to the
database.java.sql.SQLException
- Indicates there is a problem creating the
connection.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |