The constructor of the DBConNetionManager is a private function to avoid other classes to create its examples.
Private dbconnectionmanager () {
INIT ();
}
DBConNetionManager calls the getInstance () method to get a reference to the single instance of this class.
Static synchronized public dbconnectionManager getInstance () {
IF (instance == null) {
Instance = New dbconnectionManager ();
}
CLIENTS ;
Return Instance;
}
A single instance is created during the first call, and the later call returns the static application of the instance. A counter record all customers until the customer releases a reference. This counter is used to coordinate closing the connection pool.
First, initialization
The constructor calls a private init () function initialization object.
Private vidinit () {
InputStream IS = getClass (). GetResourceAsStream ("/ db.properties");
Properties dbprops = new profment ();
Try {
DBProps.Load (IS);
}
Catch (Exception E) {
System.err.Println ("Can NOT Read The Properties File."
"Make Sure DB.Properties IS 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 to open an external input file. The location of the file depends on the class loader, and the standard class loader starts searching from classpath. The db.properties file is a file in a Porperties format, saved in the Key-Value pair defined in the pool. Some commonly used properties below can be defined:
Drivers in the JDBC driver separated by spaces
Logfile log file absolute path
Other properties are also used in each connection pool. These properties begin with the name of the connection pool:
.url database JDBC URL
.maxconn maximum connections. 0 indicates unlimited.
.user connection pool username
.password related password
The URL attribute is necessary, other properties are optional. The username and password must match the defined database.
Below is an example of a DB.Properties file under the Windows platform. There is an instantdb connection pool and a data source for the Access database connected to the 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.prpidb.maxconn=2
Access.url = JDBC: ODBC: Demo
Access.user = DEMO
Access.password = Demopw
Note that the anti-slope must be both written under the Windows platform.
Initialization method init () Creates a Porperties object and loads the DB.Properties file and then read the log file properties. If the log file is not named, use the default name DBConnectionManager.log in the current directory. In this case, a system error is recorded.
Method LOADDRIVERS () Register all the specified JDBC drivers to load.
Private void loadingDrivers (property props) {
String driverclasses = props.getproperty ("drivers");
StringTokenizer ST = New StringTokenizer (DriverClasses);
while (st.hasmorelements ()) {
String driverclassname = st.nextToken (). TRIM ();
Try {
Driver driver = (driver)
Class.Forname (driverclassname) .newinstance ();
Drivermanager.RegisterDriver (driver);
Drivers.addeElement (driver);
"Registered JDBC Driver" DriverClassName;
}
Catch (Exception E) {
LOG ("Can Not Register JDBC Driver:"
Driverclassname ", Exception:" E);
}
}
}
LoadDrivers () Use StringTokenizer to divide the Dirvers properties into separate Driver strings and load each driver into the Java virtual machine. The driver is registered in the JDBC's DriverManager and joins a private vector driver. Vector drivers are used to close and log out of all drivers.
Then, the DBConnectionPool object is created by private method CreatePools ().
Private void createpools (property prots) {
ENUMERATION PROPNAMES = props.propertynames ();
While (propNames.haASMoreElements ()) {
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 enumerated object saves all attribute names, if the property name is ended, it is indicated 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 is an index, and the connection pool object is used as a value.