Iii. DBConnectionPool description
This class is implemented in rows 209 to 345. It indicates the connection pool pointing to a database. The database is identified by the jdbc url. A jdbc url consists of three parts: Protocol identifier
(Always jdbc), driver identifier (such as odbc, idb, oracle, etc.), and database identifier (the format depends on the driver ). For example, jdbc: odbc: de
Mo is a jdbc url pointing to the demo database, and the JDBC-ODBC driver is used to access the database. Each connection pool has a client program.
The name used and the optional user account, password, and maximum number of connections. If some database operations supported by Web applications can be performed by all users,
Other operations should be performed by the user with special permission. The connection pool can be defined for the two types of operations respectively. The two connection pools use the same jdbc url, but do not
The same account and password.
The Construction Function of DBConnectionPool requires all the above data as its parameter. As shown in rows 222 to 238, the data is saved as its instance variable:
As shown in rows 252 to 283 and 285 to 305, the customer program can use the two methods provided by the DBConnectionPool class to obtain available connections. What are the similarities between the two?
If there is a available connection in the connection pool, return directly. Otherwise, create a new connection and return it. If no connection is available and the total number of existing connections is equal to the maximum limit
Number, the first method will return null directly, and the second method will wait until there is a available connection.
All available connection objects are registered in the Vector named freeConnections. If the vector contains more than one connection, getConnection ()
Always select the first one. At the same time, because the new available connections always add vectors from the end, the risk of database connections being closed due to long idle periods of time is reduced.
To the minimum extent.
Before returning the available connection to the client program, the first getConnection () method is called to verify that the connection is still valid. If the connection is closed or
When an exception is triggered, getConnection () recursively calls itself to try to obtain another available connection. If no connection is available in vector freeConnections
The getConnection () method checks whether the maximum number of connections has been specified. If specified, check whether the current number of connections has reached the limit. Here
If maxConn is 0, there is no limit. If the maximum number of connections is not specified or the current number of connections is smaller than this value, this method attempts to create a new connection. If creation is successful,
The number of connections in use is increased and a null value is returned.
As shown in rows 325 to 345, creating a new connection is implemented by the newConnection () method. The creation process depends on whether the database account and password have been specified.
The JDBC DriverManager class provides multiple getConnection () methods. These methods use JDBC URLs and other parameters, such as user accounts and passwords.
DriverManager uses the specified jdbc url to determine the driver suitable for the target database and establish a connection.