* loads properties and initializes the instance with its values.
*/
private void init() throws sqlexception {
log = new printwriter(system.err);
loaddrivers();
//createpools(0);
}
/**
* loads and registers all jdbc drivers. this is done by the
* dbconnectionmanager, as opposed to the dbconnectionpool,
* since many pools may share the same driver.
*
* @param props the connection pool properties
*/
private void loaddrivers() throws sqlexception {
drivermanager.registerdriver(new oracle.jdbc.driver.oracledriver());
}
/**
* writes a message to the log file.
*/
private void log(string msg) {
log.println(new date() + ": " + msg);
}
/**
* writes a message with an exception to the log file.
*/
private void log(throwable e, string msg) {
log.println(new date() + ": " + msg);
e.printstacktrace(log);
}
/**
* this inner class represents a connection pool. it creates new
* connections on demand, up to a max number if specified.
* it also makes sure a connection is still open before it is
* returned to a client.
*/
class dbconnectionpool {
private int checkedout;
private vector freeconnections = new vector();
private int maxconn;
private string name;
private string password;
private string url;
private string user;
/**
* creates new connection pool.
*
* @param name the pool name
* @param url the jdbc url for the database
* @param user the database user, or null
* @param password the database user password, or null
* @param maxconn the maximal number of connections, or 0
* for no limit
*/
public dbconnectionpool(string name, string url, string user,
string password,int maxconn) {
this.name = name;
this.url = url;
this.user = user;
this.password = password;
this.maxconn = maxconn;
}
/**
* checks in a connection to the pool. notify other threads that
* may be waiting for a connection.
*
* @param con the connection to check in
*/
public synchronized void freeconnection(connection con) {
// put the connection at the end of the vector
freeconnections.addelement(con);
checkedout--;
notifyall();
}
/**
* checks out a connection from the pool. if no free connection
* is available, a new connection is created unless the max
* number of connections has been reached. if a free connection
* has been closed by the database, it's removed from the pool
* and this method is called again recursively.
*/
public synchronized connection getconnection() throws sqlexception {
connection con = null;
if (freeconnections.size() > 0) {
// pick the first connection in the vector
// to get round-robin usage
con = (connection) freeconnections.firstelement();
freeconnections.removeelementat(0);
if (con.isclosed()) {
log("removed bad connection from " + name);
// try again recursively
con = getconnection();
}
}
else if (maxconn == 0 || checkedout < maxconn) {
con = newconnection();
}
if (con != null) {
checkedout++;
}
return con;
}
/**
* closes all available connections.
*/
public synchronized void release() throws sqlexception {
enumeration allconnections = freeconnections.elements();
while (allconnections.hasmoreelements()) {
connection con = (connection) allconnections.nextelement();
con.close();
}
freeconnections.removeallelements();
}
/**
* creates a new connection, using a userid and password
* if specified.
*/
private connection newconnection() throws sqlexception {
connection con = null;
if (user == null) {
con = drivermanager.getconnection(url);
}
else {
con = drivermanager.getconnection(url, user, password);
}
log("created a new connection in pool " + name);
return con;
}
}
}