1. Use database connection pooling to optimize program performance
L Application direct access to links disadvantage
Each request requires a link to the database, and a database creation connection typically consumes relatively large resources and is created for a long time. If the site 100,000 visits a day, the database server needs to create 100,000 connections, a huge waste of database resources, and very easy to cause the database server memory overflow, downtime.
The application directly obtains the connection diagram:
N Users-"service-" dao-"DB"
With connection pooling:
N Users-"service-" dao-"Connection pool (n connections)-" DB
2. Write Database connection pool
L The Javax.sql.DataSource interface should be implemented for writing connection pools. Two overloaded getconnection methods are defined in the DataSource interface:
connection getconnection()
connection getconnection(string Username, string password)
• Steps to implement the DataSource interface and implement the connection pooling function:
• Create a connection to the database in bulk in the DataSource constructor and add the created connection to the LinkedList object.
• Implement the Getconnection method so that the Getconnection method takes a connection from the LinkedList to the user each time it is invoked.
• When the user finishes using connection and calls the Connection.close () method, the collection object should ensure that it returns to the LinkedList instead of the Conn back to the database.
• collection guarantee to return to LinkedList is the difficulty of programming here.
The connection was created by Jdbcutil, and now the connection is created by DataSource, which is bound for the implementation not and specific data, so DataSource should also get the connection using the configuration file method.
3. Database Connection Pool Core code
• Building connection in a connection pool using dynamic proxy technology
Proxyconn = (Connection) proxy.newproxyinstance (this. getclass (). getClassLoader (), Conn.getclass (). Getinterfaces (),new Invocationhandler () {
This is an inner class that returns the conn back to the pool when the Close method is invoked, and other methods execute directly
Public Object Invoke (Object proxy, method method,
Object[] args) throws Throwable {
if (Method.getname (). Equals ("Close")) {
POOL.ADDLAST (conn);
return null;
}
return Method.invoke (conn, args);
}
});
L Dynamic Agent
define two concepts :
The value of a proxy object: primarily used to intercept access to real business objects.
What method the proxy object has. How to generate a proxy object.
L Java provides a proxy class that invokes its Newinstance method to generate a proxy object for an object that requires three parameters when it is used to generate a proxy object:
·1. Build proxy object which class loader to use
2. A proxy object that generates which object, specified by the interface
3. What happens in the method of the generated proxy object is specified by the developer writing the implementation of the handler interface.
L Beginners must understand or not understand the 2 things that must be remembered :
When the proxy class is responsible for creating a proxy object, if the handler (processor) is specified, the method calls the processor's Invoke method regardless of what method the user invokes on the proxy object.
• Because the Invoke method is called with three parameters: the proxy object, the method, and the method's arguments, you must pass in the object that you are in, the method that invokes the Invoke method, and the parameters of the method, regardless of which method of the proxy object invokes the processor's Invoke method.
4. Open Source database connection pool
L now many Web servers (Weblogic, WebSphere, Tomcat) provide a DataSource implementation, that is, the implementation of the connection pool. Usually we put the implementation of DataSource, according to its English meaning is called the data source, the data source contains the database connection pool realization.
L also have some open source organizations that provide independent implementations of the data source:
DBCP Database Connection pool
C3P0 Database Connection pool
L do not need to write the connection database code in the actual application, obtains the database connection directly from the data source. Programmers should also use the implementation of these data sources as much as possible to improve the database access performance of the program.
L DBCP is an open source connection pool implementation under the Apache Software Fund organization , and the application should add the following two jar files to the system using the DBCP data source:
Commons-dbcp.jar: Implementation of connection pooling
Commons-pool.jar: Dependency libraries for connection pooling implementations
L Tomcat's connection pool is implemented using this connection pool. This database connection pool can be used either in combination with the application server or independently by the application .