Source:
http://blog.9cbs.net/legenDinfo/archive/2004/12/10/211729.aspx
2.2JDBC connection pool
In the interface of the standard JDBC, there is no management method for resources. Therefore, the default resource management is responsible for applying itself. Although in the JDBC specification, the closure / recovery of resources and other reasonable applications are mentioned many times. But the most secure way is to provide effective management means for applications. Therefore, JDBC provides a management standard interface implemented by a database manufacturer for a third-party application server (Connection Pool). The concept of connecting pool is introduced, that is, manages the resources of the database with the mechanism of the buffer pool.
There are three categories: the most common resources of JDBC:
-Connection: Database connection.
-Statement: Session declaration.
-ResultSet: Results gauge.
There is the following relationships, respectively:
This is a relationship of '- parent - sub', the management of Connection, is the management of database resources. For example: If you want to determine if a database connection is timeout, you need to make sure it (all) substi is timeout, the same, you need to determine if all related results are timeout; before turning off the connection, you need to close all relevant Statement and resultSet.
Therefore, the role of connecting pool is not just simply managing Connection, but also on Statement and ResultSet.
2.3 ConnectionPool and Resource Management
ConnectionPool controls management Connection, Statement, and ResultSet within a certain number of upper limitations in a certain number of caps. The resources of any database are limited, and more data services cannot be obtained if they are exhausted.
In most cases, the depletion of resources is not due to the normal load of the application, but the cause of the program.
In actual work, data resources are often bottleneck resources, and different applications will access the same data source. One of the applications run out of the database resources, other applications can not run normally. Therefore, the first task of ConnectionPool is to limit: the maximum resource that can be owned each application or system. That is to determine the size of the connection pool (Poolsize).
The second task of ConnectionPool: Use resources to maximize the use of resources to the poolsize range, shorten the use cycle of database access. In many databases, connectivity is not the smallest unit of resources, and the STATEMENT resource is more important than Connection. Take oracle as an example:
Each application will establish a connection for communication on a physical network (such as TCP / IP network), and you can also apply for a certain number of statement on this connection. The active statement number of available connections can reach several hundred. While saving network resources, shortening each session cycle (the establishment of physical connection is a fee). However, in the general application, most of them operate in accordance with the 2.1 example, there is 10 programs, which will generate 10 physical connections, each Statement, separately, is a very resource waste. ConnectionPool can solve this problem, let hundreds, hundreds of Statement only occupy the same physical connection, and play the original advantages of the database. The total number of STATEMENTs available to the application can be obtained by ConnectionPool's effective management of resources.
(Candle Physical Connection) X (number of STATEMENTs available for each connection)
For example, some database can be established at the same time of 200, each connection can provide 250 Statement at the same time, then the total number of concurrent STATEMENT provided by the application is: 200 x 250 = 50,000. This is a concurrent number, very few systems will break through this level. Therefore, at the beginning of this section, it is pointed out that the depletion of resources is related to the direct management of applications.
For the optimization management of resources, it is largely relying on whether the database itself has a JDBC DRIVER. Some databases JDBC Driver does not support logical connection between Connection and Statement, such as SQL Server, we can only wait for her own update version.
For resource application, release, recycling, sharing, and synchronization, these management are complex and precise. So, another function of ConnectionPool is to encapsulate these operations, providing simple, or even do not change the call interface of the application style.
3. Simple JDBC Connection Pool Realization According to the principle mechanism in Chapter 2, Snap-ConnectionPool (a simple and fast connection pool tool) implements the effective management function of the connection pool according to the partial JDBC specification. 3.1 System Description In the JDBC specification, apply the resources of the database through the Driver Interface Direct Method. For effective, reasonable management of resources, the connection pool is added between the application and JDBC DRIVER: SNAP-ConnectionPool. And through the object-oriented mechanism, most of the operation of the connection pool is transparent. See the following figure, Snap-ConnectionPool system: The figure shows in the figure, three logical resource objects are generated inside the SNAP-ConnectionPool in the Snap-ConnectionPool, respectively. They are also mainly managed operation objects that connect pools and inherit the corresponding dependence in JDBC. Such a system has the following features: - Transparency. Provide resource management services without changing the application of the original JDBC driver interface. Application system, like the original JDBC, using the logical object resources provided by the connection pool. Simplified the application's connection pool transformation. - Resource package. Complex resource management is encapsulated inside Snap-ConnectionPool and does not require excessive interference. The reliability of the management operation is guaranteed by the connection pool. Interference (such as: Active Close Resources), only the role of optimizing system performance, and missing operations will not have a negative impact. - rational application of resources. According to the dependence of resources in JDBC, Snap-ConnectionPool is not only buffering Connection, but also has corresponding mechanisms to Statement. In 2.3, the relationship between Connection and Statement is reasonably used, and resources can be used in greater limits. So, Snap-ConnectionPool encapsulates the Connection resource, manages the PooledConnection inside, provides more Statement resources for the application. - Resource chain management. Snap-connectionPool includes three logical objects that inherit the dependence between the corresponding objects in the JDBC. In internal management, chain management is also performed in accordance with the dependent relationship. For example, if it is determined whether a connection is timeout, it is necessary to determine whether the STATEMENT is active; it is judged that Statement should also be based on the activity of ResultSet. 3.2 Connecting Pool Concentration Management ConnectionManager ConnectionPool is the connection pool object of Snap-ConnectionPool. In Snap-ConnectionPool, you can specify a plurality of different connection pools (ConnectionPool) for application services. ConnectionManager Manages all connection pools, each connection pool differently. Adapted to different database types by profile. As shown below: With the ConnectionManager, you can manage multiple different connecting pools at the same time, and provide a universal management interface. In the application system through the ConnectionManager and related profiles, you can scatter the database configuration information (including: database name, user, password, etc. information) in a file in a file. Easy to maintain the system's maintenance.
3.3 Connection pool use example to the standard JDBC of 2.1, change to use the connection pool, the result is as follows: import java.sql. *;
Import net.snapbug.util.dbtool. *;
...
..ConnectionPool dbconn = connectionManager
.GetConnectionPool ("testoracle");
Statement ST = dbconn.createstatement ();
ResultSet RS = St.executeQuery
"SELECT * from demo_table");
...
Some Data Source Operation
In Herers.Close (); st.close ();
In the example, SNAP-ConnectionPool encapsulates the application of the application to the Connection. As long as the JDBC gets the connection pool (bold portion), other data operations can not be modified for the CONNECTIONPOOL. According to this way, Snap-ConnectionPool helps to use effectively manage database resources. If the application ignores the release of the final resource: rs.close () and st.close (), the connection pool will be automatically recycled through the time-out mechanism. 4. Small knot should be Snap-ConnectionPool or other database connection pools, it should have basic functions: - Protection of source database resources - Take advantage of the effective resource of the database - Simplify the application of database interfaces, closed resource management. - A automatic recycling and finishing of the legacy resource, improve the rehabilitation utilization rate of resources. Under this premise, the application can put more energy in their respective business logic. Database resources are no longer a bottleneck of the system. Note: Download free SNAP-ConnectionPool and more detailed documentation for free at website www.snapbug.net.