public class ConnectionManager extends Object
This class represents a connection manager, which creates a JDBC driver manager and manages established database connections. This class lets you specify the following settings for JDBC connections:
You can change only the following settings when the connection manager is running:
setMinPool and setMaxPool)
setMsWait and setMsInterval)
You cannot set any other setting while the connection manager is running.
To change other settings, shut down the connection manager using the
shutDown method, then change
the settings. Then, start the connection manager
using the startUp method.
If you use a connection manager to manage your database connections, the connection manager can also extend a SynerJ transaction to include JDBC database transactions. In other words, if the JDBC database transactions occur within a SynerJ transaction, the JDBC database transactions are committed or rolled back when the SynerJ transaction committed or rolled back.
You can set up your connection manager to manage database connections in the following ways:
You also have the choice of either defining the connection manager as a service object typed as a ConnectionManager object, or defining the connection manager by dynamically creating a ConnectionManager object in your code. If you define the connection manager as a service object, the SynerJ partitioning system can help you determine how to define partitions that contain the service object by being aware of where JDBC drivers are installed, for example. If you define the ConnectionManager object dynamically, then you need to keep such issues in mind when you define the partitions whose code defines the ConnectionManager objects; the partitioning system will not help you.
In this situation, the connection manager establishes a new connection each time you request a connection. By default, the connection manager does not establish and maintain a pool of connections. In this case, you can leave the maximum and minimum number of pooled connections set to 0. Each time you need a connection to the database, use the getConnection method. You can use the default user name, password, and database URL for the current connection manager by invoking the getConnection method with no parameters. The default settings are specified in one of the ConnectionManager constructors when you create the connection manager. You can also specify a user name, password, and database URL on the getConnection method.
import java.sql.*;
import com.sun.jdo.api.persistence.support.*;
Connection con = myTransaction.getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM T1");
When you create a connection manager using the ConnectionManager class, you can have the connection manager establish a pool of connections for a given user name and password to allow a thread to using an existing connection instead of waiting for a new database connection to be created.
To create a connection manager with a pool of connections using a service object:
If you set the values for the minimum and maximum connections to 0, then no pool of connections is established. Any value greater than 0 means that a pool of connections is established. The maximum value must be equal to or greater than the value of the minimum value. If you set the maximum and minimum as equal values, the number of connections established for the pool will be constant.
The connection manager establishes the minimum number of connections when it starts up using the default database URL, user name, and password.
To get one of the pooled connections, you can use the
getConnection
method with no parameters. If all the existing connections are in
use, the connection manager establishes another connection with
the same database URL, user name, and password and adds it to the pool,
up to the specified maximum number of connections.
When you have finished using a connection, you can use the
close() method to return the connection to the pool
of available connections.
The connection manager periodically checks its pool of connections,
and can reduce the number of established connections to the minimum
number, if enough connections are not in use.
At runtime, you can change the maximum and minimum number of connections,
because the ConnectionManager
is a component.
You can have the connection manager establish and maintain a pool of connections and have the connection manager establish new connections on a request-by-request basis.
import com.sun.jdo.api.persistence.support.*;
void getT1Data() {
// The following connection is from the connection pool.
Connection myConn = myTransaction.getConnection();
Statement myStatement = myConn.createStatement();
ResultSet myResults = myStatement.executeQuery(
"SELECT * FROM T1);
// Free the connection; it is returned to the connection pool.
myConn.close();
// The connection manager creates a new connection for the
// following request.
Connection yourConn = myConnMgr.getConnection(
"data:oracle:thin:@CUSTOMERDB:1521:ORCL", "paul", "omni8");
Statement yourStatement = yourConn.createStatement();
ResultSet yourResults = yourStatement.executeQuery(
"SELECT Customer, Date, Amount FROM Orders");
.
.
.
}
| Constructor and Description |
|---|
ConnectionManager()
Default constructor.
|
ConnectionManager(String driverName)
Creates a new connection manager and loads the named
JDBC driver.
|
ConnectionManager(String driverName,
String url)
Creates a new connection manager, loads the named JDBC driver, and
sets the default database URL for the new connection manager.
|
ConnectionManager(String driverName,
String url,
String userName)
Creates a new connection manager, loads the named JDBC driver, and
sets the default database URL and user name for the new
connection manager.
|
ConnectionManager(String driverName,
String url,
String userName,
char[] password)
Creates a new connection manager, loads the named JDBC driver, and
sets the default database URL, user name, and password for the new
connection manager.
|
ConnectionManager(String driverName,
String url,
String userName,
char[] password,
int minPool,
int maxPool)
Creates a new connection manager, loads the named JDBC driver, and sets
the default database URL, user name, password and minimum and maximum
connection pool sizes for the new
connection manager.
|
ConnectionManager(String driverName,
String url,
String userName,
char[] password,
int minPool,
int maxPool,
int msWait)
Creates a new connection manager, loads the named JDBC driver, and defines
the default values for the database URL, user name, password, minimum and maximum
connection pool sizes, and the length of time to wait for a
database connection.
|
ConnectionManager(String driverName,
String url,
String userName,
char[] password,
int minPool,
int maxPool,
int msWait,
int msInterval)
Creates a new connection manager, loads the named JDBC driver, and
defines the default values for the database URL, user name, password, minimum and maximum
connection pool sizes, the length of time to wait for a database connection,
and how frequently to try again to get a database connection.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
finalize()
Disconnects all free database connections managed by
the current connection manager and sets the shutDownPending
flag to true.
|
Connection |
getConnection()
Establishes a connection to the default database URL
using the default user name and password.
|
Connection |
getConnection(String userName,
char[] password)
Establishes a connection to the database at the default database URL using
the specified user name and password.
|
Connection |
getConnection(String url,
String userName,
String password)
Establishes a connection to the specified
database URL using the specified user name and password.
|
String |
getDriverName()
Gets the name of the JDBC driver.
|
int |
getLoginTimeout() |
int |
getMaxPool()
Gets the maximum number of pooled connections for the current
connection manager.
|
int |
getMinPool()
Gets the minimum number of pooled connections for the current
connection manager.
|
int |
getMsInterval()
Gets the amount of time, in milliseconds,
between the connection manager's attempts to get a pooled connection.
|
int |
getMsWait()
Gets the amount of time, in milliseconds, the connection manager should spend trying
to get a pooled connection, which is the amount of time a requester might wait.
|
char[] |
getPassword()
Gets the default database password for the current
connection manager.
|
String |
getURL()
Gets the default database URL for the data source.
|
String |
getUserName()
Gets the default database user name for the current
connection manager.
|
protected void |
replaceFreeConnection(ConnectionImpl c)
Called by ConnectionImpl to save a connection when it is no longer used.
|
void |
setDriverName(String driverName)
Sets the name of the JDBC driver.
|
void |
setLoginTimeout(int seconds) |
void |
setMaxPool(int maxPool)
Sets the maximum number of pooled connections for the current
connection manager.
|
void |
setMinPool(int minPool)
Sets the minimum number of pooled connections for the current
connection manager.
|
void |
setMsInterval(int msInterval)
Sets the amount of time, in milliseconds, between the connection
manager's attempts to get a pooled connection.
|
void |
setMsWait(int msWait)
Sets the amount of time, in milliseconds, the connection manager should spend trying
to get a pooled connection, which is the amount of time a requester might wait.
|
void |
setPassword(char[] password)
Sets the default database password for the current connection manager.
|
void |
setURL(String url)
Sets the default database URL for the data source.
|
void |
setUserName(String userName)
Sets the default database user name for the current
connection manager.
|
void |
shutDown()
Disconnects all free database connections managed by
the current connection manager and sets the shutDownPending
flag to true.
|
void |
startUp()
Starts up this ConnectionManager by loading the proper
JDBC driver class and initializing the pool if necessary.
|
String |
toString()
Returns a string representation of the current ConnectionManager object.
|
public ConnectionManager()
public ConnectionManager(String driverName) throws ClassNotFoundException, SQLException
driverName - name of JDBC driver.ClassNotFoundException - if the driver cannot be found.SQLException - if a SQL error is encountered.public ConnectionManager(String driverName, String url) throws ClassNotFoundException, SQLException
driverName - the name of JDBC driver.url - the database URL for the data source.ClassNotFoundException - if the driver cannot be found.SQLException - if a SQL error is encountered.public ConnectionManager(String driverName, String url, String userName) throws ClassNotFoundException, SQLException
driverName - the name of JDBC driver.url - the default database URL for the data source.userName - the default user name for database connections.ClassNotFoundException - if the driver cannot be found.SQLException - if a SQL error is encountered.public ConnectionManager(String driverName, String url, String userName, char[] password) throws ClassNotFoundException, SQLException
driverName - the name of JDBC driver.url - the default database URL for the data source.userName - the default user name for database connections.password - the default password for database connections.ClassNotFoundException - if the driver cannot be found.SQLException - if a SQL error is encountered.public ConnectionManager(String driverName, String url, String userName, char[] password, int minPool, int maxPool) throws ClassNotFoundException, SQLException
If minPool and maxPool are 0, connection pooling is disabled. If minPool is greater than 0 and maxPool is greater than or equal to minPool, this constructor creates a connection pool containing minPool connections.
driverName - the name of JDBC driver.url - the default database URL for the data source.userName - the default user name for database connections.password - the default password for database connections.minPool - the default minimum size of the connection pool.maxPool - the default maximum size of the connection pool.ClassNotFoundException - if the driver cannot be found.SQLException - if a SQL error is encountered or if the
specified value of minPool is not less than or equal to the specified
value of maxPool.public ConnectionManager(String driverName, String url, String userName, char[] password, int minPool, int maxPool, int msWait) throws ClassNotFoundException, SQLException
If minPool and maxPool are 0, connection pooling is disabled. If minPool is greater than 0 and maxPool is greater than or equal to minPool, this constructor creates a connection pool containing minPool connections.
If msWait is set to 0, the connection manager does not try again to create or return a database connection if the first try fails. For any other value, the connection manager waits 1000 milliseconds (ms) (1 second) before trying again. If the msWait value is less than 1000 ms, the connection manager waits 1000 ms before trying. The connection manager continues trying until the value specified by msWait is met or exceeded.
If you want to set the interval length yourself, you can use
the ConnectionManager constructor that
specifies the msInterval parameter or the setInterval
method.
driverName - the name of JDBC driver.url - the default database URL for the data source.userName - the default user name for database connections.password - the default password for database connections.minPool - the default minimum size of the connection pool.maxPool - the default maximum size of the connection pool.msWait - the total number of milliseconds to wait for a successful connection.ClassNotFoundException - if the driver cannot be found.SQLException - if a SQL error is encountered or if the
specified value of minPool is not less than or equal to the specified
value of maxPool.public ConnectionManager(String driverName, String url, String userName, char[] password, int minPool, int maxPool, int msWait, int msInterval) throws ClassNotFoundException, SQLException
If minPool and maxPool are 0, connection pooling is disabled. If minPool is greater than 0 and maxPool is greater than or equal to minPool, this constructor creates a connection pool containing minPool connections.
If msWait or msInterval is set to 0, the connection manager does not try again to create or return a database connection if the first try fails.
For any other values greater than 0, the The connection manager continues trying after every specified value for msInterval until the value specified by msWait is met or exceeded. If the value for msInterval is greater than the value for msWait, the connection manager tries again to return a connection once, then fails if it could get a connection.
driverName - the name of JDBC driver.url - the default database URL for the data source.userName - the default user name for database connections.password - the default password for database connections.minPool - the default minimum size of the connection pool.maxPool - the default maximum size of the connection pool.msWait - the total number of milliseconds to wait to get a connection.msInterval - the number of milliseconds to wait before trying again to get a connection.ClassNotFoundException - if the driver cannot be found.SQLException - if a SQL error is encountered or if the
specified value of minPool is not less than or equal to the specified
value of maxPool.public Connection getConnection() throws SQLException
If the current connection manager maintains a
connection pool, this method returns a pooled connection
instead of establishing a new connection.
If all pooled connections are in use, and the total wait time (msWait)
for the connection manager and the retry interval (msInterval) are
not 0, then the connection manager tries to get a database connection
after the retry interval. The connection manager continues to
try until a pooled connection becomes available or
until the total time equals or exceeds the wait time.
If the wait time expires before the connection manager returns
a database connection, this method throws a SQLException
exception with SQLState = "08006".
If the current connection manager is not set to try again for connections
(the wait time is 0) and no pooled connections are available, this
method throws a SQLException exception
with SQLState = "08006".
If the current connection manager is being shut down,
this method throws a SQLException exception with
SQLState = "08003".
SQLException - if no database connection is available.public Connection getConnection(String userName, char[] password) throws SQLException
userName - the database user name.password - the database password.SQLException - if the connection fails.public Connection getConnection(String url, String userName, String password) throws SQLException
url - the database URL for the database.userName - the database user name.password - the database password.SQLException - if the connection fails.public void startUp()
throws ClassNotFoundException,
SQLException
You need to call this method if you are using the ConnectionManager
as a component, or if you use the default constructor and set the
attributes via the setXXX methods.
ClassNotFoundException - if the driver cannot be found.SQLException - if a SQL error is encountered.public void shutDown()
throws SQLException
SQLExceptionprotected void finalize()
public String getDriverName()
setDriverName(java.lang.String)public void setDriverName(String driverName) throws SQLException
driverName - the name of the JDBC driver.SQLException - if the driverName is NULL.getDriverName()public String getURL()
ConnectionManager constructor.
This default is used if you don't specify another database URL
with a getConnection method.
To change this default value, use the setURL method.getConnection(),
setURL(java.lang.String)public void setURL(String url) throws SQLException
getConnection method that specifies a database URL as a parameter.url - URL for this connection manager.SQLException - if the URL is NULL.getConnection(),
getURL()public String getUserName()
ConnectionManager
constructor, and is used if you don't specify another user name
with a getConnection method.
To change this default value, use the setUserName method.getConnection(),
setUserName(java.lang.String)public void setUserName(String userName) throws SQLException
userName - the default user name for the current connection manager.SQLExceptiongetUserName()public char[] getPassword()
ConnectionManager
constructor, and is used if you don't specify another password
with a getConnection method.
To change this default value, use the setPassword method.getConnection(),
setPassword(char[])public void setPassword(char[] password)
throws SQLException
password - the default password for the current connection manager.SQLExceptiongetPassword()public int getMinPool()
To change the minimum number of pooled connections, use the
setMinPool method.
getConnection(),
setMinPool(int)public void setMinPool(int minPool)
throws SQLException
The specified value of the minPool parameter must be:
minPool - the minimum number of pooled connections.SQLException - if the connection manager is being shut down or
if the minPool value is not valid.getMaxPool(),
getMinPool(),
setMaxPool(int)public int getMaxPool()
getConnection
method, the getConnection
method always returns a new connection.
To change the maximum number of pooled connections, use the
setMaxPool method.
setMaxPool(int)public void setMaxPool(int maxPool)
throws SQLException
The specified value of the maxPool parameter must be:
maxPool - the maximum number of pooled connections.SQLException - if the connection manager is being shut down or
if the maxPool value is not valid.getMaxPool(),
getMinPool(),
setMinPool(int)public int getMsWait()
This value is only meaningful when you use the getConnection
to get a pooled connection, which means that no database URL, user name, or
password is specified.
getConnection(),
setMsInterval(int)public void setMsWait(int msWait)
throws SQLException
This value is only meaningful when you use the getConnection
to get a pooled connection, which means that no database URL, user name, or
password is specified.
The connection manager retries after the set interval until the total wait time is equal to or greater than the specified wait time. You can determine the total number of tries for a connection based on wait time and interval settings using the following formula, where msWait is the wait time, msInterval is the time between attempts to get a connection:
tries = msWait/msInterval + 2For example, if msWait is set to 2000 ms and msInterval is set to 1500 ms, then the connection manager will try to get a database connection 3 times before throwing an exception if it has failed.
If the wait time value is less than the set value for the interval between retries, but not zero, the connection manager waits the amount of time specified by the interval, tries once, then returns an exception if it still could not get a connection.
msWait - the wait time in milliseconds.SQLExceptiongetConnection(),
getMsInterval(),
getMsWait(),
setMsInterval(int)public int getMsInterval()
This value is only meaningful when you use the getConnection
to get a pooled connection, which means that no database URL, user name, or
password is specified.
getConnection(),
setMsInterval(int)public void setMsInterval(int msInterval)
throws SQLException
This value is only meaningful when you use the getConnection
to get a pooled connection, which means that no database URL, user name, or
password is specified.
The connection manager retries after the specified interval until the total wait time is equal to or greater than the set wait time. You can determine the total number of tries for a connection based on wait time and interval settings using the following formula, where msWait is the wait time, msInterval is the time between attempts to get a connection:
tries = msWait/msInterval + 2For example, if msWait is set to 2000 ms and msInterval is set to 1500 ms, then the connection manager will try to get a database connection 3 times before throwing an exception if it has failed.
If the wait time value is greater than 0 but less than the set value for the interval between retries, the connection manager waits the amount of time specified by the interval, tries once, then returns an exception if it still could not get a connection.
msInterval - the interval between attempts to get a database connection, in milliseconds.SQLExceptiongetConnection(),
getMsInterval(),
getMsWait(),
setMsWait(int)public String toString()
public void setLoginTimeout(int seconds)
throws SQLException
SQLExceptionpublic int getLoginTimeout()
throws SQLException
SQLExceptionprotected void replaceFreeConnection(ConnectionImpl c)
Copyright © 2019. All rights reserved.