Dbconnetionmanager constructors are private functions to prevent other classes from creating instances of them.
Private Dbconnectionmanager () {
Init ();
}
The Dbconnetionmanager client invokes the getinstance () method to get a reference to a single instance of the class.
Static synchronized public Dbconnectionmanager getinstance () {
if (instance = = null) {
Instance = new Dbconnectionmanager ();
}
clients++;
return instance;
}
A single instance is created on the first call, and a subsequent call returns the static application of that instance. A counter records all the number of customers until the customer releases the reference. This counter is used later to coordinate the shutdown of the connection pool.
First, initialization
The constructor calls a private init () function to initialize the object.
private void init () {
InputStream is = GetClass (). getResourceAsStream ("/db.properties");
Properties Dbprops = new properties ();
try {
Dbprops.load (IS);
}
catch (Exception e) {
System.err.println ("Can not read the properties file." +
"Make sure db.properties are in the CLASSPATH");
Return
}
String logFile = Dbprops.getproperty ("LogFile",
"DBConnectionManager.log");
try {
Log = new PrintWriter (new FileWriter (LogFile, True), true);
}
catch (IOException e) {
System.err.println ("Can not open the log file:" + logFile);
Log = new PrintWriter (SYSTEM.ERR);
}
Loaddrivers (Dbprops);
Createpools (Dbprops);
}
Method getResourceAsStream () is a standard method for opening an external input file. The location of the file depends on the class loader, and the standard class loader starts the search from Classpath. The Db.properties file is a porperties-formatted file that holds the Key-value pairs defined in the connection pool. Some of the following commonly used properties can be defined:
Drivers the list of JDBC drivers separated by spaces
LogFile the absolute path of the log file
Other properties are also used in each connection pool. These properties begin with the name of the connection pool:
. The jdbc URL for the URL database
. maxconn maximum number of connections. 0 represents Infinity.
. user Connection Pool username
. Password related passwords
The URL property is required and the other attributes are optional. The username and password must match the database defined.
The following is an example of a db.properties file under the Windows platform. There is a INSTANTDB connection pool and a data source for an Access database that is connected via ODBC, named Demo.
Drivers=sun.jdbc.odbc.jdbcodbcdriver Jdbc.idbdriver
Logfile=d:\\user\\src\\java\\dbconnectionmanager\\log.txt
Idb.url=jdbc:idb:c:\\local\\javawebserver1.1\\db\\db.prp
idb.maxconn=2
Access.url=jdbc:odbc:demo
Access.user=demo
Access.password=demopw
Note that the backslash must be double written under the Windows platform.
Initialization Method Init () creates a Porperties object and loads the Db.properties file, and then reads the log file properties. If the log file is not named, it is created in the current directory using the default name DBConnectionManager.log. In this case, a system error is recorded.
Method Loaddrivers () registers and mounts all the specified JDBC drivers.
private void Loaddrivers (Properties props) {
String driverclasses = Props.getproperty ("Drivers");
StringTokenizer st = new StringTokenizer (driverclasses);
while (St.hasmoreelements ()) {
String driverclassname = St.nexttoken (). Trim ();
try {
Driver Driver = (Driver)
Class.forName (Driverclassname). newinstance ();
Drivermanager.registerdriver (driver);
Drivers.addelement (driver);
Log ("Registered JDBC driver" + driverclassname);
}
catch (Exception e) {
Log ("Can not register JDBC driver:" +
Driverclassname + ", Exception:" + e);
}
}
}
Loaddrivers () uses StringTokenizer to divide the Dirvers property into separate driver strings and loads each driver into the Java Virtual machine. An instance of the driver is registered in the JDBC DriverManager and is added to a private vector drivers. Vector drivers is used to close and unregister all drivers.
The Dbconnectionpool object is then created by the private Method Createpools ().
private void Createpools (Properties props) {
Enumeration propnames = Props.propertynames ();
while (Propnames.hasmoreelements ()) {
String name = (string) propnames.nextelement ();
if (Name.endswith (". url")) {
String poolname = name.substring (0, Name.lastindexof ("."));
String url = props.getproperty (poolname + ". url");
if (url = = null) {
Log ("No URL specified for" + poolname);
Continue
}
String user = Props.getproperty (poolname + ". User");
String password = props.getproperty (poolname + ". Password");
String maxconn = props.getproperty (poolname + ". Maxconn", "0");
int Max;
try {
max = integer.valueof (maxconn). Intvalue ();
}
catch (NumberFormatException e) {
Log ("Invalid maxconn value" + Maxconn + "for" +
poolname);
max = 0;
}
Dbconnectionpool pool =
New Dbconnectionpool (poolname, url, user, password, max);
Pools.put (poolname, pool);
Log ("initialized pool" + poolname);
}
}
}
An enumeration object holds all the property names, and if the property name ends with a. URL, it means that a connection pool object needs to be instantiated. The created connection pool object is saved in a Hashtable instance variable. The connection pool name as the index, and the connection pool object as a value.