The DBConnectionPool class represents a database connection pool identified by the URL. As front, we have mentioned that the URL of the JDBC consists of three parts: protocol identifier (for example, ODBC.Oracle), and database identity (related to a specific database). The connection pool also has a name for customer program reference. In addition, there is a username, a password and a maximum allowable connection number. If the web application allows all users to use certain database operations, while others can be restricted, you can create two connection pools, with the same URL, different user name, and password, handle two types of different operation permissions separately . The DBConnectionPool is now described in detail below:
First, DBConnectionPool constructors
The constructor acquires all the parameters described above:
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;
}
Save all parameters in the instance variable.
Second, open a connection from the pool
DBConnectionPool provides two ways to check. Both methods returns an available connection, and if there is no extra connection, create a new connection. If the maximum number of connections has reached, the first method returns NULL, and the second method waits for a connection to be released by other processes.
Public synchronized connection getConnection () {
Connection con = NULL;
IF (FreeConnections.Size ()> 0) {
// pick the first connection in the vector
// TO GET ROUND-ROBIN USAGE
Con = (connection) freeConnections.FirstEth ();
FreeConnections.RemoveElementat (0);
Try {
IF (con?isclosed ()) {
"Removed Bad Connection from" Name);
// Try Again Recursively
CON = getConnection ();
}
}
Catch (SQLException E) {
"Removed Bad Connection from" Name);
// Try Again Recursively
CON = getConnection ();
}
}
Else IF (MaxConn == 0 || Checkedout CON = NewConnection (); } IF (con! = null) { CHECKEDOUT ; } Return con; } All idle connection objects are saved in a Vector called FreeConnections. If there is at least one idle connection, getConnection () returns the first connection. Next, it will be seen that the connection released by the process returns to the end of FreeConnections. In this way, the risk of accidentally shutting down is to minimize the risk of being unconnected. Before returning to the customer, isclosed () checks if the connection is valid. If the connection is turned off, or an error occurs, the method recursively calls another connection. If there is no connection available, the method checks if the maximum number of connections is set to 0 indicates an infinite connection, or the maximum number of connections is reached. If you can create a new connection, create a new connection. Otherwise, return NULL. Method NewConnection () is used to create a new connection. This is a private method, based on username and password to determine if a new connection can be created. PRIVATE connection newconnection () { Connection con = NULL; Try { IF (user == NULL) { Con = DriverManager.getConnection (URL); } Else { Con = DriverManager.getConnection (URL, User, Password); } "CREATED A New Connection in Pool" Name); } Catch (SQLException E) { LOG (E, "Can Not Create A New Connection for" URL); Return NULL; } Return con; } JDBC's DriverManager provides a series of getConnection () methods, you can create a connection using the URL and username, password and other parameters. The second getConnection () method has a timeout parameter Timeout, when the number of milliseconds specified by this parameter indicates the time when the customer is willing to wait for a connection. This method calls the previous method. Public synchronized connection getConnection (long timeout) { Long StartTime = New Date (). gettime (); CONNECTION CON; While (con = getConnection ()) == null) { Try { WAIT (TIMEOUT); } Catch (InterruptedExcect E) {} IF ((New Date (). gettime () - starttime)> = timeout) { // Timeout Has Expired Return NULL; } } Return con; } Local variables StartTime initializes the current time. A While loop first tries to get a connection, if a failed, the wait () function is called to wait for the time. It will be seen later that the wait () function returns when another process calls notify () or NOTIFYAll (), or wait until the time is over. In order to determine WAIT () because the cause is returned, we use the start time to subtract the current time and check if it is greater than Timeout. If the result is greater than Timeout, return null, otherwise call the getConnection () function here. Fourth, return a connection back to the pool There is a FreeConnection method in the DBConnectionPool class to return the connection to the connection pool. Public synchronized void freeconnection (connection con) {// put the connection at the end of the vector FreeConnections.AddeElement (con); Checkedout -; NotifyAll (); } The connection is added in the final of the freeConnections vector, the number of connections is reduced by 1, and the NotifyAll () function notifies other waiting customers now have a connection. V. Close Most servlet engines provide a complete shutdown method. The database connection pool needs to be notified to close all connections correctly. DBConnectionManager is responsible for coordinating the closing event, but the connection is turned off by each connection pool. Method relase () is called by DBConnectionManager. Public synchronized void release () { Enumeration allConnections = freeConnections.efficient (); While (allConnections.haASMoreElements ()) { Connection Con = (Connection) AllConnections.nexTelement (); Try { C. close (); Log ("Closed Connection for Pool" Name); } Catch (SQLException E) { LOG (E, "Can Not Close Connection for Pool" Name); } } FreeConnections.RemoveAllelements (); } This method traverses the FreeConnections vector to close all connections.